Macro serde_plain::forward_from_str_to_serde [] [src]

macro_rules! forward_from_str_to_serde {
    ($type:ty) => { ... };
    ($type:ty, |$var:ident| -> $err_type:ty { $err_conv:expr }) => { ... };
    ($type:ty, $err_type:ty) => { ... };
}

Implements FromStr for a type that forwards to serde.

#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_plain;

#[derive(Deserialize, Debug)]
pub enum MyEnum {
    VariantA,
    VariantB,
}

forward_from_str_to_serde!(MyEnum);

This automatically implements FromStr which will invoke the from_str method from this crate.

Additionally this macro supports a second argument which can be the error type to use. In that case From<serde_plain::Error> needs to be implemented for that error.

A third form with a conversion function as second argument is supported. The closure needs to be in the form |err| -> ErrType { ... }:

#[macro_use] extern crate serde_derive;
#[macro_use] extern crate serde_plain;

#[derive(Deserialize, Debug)]
pub enum MyEnum {
    VariantA,
    VariantB,
}

#[derive(Debug)]
pub struct MyError(String);

forward_from_str_to_serde!(MyEnum, |err| -> MyError { MyError(err.to_string()) });