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

Implements FromStr for a type that forwards to Deserialize.

use serde::Deserialize;
use serde_plain::derive_fromstr_from_deserialize;

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

derive_fromstr_from_deserialize!(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 { ... }:

use serde::Deserialize;
use serde_plain::derive_fromstr_from_deserialize;

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

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

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