poke_engine/
lib.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#[cfg(feature = "gen1")]
#[path = "gen1/abilities.rs"]
pub mod abilities;
#[cfg(feature = "gen1")]
#[path = "gen1/base_stats.rs"]
pub mod base_stats;
#[cfg(feature = "gen1")]
#[path = "gen1/choice_effects.rs"]
pub mod choice_effects;
#[cfg(feature = "gen1")]
#[path = "gen1/damage_calc.rs"]
pub mod damage_calc;
#[cfg(feature = "gen1")]
#[path = "gen1/evaluate.rs"]
pub mod evaluate;
#[cfg(feature = "gen1")]
#[path = "gen1/generate_instructions.rs"]
pub mod generate_instructions;
#[cfg(feature = "gen1")]
#[path = "gen1/items.rs"]
pub mod items;
#[cfg(feature = "gen1")]
#[path = "gen1/state.rs"]
pub mod state;

#[cfg(feature = "gen2")]
#[path = "gen2/abilities.rs"]
pub mod abilities;
#[cfg(feature = "gen2")]
#[path = "gen2/choice_effects.rs"]
pub mod choice_effects;
#[cfg(feature = "gen2")]
#[path = "gen2/damage_calc.rs"]
pub mod damage_calc;
#[cfg(feature = "gen2")]
#[path = "gen2/evaluate.rs"]
pub mod evaluate;
#[cfg(feature = "gen2")]
#[path = "gen2/generate_instructions.rs"]
pub mod generate_instructions;
#[cfg(feature = "gen2")]
#[path = "gen2/items.rs"]
pub mod items;
#[cfg(feature = "gen2")]
#[path = "gen2/state.rs"]
pub mod state;

#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod abilities;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod choice_effects;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod damage_calc;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod evaluate;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod generate_instructions;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod items;
#[cfg(not(any(feature = "gen2", feature = "gen1")))]
pub mod state;

pub mod choices;
pub mod instruction;
pub mod io;
pub mod mcts;
pub mod pokemon;
pub mod search;

#[macro_export]
macro_rules! assert_unique_feature {
    () => {};
    ($first:tt $(,$rest:tt)*) => {
        $(
            #[cfg(all(feature = $first, feature = $rest))]
            compile_error!(concat!("features \"", $first, "\" and \"", $rest, "\" cannot be used together"));
        )*
        assert_unique_feature!($($rest),*);
    }
}

#[macro_export]
macro_rules! define_enum_with_from_str {
    // Case when a default variant is provided
    (
        #[repr($repr:ident)]
        $(#[$meta:meta])*
        $name:ident {
            $($variant:ident),+ $(,)?
        },
        default = $default_variant:ident
    ) => {
        #[repr($repr)]
        $(#[$meta])*
        pub enum $name {
            $($variant),+
        }

        impl std::str::FromStr for $name {
            type Err = ();

            fn from_str(input: &str) -> Result<Self, Self::Err> {
                match input.to_uppercase().as_str() {
                    $(
                        stringify!($variant) => Ok($name::$variant),
                    )+
                    _ => Ok($name::$default_variant),
                }
            }
        }

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

        impl From<$repr> for $name {
            fn from(value: $repr) -> $name {
                match value {
                    $(
                        x if x == $name::$variant as $repr => $name::$variant,
                    )+
                    _ => $name::$default_variant,
                }
            }
        }
        impl Into<$repr> for $name {
            fn into(self) -> $repr {
                self as $repr
            }
        }
    };

    // Case when no default variant is provided
    (
        #[repr($repr:ident)]
        $(#[$meta:meta])*
        $name:ident {
            $($variant:ident),+ $(,)?
        }
    ) => {
        #[repr($repr)]
        $(#[$meta])*
        pub enum $name {
            $($variant),+
        }

        impl std::str::FromStr for $name {
            type Err = ();

            fn from_str(input: &str) -> Result<Self, Self::Err> {
                match input.to_uppercase().as_str() {
                    $(
                        stringify!($variant) => Ok($name::$variant),
                    )+
                    _ => panic!("Invalid {}: {}", stringify!($name), input.to_uppercase().as_str()),
                }
            }
        }

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

        impl From<$repr> for $name {
            fn from(value: $repr) -> $name {
                match value {
                    $(
                        x if x == $name::$variant as $repr => $name::$variant,
                    )+
                    _ => panic!("Invalid {}: {}", stringify!($name), value),
                }
            }
        }
        impl Into<$repr> for $name {
            fn into(self) -> $repr {
                self as $repr
            }
        }
    };
}

assert_unique_feature!("gen2", "gen3", "gen4", "gen5", "gen6", "gen7", "gen8", "gen9");