Parsing via regular expressions using format syntax
Derive will require attribute reformation to specify format string, which will be treated as format string -> regular expression string
Types implementing Reformation
by default:
i8
i16
i32
i64
i128
isize
u8
u16
u32
u64
u128
usize
f32
f64
String
, &str
char
Option<T>
It can be used to parse:
```rust use reformation::Reformation;
struct Date{ year: u16, month: u8, day: u8, hour: u8, minute: u8, }
fn main(){ let date = Date::parse("2018-12-22 20:23").unwrap();
assert_eq!(date.year, 2018);
assert_eq!(date.month, 12);
assert_eq!(date.day, 22);
assert_eq!(date.hour, 20);
assert_eq!(date.minute, 23);
} ```
```rust use reformation::Reformation;
struct Predicate(Empty, char);
struct Empty;
fn main(){ let p = Predicate::parse("Empty -> X").unwrap(); asserteq!(p.0, Empty); asserteq!(p.1, 'X'); } ```
```rust use reformation::Reformation;
enum Ant{ #[reformation(r"Queen({})")] Queen(String), #[reformation(r"Worker({})")] Worker(i32), #[reformation(r"Warrior")] Warrior }
fn main(){ let queen = Ant::parse("Queen(We are swarm)").unwrap(); asserteq!(queen, Ant::Queen("We are swarm".tostring()));
let worker = Ant::parse("Worker(900000)").unwrap();
assert_eq!(worker, Ant::Worker(900000));
let warrior = Ant::parse("Warrior").unwrap();
assert_eq!(warrior, Ant::Warrior);
} ```
Order, in which modes are specified does not matter.
Makes format string behave as regular string (in contrast with being regular expression), by escaping all special regex characters.
```rust use reformation::Reformation;
struct Vec{ x: i32, y: i32, }
fn main(){ let v= Vec::parse("Vec{-1, 1}").unwrap(); asserteq!(v.x, -1); asserteq!(v.y, 1); } ```
Allow arbitrary number of spaces after separators: ',', ';', ':'. For separator to be recognized as slack, it must be followed by at least one space in format string.
```rust use reformation::Reformation;
struct Vec{ x: i32, y: i32, }
fn main(){ let v = Vec::parse("Vec{-1,1}").unwrap(); asserteq!(v.x, -1); asserteq!(v.y, 1);
let r = Vec::parse("Vec{15, 2}").unwrap();
assert_eq!(r.x, 15);
assert_eq!(r.y, 2);
} ```
Combination of no_regex and slack behaves as expected:
```rust use reformation::Reformation;
struct Vec{ x: i32, y: i32, }
fn main(){ let v = Vec::parse("Vec(-1;1)").unwrap(); asserteq!(v.x, -1); asserteq!(v.y, 1);
let r = Vec::parse("Vec(15; 2)").unwrap();
assert_eq!(r.x, 15);
assert_eq!(r.y, 2);
} ```
Format string behaves as regular expression, so special symbols needs to be escaped.
Also they can be used for more flexible format strings.
AVOID capture groups, since they would mess up with indexing of capture group
generated by macro. use non-capturing groups r"(?:)"
instead.
```rust use reformation::Reformation;
// '{' is special symbol in both format and regex syntax, so it must be escaped twice. // Say hello to good old escape hell. Good thing its only one.
struct Vec{ x: f64, y: f64, z: f64, }
fn main(){ // spaces between coordinates does not matter, since any amount of spaces // matches to r"\s*" let v = Vec::parse("Vec{-0.4,1e-3, 2e-3}").unwrap();
assert_eq!(v.x, -0.4);
assert_eq!(v.y, 0.001);
assert_eq!(v.z, 0.002);
} ```