Skip to main content

gdlib/
lib.rs

1#![warn(clippy::all)]
2#![deny(missing_docs)]
3#![doc = include_str!("../README.md")]
4
5pub mod ccgamemanager;
6pub mod cclocallevels;
7pub mod core;
8
9#[cfg(test)]
10pub mod tests;
11
12/// Enum definition helper
13macro_rules! repr_t {
14    ($(#[$meta:meta])* $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? }) => {
15        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16        #[allow(missing_docs)]
17        $(#[$meta])*
18        pub enum $name {
19            $(
20                $(#[$vmeta])*
21                $variant,
22            )*
23            /// Value that is unaccounted for (unknown case or corrupt value)
24            Unrecognized($t)
25        }
26
27        impl From<$t> for $name {
28            fn from(value: $t) -> Self {
29                match value {
30                    $($val => Self::$variant,)*
31                    _ => Self::Unrecognized(value),
32                }
33            }
34        }
35        #[allow(missing_docs)]
36
37        impl $name {
38            /// Converts `self` to its numeric type. Autogenerated by the `crate::repr_t` macro.
39            ///
40            /// Normally, `#[repr(t)]` would be used here as that would serve as the same thing. However, for cases where a value may be parsed that isn't accounted for, an `Unrecognized` variant is necessary, which allows us to catch "other" values.
41            pub fn to_num(&self) -> $t {
42                match self {
43                    Self::Unrecognized(n) => *n,
44                    $(Self::$variant => $val,)*
45                }
46            }
47        }
48    };
49
50    ($(#[$meta:meta])* $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? } default $default:ident) => {
51        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
52        #[allow(missing_docs)]
53        $(#[$meta])*
54        pub enum $name {
55            $(
56                $(#[$vmeta])*
57                $variant,
58            )*
59            /// Value that is unaccounted for (unknown case or corrupt value)
60            Unrecognized($t)
61        }
62
63        impl From<$t> for $name {
64            fn from(value: $t) -> Self {
65                match value {
66                    $($val => Self::$variant,)*
67                    _ => Self::Unrecognized(value),
68                }
69            }
70        }
71
72        impl $name {
73            /// Converts `self` to its numeric type. Autogenerated by the `crate::repr_t` macro.
74            pub fn to_num(&self) -> $t {
75                match self {
76                    Self::Unrecognized(n) => *n,
77                    $(Self::$variant => $val,)*
78                }
79            }
80        }
81
82        impl Default for $name {
83            fn default() -> Self {
84                Self::$default
85            }
86        }
87    };
88
89    // next two macros are for enums which have a known and fixed set of values where an unrecognized value would be irregular. e.g. speed (0.5x, 1x, 2x, 3x, 4x)
90
91    ($(#[$meta:meta])* strict $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? }) => {
92        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
93        #[allow(missing_docs)]
94        #[repr($t)]
95        $(#[$meta])*
96        pub enum $name {
97            $(
98                $(#[$vmeta])*
99                $variant,
100            )*
101        }
102
103        impl TryFrom<$t> for $name {
104            type Error = $t;
105            fn try_from(value: $t) -> Result<Self, Self::Error> {
106                match value {
107                    $($val => Ok(Self::$variant),)*
108                    _ => Err(value),
109                }
110            }
111        }
112    };
113
114    ($(#[$meta:meta])* strict $name:ident : $t:ty { $( $(#[$vmeta:meta])* $variant:ident = $val:expr),* $(,)? } default $default:ident) => {
115        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
116        #[allow(missing_docs)]
117        #[repr($t)]
118        $(#[$meta])*
119        pub enum $name {
120            $(
121                $(#[$vmeta])*
122                $variant,
123            )*
124        }
125
126        impl TryFrom<$t> for $name {
127            type Error = $t;
128            fn try_from(value: $t) -> Result<Self, Self::Error> {
129                match value {
130                    $($val => Ok(Self::$variant),)*
131                    _ => Err(value),
132                }
133            }
134        }
135
136        impl Default for $name {
137            fn default() -> Self {
138                Self::$default
139            }
140        }
141    };
142}
143pub(crate) use repr_t;