use std::fmt;
use std::marker::PhantomData;
use std::str::FromStr;
pub(crate) struct FromStrVisitor<T> {
expecting: &'static str,
target: PhantomData<T>,
}
impl<T> FromStrVisitor<T> {
pub(crate) const fn new(expecting: &'static str) -> Self {
Self {
expecting,
target: PhantomData,
}
}
}
impl<T> serde::de::Visitor<'_> for FromStrVisitor<T>
where
T: FromStr,
T::Err: fmt::Display,
{
type Value = T;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.expecting)
}
fn visit_str<E: serde::de::Error>(self, text: &str) -> Result<T, E> {
text.parse().map_err(E::custom)
}
}