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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.

//! Parent scope
//! for modules that implement
//! miscellaneous generally-used types.

macro_rules! enum_boilerplate {
    ($name:ident ($description:expr, $default:ident, $error:ident) {
        $($variant:ident => $serialization:expr,)+
    }) => {
        #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
        pub enum $name {
            $($variant,
            )+
        }

        impl AsRef<str> for $name {
            fn as_ref(&self) -> &str {
                match *self {
                    $($name::$variant => $serialization,
                    )+
                }
            }
        }

        impl std::default::Default for $name {
            fn default() -> Self {
                $name::$default
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(formatter, "{}", self.as_ref())
            }
        }

        impl<'d> serde::de::Deserialize<'d> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: serde::de::Deserializer<'d>,
            {
                let value: String = serde::de::Deserialize::deserialize(deserializer)?;
                std::convert::TryFrom::try_from(value.as_str())
                    .map_err(|_| D::Error::invalid_value(serde::de::Unexpected::Str(&value), &$description))
            }
        }

        impl serde::ser::Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: serde::ser::Serializer,
            {
                serializer.serialize_str(self.as_ref())
            }
        }

        impl<'v> std::convert::TryFrom<&'v str> for $name {
            type Error = AppError;

            fn try_from(value: &str) -> Result<Self, Self::Error> {
                match value {
                    $($serialization => Ok($name::$variant),
                    )+
                    _ => Err(AppErrorKind::$error(value.to_owned()))?,
                }
            }
        }
    }
}

pub mod duration;
pub mod email_address;
pub mod env;
pub mod error;
pub mod headers;
pub mod logging;
pub mod provider;
pub mod regex;
pub mod validate;