macro_rules! derive_deserialize_from_fromstr {
    ($type:ty, $expectation:expr) => { ... };
}
Expand description

Derives Deserialize for a type that implements FromStr.

use std::str::FromStr;
use std::num::ParseIntError;
use serde_plain::derive_deserialize_from_fromstr;

pub struct MyStruct(u32);

impl FromStr for MyStruct {
    type Err = ParseIntError;
    fn from_str(value: &str) -> Result<MyStruct, Self::Err> {
        Ok(MyStruct(value.parse()?))
    }
}

derive_deserialize_from_fromstr!(MyStruct, "valid positive number");

This automatically implements Serialize which will invoke the from_str function on the target type internally. First argument is the name of the type, the second is a message for the expectation error (human readable type effectively).