1use core::fmt;
2use core::marker::PhantomData;
3use serde::de;
4
5pub(crate) struct FromStrVisitor<T> {
6 expecting: &'static str,
7 marker: PhantomData<T>,
8}
9
10impl<T> FromStrVisitor<T> {
11 pub(crate) fn new(expecting: &'static str) -> FromStrVisitor<T> {
12 FromStrVisitor {
13 expecting,
14 marker: PhantomData,
15 }
16 }
17}
18
19impl<T> de::Visitor<'_> for FromStrVisitor<T>
20where
21 T: std::str::FromStr,
22 T::Err: fmt::Display,
23{
24 type Value = T;
25
26 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
27 formatter.write_str(self.expecting)
28 }
29
30 fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
31 where
32 E: de::Error,
33 {
34 T::from_str(value).map_err(de::Error::custom)
35 }
36}