#deserialize #regex #serde #derive-deserialize

de-regex

Regular expression based deserialization for serde

1 unstable release

0.1.0 Apr 29, 2021

#2042 in Encoding

MIT/Apache

20KB
362 lines

De-Regex

This crate contains a library that deserializes a string into a struct based on a regular expression and serde.

Example

use serde::Deserialize;

#[derive(Deserialize)]
struct Dimensions {
    width: u32,
    height: u32
}

let pattern = r"^(?P<width>\d+)x(?P<height>\d+)$";
let input = "800x600";

let dim: Dimensions = de_regex::from_str(input, pattern).unwrap();

assert_eq!(dim.width, 800);
assert_eq!(dim.height, 600);

License

This project is licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in serde_urlencoded by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

De-Regex

This crate contains a library that deserializes a string into a struct based on a regular expression and serde.

Example: Parse image dimension into struct

use serde::Deserialize;

#[derive(Deserialize)]
struct Dimension {
width: u32,
height: u32
}

let pattern = r"^(?P<width>\d+)x(?P<height>\d+)$";
let input = "800x600";

let dim: Dimension = de_regex::from_str(input, pattern)?;

assert_eq!(dim.width, 800);
assert_eq!(dim.height, 600);

Supported data types

The following data types can be used as struct fields.

  • bool: Supported values are true or false case insensitive
    Example pattern: ^(?P<group_name>(?i)(true|false))$

  • u8, u16, u32, u64: Decimal values prefixed with an optional +
    Example pattern: ^(?P<group_name>\+?\d+)$

  • i8, i16, i32, i64: Decimal values prefixed with an optional +
    Example pattern: ^(?P<group_name>[-+]?\d+)$

  • f32, f64: See the documentation of the FromStr implementation of f32/f64 for the valid syntax
    Example pattern for simple decimal floats: ^(?P<group_name>[-+]?\d+(\.\d*)?)$

  • String: A unicode (utf8) string value.
    Example pattern: ^(?P<group_name>\w*)$

  • Tuple struct: A tuple struct with one field (New Type Idiom). The struct needs to implement ´Deserialize´:

#[derive(Deserialize)]
struct NewType(i32);
  • Enum: Enums with unit variants (No newtypes and variant fields). The enum needs to implement ´Deserialize´:
#[derive(Deserialize)]
enum SomeEnum {
Foo,
#[serde(rename = "bar")]
Bar,
Baz(i32) // <- Will compile but is not available in matching because of newtype
};
  • Option<>: All types above can be used as an optional value

Other data types supported by serde might work but are not officially supported and tested.

Words of wisdom

If your regular expression looks like a behemoth no mere mortal will ever understand, please reconsider using this crate

Dependencies

~2.6–4MB
~77K SLoC