1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[cfg_attr(feature = "json-schema", derive(schemars::JsonSchema))]
13#[cfg_attr(feature = "json-schema", schemars(rename = "Landescode"))]
14#[non_exhaustive]
15pub enum Country {
16 #[serde(rename = "DE")]
18 Germany,
19
20 #[serde(rename = "AT")]
22 Austria,
23
24 #[serde(rename = "CH")]
26 Switzerland,
27
28 #[serde(rename = "NL")]
30 Netherlands,
31
32 #[serde(rename = "BE")]
34 Belgium,
35
36 #[serde(rename = "FR")]
38 France,
39
40 #[serde(rename = "LU")]
42 Luxembourg,
43
44 #[serde(rename = "PL")]
46 Poland,
47
48 #[serde(rename = "CZ")]
50 CzechRepublic,
51
52 #[serde(rename = "DK")]
54 Denmark,
55
56 #[serde(rename = "IT")]
58 Italy,
59
60 #[serde(rename = "ES")]
62 Spain,
63
64 #[serde(rename = "GB")]
66 UnitedKingdom,
67
68 #[serde(rename = "SE")]
70 Sweden,
71
72 #[serde(rename = "NO")]
74 Norway,
75
76 #[serde(rename = "FI")]
78 Finland,
79
80 #[serde(rename = "PT")]
82 Portugal,
83
84 #[serde(rename = "GR")]
86 Greece,
87
88 #[serde(rename = "IE")]
90 Ireland,
91
92 #[serde(rename = "HU")]
94 Hungary,
95
96 #[serde(rename = "SK")]
98 Slovakia,
99
100 #[serde(rename = "SI")]
102 Slovenia,
103
104 #[serde(rename = "HR")]
106 Croatia,
107
108 #[serde(rename = "RO")]
110 Romania,
111
112 #[serde(rename = "BG")]
114 Bulgaria,
115
116 #[serde(rename = "EE")]
118 Estonia,
119
120 #[serde(rename = "LV")]
122 Latvia,
123
124 #[serde(rename = "LT")]
126 Lithuania,
127
128 #[serde(rename = "CY")]
130 Cyprus,
131
132 #[serde(rename = "MT")]
134 Malta,
135
136 #[serde(rename = "LI")]
138 Liechtenstein,
139
140 #[serde(rename = "IS")]
142 Iceland,
143}
144
145impl Country {
146 pub fn german_name(&self) -> &'static str {
148 match self {
149 Self::Germany => "Deutschland",
150 Self::Austria => "Oesterreich",
151 Self::Switzerland => "Schweiz",
152 Self::Netherlands => "Niederlande",
153 Self::Belgium => "Belgien",
154 Self::France => "Frankreich",
155 Self::Luxembourg => "Luxemburg",
156 Self::Poland => "Polen",
157 Self::CzechRepublic => "Tschechien",
158 Self::Denmark => "Daenemark",
159 Self::Italy => "Italien",
160 Self::Spain => "Spanien",
161 Self::UnitedKingdom => "Vereinigtes Koenigreich",
162 Self::Sweden => "Schweden",
163 Self::Norway => "Norwegen",
164 Self::Finland => "Finnland",
165 Self::Portugal => "Portugal",
166 Self::Greece => "Griechenland",
167 Self::Ireland => "Irland",
168 Self::Hungary => "Ungarn",
169 Self::Slovakia => "Slowakei",
170 Self::Slovenia => "Slowenien",
171 Self::Croatia => "Kroatien",
172 Self::Romania => "Rumaenien",
173 Self::Bulgaria => "Bulgarien",
174 Self::Estonia => "Estland",
175 Self::Latvia => "Lettland",
176 Self::Lithuania => "Litauen",
177 Self::Cyprus => "Zypern",
178 Self::Malta => "Malta",
179 Self::Liechtenstein => "Liechtenstein",
180 Self::Iceland => "Island",
181 }
182 }
183
184 pub fn alpha2_code(&self) -> &'static str {
186 match self {
187 Self::Germany => "DE",
188 Self::Austria => "AT",
189 Self::Switzerland => "CH",
190 Self::Netherlands => "NL",
191 Self::Belgium => "BE",
192 Self::France => "FR",
193 Self::Luxembourg => "LU",
194 Self::Poland => "PL",
195 Self::CzechRepublic => "CZ",
196 Self::Denmark => "DK",
197 Self::Italy => "IT",
198 Self::Spain => "ES",
199 Self::UnitedKingdom => "GB",
200 Self::Sweden => "SE",
201 Self::Norway => "NO",
202 Self::Finland => "FI",
203 Self::Portugal => "PT",
204 Self::Greece => "GR",
205 Self::Ireland => "IE",
206 Self::Hungary => "HU",
207 Self::Slovakia => "SK",
208 Self::Slovenia => "SI",
209 Self::Croatia => "HR",
210 Self::Romania => "RO",
211 Self::Bulgaria => "BG",
212 Self::Estonia => "EE",
213 Self::Latvia => "LV",
214 Self::Lithuania => "LT",
215 Self::Cyprus => "CY",
216 Self::Malta => "MT",
217 Self::Liechtenstein => "LI",
218 Self::Iceland => "IS",
219 }
220 }
221}
222
223#[cfg(test)]
224mod tests {
225 use super::*;
226
227 #[test]
228 fn test_serialize() {
229 assert_eq!(serde_json::to_string(&Country::Germany).unwrap(), r#""DE""#);
230 assert_eq!(serde_json::to_string(&Country::Austria).unwrap(), r#""AT""#);
231 }
232
233 #[test]
234 fn test_deserialize() {
235 assert_eq!(
236 serde_json::from_str::<Country>(r#""DE""#).unwrap(),
237 Country::Germany
238 );
239 assert_eq!(
240 serde_json::from_str::<Country>(r#""CH""#).unwrap(),
241 Country::Switzerland
242 );
243 }
244
245 #[test]
246 fn test_roundtrip() {
247 for country in [
248 Country::Germany,
249 Country::Austria,
250 Country::Switzerland,
251 Country::Netherlands,
252 Country::Belgium,
253 Country::France,
254 Country::Luxembourg,
255 Country::Poland,
256 ] {
257 let json = serde_json::to_string(&country).unwrap();
258 let parsed: Country = serde_json::from_str(&json).unwrap();
259 assert_eq!(country, parsed);
260 }
261 }
262
263 #[test]
264 fn test_alpha2_code() {
265 assert_eq!(Country::Germany.alpha2_code(), "DE");
266 assert_eq!(Country::Austria.alpha2_code(), "AT");
267 }
268}