From IRC:
Is there a way to tell serde to desrialize my struct using it's FromStr implementation?
extern crate serde;
use std::str::FromStr;
use serde::{de, Deserialize, Deserializer};
struct S;
impl FromStr for S {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
unimplemented!()
}
}
impl<'de> Deserialize<'de> for S {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
{
let s = String::deserialize(deserializer)?;
FromStr::from_str(&s).map_err(de::Error::custom)
}
}
Considering how this is such a common pattern, is there not a way to do this with attributes?
This is available in _serde_with_ as display_fromstr.
Most helpful comment
Considering how this is such a common pattern, is there not a way to do this with attributes?