From 34267f6939ef66cecd53fb691a0c17b1e314e05f Mon Sep 17 00:00:00 2001 From: vodofrede Date: Thu, 21 Oct 2021 16:07:28 +0200 Subject: [PATCH] 0.1 --- .gitignore | 1 + Cargo.lock | 7 ++++++ Cargo.toml | 6 +++++ src/lib.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 17 +++++++++++++ 5 files changed, 102 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..cabb0f4 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "eyes" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..54b79f8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "eyes" +version = "0.1.0" +edition = "2018" + +[dependencies] diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ff4de5f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,71 @@ +pub struct Parser<'a> { + _input: &'a str, + _pattern: &'a str, + captures: Vec<&'a str>, +} + +impl<'a> Parser<'a> { + pub fn new(input: &'a str, pattern: &'a str) -> Self { + let splits = pattern + .split("{}") + .filter(|pat| pat != &"") + .collect::>(); + let mut captures = vec![input]; + + println!("splits: {:?}", splits); + + for pat in splits { + captures = captures + .iter() + .map(|sub| sub.split(pat)) + .flatten() + .collect(); + } + + Self { + _input: input, + _pattern: pattern, + captures: captures[1..].to_vec(), + } + } + + pub fn captures(&self) -> Vec<&'a str> { + self.captures.to_owned() + } +} + +#[macro_export] +macro_rules! parse { + ($input: expr, $pattern: tt, $($type:ty),*) => { + { + let mut parser = eyes::Parser::new($input, $pattern); + let mut captures = parser.captures(); + captures.reverse(); + println!("caps: {:?}", captures); + + ( + $({ + captures.pop().unwrap().parse::<$type>().unwrap() + },)* + ) + } + }; +} + +#[macro_export] +macro_rules! try_parse { + ($input: expr, $pattern: tt, $($type:ty),*) => { + { + let mut parser = eyes::Parser::new($input, $pattern); + let mut captures = parser.captures(); + captures.reverse(); + println!("caps: {:?}", captures); + + ( + $({ + captures.pop().unwrap().parse::<$type>() + },)* + ) + } + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b6c4827 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +use eyes::*; + +fn main() { + let input = "#1 @ 338,764: 20x24"; + let pattern = "#{} @ {},{}: {}x{}"; + + println!("input: {}", input); + println!("pattern: {}", pattern); + + let (id, x, y, w, h) = parse!(input, pattern, usize, isize, isize, usize, usize); + + println!("id: {:?}", id); + println!("x: {:?}", x); + println!("y: {:?}", y); + println!("w: {:?}", w); + println!("h: {:?}", h); +}