Macro serde_plain::derive_deserialize_from_str [] [src]

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

Derives serde::Deserialize a type that implements FromStr.

use std::str::FromStr;
use std::num::ParseIntError;
#[macro_use] extern crate serde;
#[macro_use] extern crate serde_plain;

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_str!(MyStruct, "valid positive number");

This automatically implements fmt::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).