Simple code generator for manual parsing
There are several attributes that control behaviour of parser Each, attached to struct's field
starts_with = <prefix>
- Specifies string with which next parse step should start(can be stacked). Errors if prefix is missing.ends_with = <prefix>
- Specifies string with which parse step should end(can be stacked). Errors if suffix is missing. If empty, expects EOF.skip = <chars>
- Specifies to skip characters, until not meeting character outside of specified in string.skip(ws)
- Specifies to skip all white space characters.format(<format>)
- Specifies list of characters that should contain value to parse from.format(not(<format>))
- Specifies list of characters that should contain value to parse from.format
, it is used as set of characters.numeric
- When specified, match using char::is_numeric()
digit(<base>)
- When specified, match using char::is_digit(<base>)
ascii
- When specified, match using char::is_ascii()
alphabetic
- When specified, match using char::is_alphabetic()
format = <format>
- Specifies string to match against.case
- Specifies case sensitive match. By default it is insensitive.default
- Specifies variant as taking default value. Should take only single String
and
there can be only one```rust use std::str::FromStr;
pub struct Data { #[startswith = "prefix"] #[skip(ws)] #[startswith = "+"] #[skip(ws)] #[format(ascii)] #[format(digit(10))] pub first: u32, #[skip(ws)] #[format(not("d"))] #[format("13")] #[endswith = "d"] #[endswith = ""] pub second: String, }
fn main() { let data = Data::fromstr("prefix + 666 13d").expect("Parse"); asserteq!(data.first, 666); assert_eq!(data.second, "13");
let data = Data::from_str("prefix + 666 13");
assert!(data.is_err());
let data = Data::from_str("prefix 666 13d");
assert!(data.is_err());
let data = Data::from_str("prefix + 666 13dg");
assert!(data.is_err());
let data = Data::from_str("");
assert!(data.is_err());
}
```
```rust use std::str::FromStr;
enum Gender { Male, #[case] Female, #[default] Other(String) }
fn main() { let gender = Gender::fromstr("male").expect("Parse"); asserteq!(gender, Gender::Male);
let gender = Gender::from_str("female").expect("Parse");
match gender {
Gender::Other(text) => assert_eq!(text, "female"),
_ => panic!("Unexpected!")
}
let gender = Gender::from_str("none").expect("Parse");
match gender {
Gender::Other(text) => assert_eq!(text, "none"),
_ => panic!("Unexpected!")
}
} ```