-
Introduce
serde(try_from = "...")
attribute to derive Deserialize based on a given implementation ofstd::convert::TryFrom
(#1526, thanks @fanzeyi)#[derive(Deserialize)] #[serde(try_from = "u32")] enum N { Zero, One, Two, } impl TryFrom<u32> for N { type Error = String; fn try_from(u: u32) -> Result<Self, Self::Error> { match u { 0 => Ok(Self::Zero), 1 => Ok(Self::One), 2 => Ok(Self::Two), other => Err(format!("out of range: {}", other)), } } }