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