Expand description
§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
orfalse
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
Enums§
- Error
- An error that occurred during deserialization.
Functions§
- from_
str - Deserialize an input string into a struct.
- from_
str_ regex - Deserialize an input string into a struct.