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