diff --git a/examples/errors.rs b/examples/errors.rs new file mode 100644 index 0000000..ee361ac --- /dev/null +++ b/examples/errors.rs @@ -0,0 +1,26 @@ +extern crate lua_patterns; + +fn main() { + let bad = [ + ("bonzo %","malformed pattern (ends with '%')"), + ("bonzo (dog%(","unfinished capture"), + ("alles [%a%[","malformed pattern (missing ']')"), + ("bonzo (dog (cat)","unfinished capture"), + ("frodo %f[%A","malformed pattern (missing ']')"), + ("frodo (1) (2(3)%2)%1","invalid capture index %2"), + ]; + + fn error(s: &str) -> lua_patterns::PatternError { + lua_patterns::PatternError(s.into()) + } + + for p in bad.iter() { + let res = lua_patterns::LuaPattern::new_try(p.0); + if let Err(e) = res { + assert_eq!(e, error(p.1)); + } else { + println!("'{}' was fine",p.0); + } + } + +} diff --git a/examples/multiple_captures.rs b/examples/multiple_captures.rs new file mode 100644 index 0000000..682807b --- /dev/null +++ b/examples/multiple_captures.rs @@ -0,0 +1,9 @@ +extern crate lua_patterns as lp; + +fn main() { + let mut p = lp::LuaPattern::new("%s*(%d+)%s+(%S+)"); + if let Some((int,rest)) = p.match_maybe_2(" 233 hello dolly") { + assert_eq!(int,"233"); + assert_eq!(rest,"hello"); + } +}