continent_code/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3pub extern crate alloc;
4
5//
6#[macro_export]
7macro_rules! continent_code {
8    (
9        $( #[$meta:meta] )*
10        $pub:vis enum $name:ident {
11            $(
12                $( #[$variant_meta:meta] )*
13                $variant:ident $( | $alias:ident )* => $variant_en_name:literal,
14            )+
15        }
16    ) => {
17        $(#[$meta])*
18        $pub enum $name {
19            $(
20                $variant,
21            )+
22        }
23
24        //
25        impl $name {
26            pub const VARS: &'static [$name] = &[
27                $(
28                    $( #[$variant_meta] )*
29                    $name::$variant,
30                )+
31            ];
32
33            pub fn en_name(&self) -> &'static str {
34                match self {
35                    $(
36                        Self::$variant => $variant_en_name,
37                    )+
38                }
39            }
40
41            pub fn from_en_name(en_name: &str) -> Option<Self> {
42                match en_name {
43                    $(
44                        $variant_en_name => Some(Self::$variant),
45                    )+
46                    _ => None,
47                }
48            }
49        }
50
51        //
52        impl ::core::str::FromStr for $name {
53            type Err = $crate::error::ParseError;
54
55            fn from_str(s: &str) -> Result<Self, Self::Err> {
56                match s {
57                    $(
58                        ::core::stringify!($variant) $( | ::core::stringify!($alias) )* | $variant_en_name => Ok(Self::$variant),
59                    )+
60                    s => Err($crate::error::ParseError::Invalid(s.into()))
61                }
62            }
63        }
64
65        //
66        impl ::core::fmt::Display for $name {
67            fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
68                match self {
69                    $(
70                        Self::$variant => ::core::write!(f, "{}", ::core::stringify!($variant)),
71                    )+
72                }
73            }
74        }
75
76        //
77        impl ::core::default::Default for $name {
78            fn default() -> Self {
79                Self::AS
80            }
81        }
82
83        //
84        impl ::core::cmp::PartialEq for $name {
85            fn eq(&self, other: &Self) -> bool {
86                $crate::alloc::format!("{}", self) == $crate::alloc::format!("{}", other)
87            }
88        }
89
90        impl ::core::cmp::Eq for $name {
91        }
92
93        //
94        impl_macros::impl_partial_eq_str_for_display! { str, $name }
95        impl_macros::impl_partial_eq_str_for_display! { &'a str, $name }
96        impl_macros::impl_partial_eq_str_for_display! { $crate::alloc::borrow::Cow<'a, str>, $name }
97        impl_macros::impl_partial_eq_str_for_display! { $crate::alloc::string::String, $name }
98
99        //
100        #[cfg(feature = "std")]
101        impl ::std::hash::Hash for $name {
102            fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
103                $crate::alloc::format!("{}", self).hash(state);
104            }
105        }
106
107        //
108        #[cfg(feature = "serde")]
109        impl<'de> ::serde::Deserialize<'de> for $name {
110            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
111            where
112                D: ::serde::Deserializer<'de>,
113            {
114                use ::core::str::FromStr as _;
115
116                let s = $crate::alloc::boxed::Box::<str>::deserialize(deserializer)?;
117                Self::from_str(&s).map_err(::serde::de::Error::custom)
118            }
119        }
120
121        //
122        #[cfg(feature = "serde")]
123        impl ::serde::Serialize for $name {
124            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
125            where
126                S: ::serde::Serializer,
127            {
128                use $crate::alloc::string::ToString as _;
129
130                self.to_string().serialize(serializer)
131            }
132        }
133    };
134}
135
136//
137pub mod error;
138
139//
140pub mod seven;
141
142pub use seven::ContinentCode;