pest_tree/
auto.rs

1//! Implementations for basic types used by the `convert(auto)` conversion attribute.
2
3use super::*;
4/// Used internally to quickly implement PestTree<R> for types implementing FromStr.
5/// Blanket implementations cannot be used because Rust doesn't support specialization yet.
6/// Using one would mean that [`Box<T>`] can't have its own separate implementation.
7macro_rules! pest_tree_implementation {
8    ($typ:ty) => {
9        impl<R: pest::RuleType> PestTree<R> for $typ {
10            fn with_pair(
11                pair: pest::iterators::Pair<'_, R>,
12                context: Rc<ParsingContext>,
13            ) -> Result<Self, TreeError<R>>
14            where
15                Self: Sized,
16            {
17                let res = pair.as_str().parse::<$typ>();
18                if let Ok(v) = res {
19                    return Ok(v);
20                } else {
21                    return Err(StringConversionError::from_str_conversion_error::<$typ>(
22                        pair, context,
23                    ));
24                }
25            }
26        }
27    };
28}
29// for all FromStr implementing types
30pest_tree_implementation!(std::net::IpAddr);
31pest_tree_implementation!(std::net::SocketAddr);
32pest_tree_implementation!(bool);
33pest_tree_implementation!(char);
34
35pest_tree_implementation!(f32);
36pest_tree_implementation!(f64);
37
38pest_tree_implementation!(i8);
39pest_tree_implementation!(i16);
40pest_tree_implementation!(i32);
41pest_tree_implementation!(i64);
42pest_tree_implementation!(i128);
43pest_tree_implementation!(isize);
44
45pest_tree_implementation!(u8);
46pest_tree_implementation!(u16);
47pest_tree_implementation!(u32);
48pest_tree_implementation!(u64);
49pest_tree_implementation!(u128);
50pest_tree_implementation!(usize);
51
52pest_tree_implementation!(std::ffi::OsString);
53pest_tree_implementation!(std::net::Ipv4Addr);
54pest_tree_implementation!(std::net::Ipv6Addr);
55pest_tree_implementation!(std::net::SocketAddrV4);
56pest_tree_implementation!(std::net::SocketAddrV6);
57
58pest_tree_implementation!(std::num::NonZeroI8);
59pest_tree_implementation!(std::num::NonZeroI16);
60pest_tree_implementation!(std::num::NonZeroI32);
61pest_tree_implementation!(std::num::NonZeroI64);
62pest_tree_implementation!(std::num::NonZeroI128);
63pest_tree_implementation!(std::num::NonZeroIsize);
64
65pest_tree_implementation!(std::num::NonZeroU8);
66pest_tree_implementation!(std::num::NonZeroU16);
67pest_tree_implementation!(std::num::NonZeroU32);
68pest_tree_implementation!(std::num::NonZeroU64);
69pest_tree_implementation!(std::num::NonZeroU128);
70pest_tree_implementation!(std::num::NonZeroUsize);
71
72pest_tree_implementation!(std::path::PathBuf);
73pest_tree_implementation!(String);
74
75impl<R: pest::RuleType, T: PestTree<R>> PestTree<R> for Box<T> {
76    fn with_pair(
77        pair: pest::iterators::Pair<'_, R>,
78        context: Rc<ParsingContext>,
79    ) -> Result<Self, TreeError<R>>
80    where
81        Self: Sized,
82    {
83        let res = T::with_pair(pair.clone(), context.clone());
84        if let Ok(v) = res {
85            Ok(Box::new(v))
86        } else {
87            Err(BoxConversionError::from_type::<T>(pair, context))
88        }
89    }
90}
91
92impl<R: pest::RuleType, T: PestTree<R>> PestTree<R> for Option<T> {
93    fn with_pair(
94        pair: pest::iterators::Pair<'_, R>,
95        context: Rc<ParsingContext>,
96    ) -> Result<Self, TreeError<R>>
97    where
98        Self: Sized,
99    {
100        let res = T::with_pair(pair, context);
101        Ok(res.ok())
102    }
103}
104
105impl<R: pest::RuleType, T: PestTree<R>> PestTree<R> for Vec<T> {
106    fn with_pair(
107        pair: pest::iterators::Pair<'_, R>,
108        context: Rc<ParsingContext>,
109    ) -> Result<Self, TreeError<R>>
110    where
111        Self: Sized,
112    {
113        let inner = pair.into_inner();
114        let v: Result<Vec<_>, _> = inner
115            .map(|pair| T::with_pair(pair, context.clone()))
116            .collect();
117        v
118    }
119}