mangadex_api_types_rust/
macros.rs

1#[macro_export]
2macro_rules! include_enums {
3    ($name:ident) => {
4        #[derive(Clone, Copy, Debug, Deserialize, Serialize, Hash, PartialEq, Eq)]
5        #[cfg_attr(feature = "specta", derive(specta::Type))]
6        #[cfg_attr(feature = "async-graphql", derive(async_graphql::Enum))]
7        #[serde(try_from = "u8", into = "u8")]
8        pub enum $name {
9            Include,
10            Exclude,
11        }
12
13        impl TryFrom<u8> for $name {
14            type Error = Error;
15            fn try_from(value: u8) -> Result<Self, Self::Error> {
16                match value {
17                    0 => Ok(Self::Exclude),
18                    1 => Ok(Self::Include),
19                    _ => Err(Error::IncludeEnumsParsing(String::from(stringify!($name)))),
20                }
21            }
22        }
23
24        impl From<$name> for u8 {
25            fn from(value: $name) -> Self {
26                match value {
27                    $name::Exclude => 0,
28                    $name::Include => 1,
29                }
30            }
31        }
32
33        impl std::fmt::Display for $name {
34            fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
35                fmt.write_str(match self {
36                    Self::Include => "Include",
37                    Self::Exclude => "Exclude",
38                })
39            }
40        }
41    };
42}