1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/// Support for parsing strings.

// Impls a single TryFrom conversion
#[doc(hidden)]
#[macro_export]
macro_rules! impl_try_from_stringly {
    ($from:ty, $to:ty, $error:ty, $func:expr) => {
        $(#[$attr])?
        impl core::convert::TryFrom<$from> for $to {
            type Error = $error;

            fn try_from(s: $from) -> Result<Self, Self::Error> {
                $func(AsRef::<str>::as_ref(s)).map_err(|source| <$error>::new(s, source))
            }
        }

    }
}

/// Implements conversions from various string types.
///
/// This macro implements `FromStr` as well as `TryFrom<{stringly}` where `{stringly}` is one of
/// these types:
///
/// * `&str`
/// * `String`
/// * `Box<str>`
/// * `Cow<'_, str>`
///
/// The last three are only available with `alloc` feature turned on.
#[macro_export]
macro_rules! impl_parse {
    ($type:ty, $descr:expr, $func:expr, $vis:vis $error:ident, $error_source:ty $(, $error_derive:path)*) => {
        $crate::parse_error_type!($vis $error, $error_source, $descr $(, $error_derive)*);

        impl core::str::FromStr for $type {
            type Err = $error;

            fn from_str(s: &str) -> Result<Self, Self::Err> {
                $func(s).map_err(|source| <$error>::new(s, source))
            }
        }

        impl_try_from_stringly!(&str);

        #[cfg(feature = "alloc")]
        impl_try_from_stringly!(alloc::string::String, $type, $error, $func);
        #[cfg(feature = "alloc")]
        impl_try_from_stringly!(alloc::borrow::Cow<'_, str>, $type, $error, $func);
        #[cfg(feature = "alloc")]
        impl_try_from_stringly!(alloc::boxed::Box<str>, $type, $error, $func);
    }
}

/// Implements conversions from various string types as well as `serde` (de)serialization.
///
/// This calls `impl_parse` macro and implements serde deserialization by expecting and parsing a
/// string and serialization by outputting a string.
#[macro_export]
macro_rules! impl_parse_and_serde {
    ($type:ty, $descr:expr, $func:expr, $error:ident, $error_source:ty $(, $error_derive:path)*) => {
        impl_parse!($type, $descr, $func, $error, $error_source $(, $error_derive)*);

        // We don't use `serde_string_impl` because we want to avoid allocating input.
        #[cfg(feature = "serde")]
        impl<'de> $crate::serde::Deserialize<'de> for $type {
            fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
            where
                D: $crate::serde::de::Deserializer<'de>,
            {
                use core::fmt::{self, Formatter};
                use core::str::FromStr;

                struct Visitor;
                impl<'de> $crate::serde::de::Visitor<'de> for Visitor {
                    type Value = $name;

                    fn expecting(&self, f: &mut Formatter) -> fmt::Result {
                        f.write_str($descr)
                    }

                    fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
                    where
                        E: $crate::serde::de::Error,
                    {
                        s.parse().map_err(|error| {
                            $crate::serde::IntoDeError::try_into_de_error(error)
                                .unwrap_or_else(|_| E::invalid_value(Unexpected::Str(s), &self))
                        })
                    }
                }

                deserializer.deserialize_str(Visitor)
            }
        }

        #[cfg(feature = "serde")]
        impl $crate::serde::Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: $crate::serde::Serializer,
            {
                serializer.collect_str(&self)
            }
        }
    }
}