mangadex_api_types_rust/
macros.rs

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
#[macro_export]
macro_rules! include_enums {
    ($name:ident) => {
        #[derive(Clone, Copy, Debug, Deserialize, Serialize, Hash, PartialEq, Eq)]
        #[cfg_attr(feature = "specta", derive(specta::Type))]
        #[cfg_attr(feature = "async-graphql", derive(async_graphql::Enum))]
        #[serde(try_from = "u8", into = "u8")]
        pub enum $name {
            Include,
            Exclude,
        }

        impl TryFrom<u8> for $name {
            type Error = Error;
            fn try_from(value: u8) -> Result<Self, Self::Error> {
                match value {
                    0 => Ok(Self::Exclude),
                    1 => Ok(Self::Include),
                    _ => Err(Error::IncludeEnumsParsing(String::from(stringify!($name)))),
                }
            }
        }

        impl From<$name> for u8 {
            fn from(value: $name) -> Self {
                match value {
                    $name::Exclude => 0,
                    $name::Include => 1,
                }
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
                fmt.write_str(match self {
                    Self::Include => "Include",
                    Self::Exclude => "Exclude",
                })
            }
        }
    };
}