adhoc_derive  

Experimental: Derive FromStr impl based on regex provided via attribute


Usage

Add the following in your Cargo.toml: [dependencies] adhoc_derive = "0.1.1" lazy_static = "1.2.0" regex = "1.1.0"

Then, you can derive a std::str::FromStr impl as follows: ``` use adhoc_derive::FromStr;

[derive(FromStr)]

[adhoc(regex = r"^#(?P\d+) @ (?P\d+),(?P\d+): (?P\d+)x(?P\d+)$")]

struct Rectangle { id: usize, x: usize, y: usize, width: usize, height: usize, }

let rect: Rectangle = "#123 @ 3,2: 5x4".parse().unwrap(); asserteq!(123, rect.id); asserteq!(3, rect.x); asserteq!(2, rect.y); asserteq!(5, rect.width); assert_eq!(4, rect.height); ```

In general, each field of the struct needs to implement std::str::FromStr and each field identifier needs to correspond to a named capture group in the regex.

For a more comprehensive guide, refer to GUIDE.md

Limitations

This crate is experimental and has a lot of rough edges. In no particular order: * Only works on structs with named fields (i.e no enums, tuple structs, etc.) * Doesn't work with generic structs out of the box (it should work if you add the required trait bounds yourself) * Not yet implemented: optional patterns, i.e. (...)? => Option<...> * Not yet implemented: repeating patterns, i.e. (...)* => Vec<...> * Error handling and especially reporting is basically non-existent * Provided regex is not validated at compile-time