mdd_api 0.7.0

Rust API for the Mammal Diversity Database
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/// List of countries with their respective alpha-2 codes.
/// This list is based on the ISO 3166-1 alpha-2 standard.
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

/// List of (alpha-2 code, country name) tuples based on ISO 3166-1 alpha-2.
pub const COUNTRY_AND_CODES: [(&str, &str); 249] = [
    ("AF", "Afghanistan"),
    ("AX", "Ă…land Islands"),
    ("AL", "Albania"),
    ("DZ", "Algeria"),
    ("AS", "American Samoa"),
    ("AD", "Andorra"),
    ("AO", "Angola"),
    ("AI", "Anguilla"),
    ("AQ", "Antarctica"),
    ("AG", "Antigua and Barbuda"),
    ("AR", "Argentina"),
    ("AM", "Armenia"),
    ("AW", "Aruba"),
    ("AU", "Australia"),
    ("AT", "Austria"),
    ("AZ", "Azerbaijan"),
    ("BS", "Bahamas"),
    ("BH", "Bahrain"),
    ("BD", "Bangladesh"),
    ("BB", "Barbados"),
    ("BY", "Belarus"),
    ("BE", "Belgium"),
    ("BZ", "Belize"),
    ("BJ", "Benin"),
    ("BM", "Bermuda"),
    ("BT", "Bhutan"),
    ("BO", "Bolivia (Plurinational State of)"),
    ("BQ", "Bonaire, Sint Eustatius and Saba"),
    ("BA", "Bosnia and Herzegovina"),
    ("BW", "Botswana"),
    ("BV", "Bouvet Island"),
    ("BR", "Brazil"),
    ("IO", "British Indian Ocean Territory"),
    ("BN", "Brunei Darussalam"),
    ("BG", "Bulgaria"),
    ("BF", "Burkina Faso"),
    ("BI", "Burundi"),
    ("CV", "Cabo Verde"),
    ("KH", "Cambodia"),
    ("CM", "Cameroon"),
    ("CA", "Canada"),
    ("KY", "Cayman Islands"),
    ("CF", "Central African Republic"),
    ("TD", "Chad"),
    ("CL", "Chile"),
    ("CN", "China"),
    ("CX", "Christmas Island"),
    ("CC", "Cocos (Keeling) Islands"),
    ("CO", "Colombia"),
    ("KM", "Comoros"),
    ("CG", "Congo"),
    ("CD", "Congo, Democratic Republic of the"),
    ("CK", "Cook Islands"),
    ("CR", "Costa Rica"),
    ("CI", "CĂ´te d'Ivoire"),
    ("HR", "Croatia"),
    ("CU", "Cuba"),
    ("CW", "Curaçao"),
    ("CY", "Cyprus"),
    ("CZ", "Czechia"),
    ("DK", "Denmark"),
    ("DJ", "Djibouti"),
    ("DM", "Dominica"),
    ("DO", "Dominican Republic"),
    ("EC", "Ecuador"),
    ("EG", "Egypt"),
    ("SV", "El Salvador"),
    ("GQ", "Equatorial Guinea"),
    ("ER", "Eritrea"),
    ("EE", "Estonia"),
    ("SZ", "Eswatini"),
    ("ET", "Ethiopia"),
    ("FK", "Falkland Islands (Malvinas)"),
    ("FO", "Faroe Islands"),
    ("FJ", "Fiji"),
    ("FI", "Finland"),
    ("FR", "France"),
    ("GF", "French Guiana"),
    ("PF", "French Polynesia"),
    ("TF", "French Southern Territories"),
    ("GA", "Gabon"),
    ("GM", "Gambia"),
    ("GE", "Georgia"),
    ("DE", "Germany"),
    ("GH", "Ghana"),
    ("GI", "Gibraltar"),
    ("GR", "Greece"),
    ("GL", "Greenland"),
    ("GD", "Grenada"),
    ("GP", "Guadeloupe"),
    ("GU", "Guam"),
    ("GT", "Guatemala"),
    ("GG", "Guernsey"),
    ("GN", "Guinea"),
    ("GW", "Guinea-Bissau"),
    ("GY", "Guyana"),
    ("HT", "Haiti"),
    ("HM", "Heard Island and McDonald Islands"),
    ("VA", "Holy See"),
    ("HN", "Honduras"),
    ("HK", "Hong Kong"),
    ("HU", "Hungary"),
    ("IS", "Iceland"),
    ("IN", "India"),
    ("ID", "Indonesia"),
    ("IR", "Iran (Islamic Republic of)"),
    ("IQ", "Iraq"),
    ("IE", "Ireland"),
    ("IM", "Isle of Man"),
    ("IL", "Israel"),
    ("IT", "Italy"),
    ("JM", "Jamaica"),
    ("JP", "Japan"),
    ("JE", "Jersey"),
    ("JO", "Jordan"),
    ("KZ", "Kazakhstan"),
    ("KE", "Kenya"),
    ("KI", "Kiribati"),
    ("KP", "Korea (Democratic People's Republic of)"),
    ("KR", "Korea, Republic of"),
    ("KW", "Kuwait"),
    ("KG", "Kyrgyzstan"),
    ("LA", "Lao People's Democratic Republic"),
    ("LV", "Latvia"),
    ("LB", "Lebanon"),
    ("LS", "Lesotho"),
    ("LR", "Liberia"),
    ("LY", "Libya"),
    ("LI", "Liechtenstein"),
    ("LT", "Lithuania"),
    ("LU", "Luxembourg"),
    ("MO", "Macao"),
    ("MG", "Madagascar"),
    ("MW", "Malawi"),
    ("MY", "Malaysia"),
    ("MV", "Maldives"),
    ("ML", "Mali"),
    ("MT", "Malta"),
    ("MH", "Marshall Islands"),
    ("MQ", "Martinique"),
    ("MR", "Mauritania"),
    ("MU", "Mauritius"),
    ("YT", "Mayotte"),
    ("MX", "Mexico"),
    ("FM", "Micronesia (Federated States of)"),
    ("MD", "Moldova, Republic of"),
    ("MC", "Monaco"),
    ("MN", "Mongolia"),
    ("ME", "Montenegro"),
    ("MS", "Montserrat"),
    ("MA", "Morocco"),
    ("MZ", "Mozambique"),
    ("MM", "Myanmar"),
    ("NA", "Namibia"),
    ("NR", "Nauru"),
    ("NP", "Nepal"),
    ("NL", "Netherlands"),
    ("NC", "New Caledonia"),
    ("NZ", "New Zealand"),
    ("NI", "Nicaragua"),
    ("NE", "Niger"),
    ("NG", "Nigeria"),
    ("NU", "Niue"),
    ("NF", "Norfolk Island"),
    ("MK", "North Macedonia"),
    ("MP", "Northern Mariana Islands"),
    ("NO", "Norway"),
    ("OM", "Oman"),
    ("PK", "Pakistan"),
    ("PW", "Palau"),
    ("PS", "Palestine, State of"),
    ("PA", "Panama"),
    ("PG", "Papua New Guinea"),
    ("PY", "Paraguay"),
    ("PE", "Peru"),
    ("PH", "Philippines"),
    ("PN", "Pitcairn"),
    ("PL", "Poland"),
    ("PT", "Portugal"),
    ("PR", "Puerto Rico"),
    ("QA", "Qatar"),
    ("RE", "Réunion"),
    ("RO", "Romania"),
    ("RU", "Russian Federation"),
    ("RW", "Rwanda"),
    ("BL", "Saint Barthélemy"),
    ("SH", "Saint Helena, Ascension and Tristan da Cunha"),
    ("KN", "Saint Kitts and Nevis"),
    ("LC", "Saint Lucia"),
    ("MF", "Saint Martin (French part)"),
    ("PM", "Saint Pierre and Miquelon"),
    ("VC", "Saint Vincent and the Grenadines"),
    ("WS", "Samoa"),
    ("SM", "San Marino"),
    ("ST", "Sao Tome and Principe"),
    ("SA", "Saudi Arabia"),
    ("SN", "Senegal"),
    ("RS", "Serbia"),
    ("SC", "Seychelles"),
    ("SL", "Sierra Leone"),
    ("SG", "Singapore"),
    ("SX", "Sint Maarten (Dutch part)"),
    ("SK", "Slovakia"),
    ("SI", "Slovenia"),
    ("SB", "Solomon Islands"),
    ("SO", "Somalia"),
    ("ZA", "South Africa"),
    ("GS", "South Georgia and the South Sandwich Islands"),
    ("SS", "South Sudan"),
    ("ES", "Spain"),
    ("LK", "Sri Lanka"),
    ("SD", "Sudan"),
    ("SR", "Suriname"),
    ("SJ", "Svalbard and Jan Mayen"),
    ("SE", "Sweden"),
    ("CH", "Switzerland"),
    ("SY", "Syrian Arab Republic"),
    ("TW", "Taiwan, Province of China"),
    ("TJ", "Tajikistan"),
    ("TZ", "Tanzania, United Republic of"),
    ("TH", "Thailand"),
    ("TL", "Timor-Leste"),
    ("TG", "Togo"),
    ("TK", "Tokelau"),
    ("TO", "Tonga"),
    ("TT", "Trinidad and Tobago"),
    ("TN", "Tunisia"),
    ("TR", "Turkey"),
    ("TM", "Turkmenistan"),
    ("TC", "Turks and Caicos Islands"),
    ("TV", "Tuvalu"),
    ("UG", "Uganda"),
    ("UA", "Ukraine"),
    ("AE", "United Arab Emirates"),
    ("GB", "United Kingdom"),
    ("UM", "United States Minor Outlying Islands"),
    ("US", "United States of America"),
    ("UY", "Uruguay"),
    ("UZ", "Uzbekistan"),
    ("VU", "Vanuatu"),
    ("VE", "Venezuela (Bolivarian Republic of)"),
    ("VN", "Viet Nam"),
    ("VG", "Virgin Islands (British)"),
    ("VI", "Virgin Islands (U.S.)"),
    ("WF", "Wallis and Futuna"),
    ("EH", "Western Sahara"),
    ("YE", "Yemen"),
    ("ZM", "Zambia"),
    ("ZW", "Zimbabwe"),
];

// Country names found in the MDD data that are not in the ISO list.
pub const NON_STANDARD_COUNTRY_CODES: [(&str, &str); 50] = [
    ("IN", "Andaman and Nicobar Islands"),
    ("IN", "Andaman Islands"),
    ("IR", "Iran"),
    ("PT", "Azores"),
    ("PT", "Madeira"),
    ("EC", "GalĂ¡pagos Islands"),
    ("FK", "Falkland Islands"),
    ("TF", "French Southern and Antarctic Lands"),
    ("CV", "Cape Verde"),
    ("CI", "Cote d'Ivoire"),
    ("CI", "CĂ´te d'Ivoire"),
    ("ST", "SĂ£o TomĂ© and PrĂ­ncipe"),
    ("ST", "SĂ£o TomĂ© & PrĂ­ncipe"),
    ("SX", "Sint Maarten"),
    ("MP", "Northern Marianas"),
    ("AG", "Antigua & Barbuda"),
    ("BO", "Bolivia"),
    ("BA", "Bosnia & Herzegovina"),
    ("VG", "British Virgin Islands"),
    ("BN", "Brunei"),
    ("CZ", "Czech Republic"),
    ("CD", "Democratic Republic of the Congo"),
    ("TL", "East Timor"),
    ("XK", "Kosovo"),
    ("LA", "Laos"),
    ("FM", "Micronesia"),
    ("MD", "Moldova"),
    ("KP", "North Korea"),
    ("PS", "Palestine"),
    ("CG", "Republic of the Congo"),
    ("RU", "Russia"),
    ("SH", "Saint Helena"),
    ("KN", "Saint Kitts & Nevis"),
    ("MF", "Saint Martin"),
    ("VC", "Saint Vincent & the Grenadines"),
    ("KR", "South Korea"),
    ("SY", "Syria"),
    ("TW", "Taiwan"),
    ("TZ", "Tanzania"),
    ("TT", "Trinidad & Tobago"),
    ("TC", "Turks & Caicos Islands"),
    ("US", "United States"),
    ("VI", "United States Virgin Islands"),
    ("VE", "Venezuela"),
    ("VN", "Vietnam"),
    ("FO", "Faroe"),
    ("AC", "Ascension"),
    ("RE", "Reunion"),
    ("GS", "South Georgia & the South Sandwich Islands"),
    ("WF", "Wallis & Futuna"),
];

/// List of non-country region names that were commented out above.
pub const KNOWN_REGION_NAMES: [(&str, &str); 16] = [
    ("ALS", "Alaska"),
    ("AND", "Andaman Islands"),
    ("AZO", "Azores"),
    ("BON", "Bonaire"),
    ("CNY", "Canary Islands"),
    ("COC", "Cocos Islands"),
    ("GAL", "Galapagos"),
    ("KER", "Kerguelen Islands"),
    ("MAD", "Madeira"),
    ("NIC", "Nicobar Islands"),
    ("PEI", "Prince Edward Islands"),
    ("REU", "Reunion"),
    ("SAB", "Saba"),
    ("STE", "Sint Eustatius"),
    ("SGS", "South Georgia & the South Sandwich Islands"),
    ("WAF", "Wallis & Futuna"),
];

pub const US_STATE_NAMES: [&str; 50] = [
    "Alabama",
    "Alaska",
    "Arizona",
    "Arkansas",
    "California",
    "Colorado",
    "Connecticut",
    "Delaware",
    "Florida",
    "Georgia",
    "Hawai'i",
    "Idaho",
    "Illinois",
    "Indiana",
    "Iowa",
    "Kansas",
    "Kentucky",
    "Louisiana",
    "Maine",
    "Maryland",
    "Massachusetts",
    "Michigan",
    "Minnesota",
    "Mississippi",
    "Missouri",
    "Montana",
    "Nebraska",
    "Nevada",
    "New Hampshire",
    "New Jersey",
    "New Mexico",
    "New York",
    "North Carolina",
    "North Dakota",
    "Ohio",
    "Oklahoma",
    "Oregon",
    "Pennsylvania",
    "Rhode Island",
    "South Carolina",
    "South Dakota",
    "Tennessee",
    "Texas",
    "Utah",
    "Vermont",
    "Virginia",
    "Washington",
    "West Virginia",
    "Wisconsin",
    "Wyoming",
];

lazy_static::lazy_static! {
    /// A static map that maps country names to their respective alpha-2 codes.
    static ref COUNTRY_MAP: HashMap<String, String> = {
        COUNTRY_AND_CODES.iter()
            .map(|&(code, name)| (name.to_string(), code.to_string()))
            .collect()
    };

    /// A static map that maps non-standard country names to their respective alpha-2 codes.
    static ref NON_STANDARD_COUNTRY_MAP: HashMap<String, String> = {
        NON_STANDARD_COUNTRY_CODES.iter()
            .map(|&(code, name)| (name.to_string(), code.to_string()))
            .collect()
    };

    /// A static map that maps known region names to their respective alpha-2 codes.
    static ref KNOWN_REGION_MAP: HashMap<String, String> = {
        KNOWN_REGION_NAMES.iter()
            .map(|&(code, name)| (name.to_string(), code.to_string()))
            .collect()
    };

    /// A static map that combines both standard and non-standard country names.
    static ref ALL_COUNTRY_REGION_MAP: HashMap<String, String> = {
        let mut map = COUNTRY_MAP.clone();
        map.extend(NON_STANDARD_COUNTRY_MAP.clone());
        map.extend(KNOWN_REGION_MAP.clone());
        map
    };
}

/// Gets the alpha-2 country code for a given country name.
/// If the country name is not found in the standard or non-standard maps,
/// it returns the country name as a fallback.
pub fn get_country_code(country_name: &str) -> String {
    // If not found, check the non-standard country map
    if let Some(code) = ALL_COUNTRY_REGION_MAP.get(country_name) {
        code.to_string()
    } else {
        // If still not found, return the country name as is
        // This is useful for cases where the country name is not in the list
        // and we want to keep it as a fallback.
        country_name.to_string()
    }
}

/// Utility function to check if a country name is in the standard list.
/// Match U.S states to United States for consistency.
pub fn is_known_country_region(country_name: &str) -> bool {
    let country_name = if US_STATE_NAMES.contains(&country_name) {
        "United States"
    } else {
        country_name
    };
    ALL_COUNTRY_REGION_MAP.contains_key(country_name)
}

fn get_country_region_map() -> &'static HashMap<String, String> {
    &ALL_COUNTRY_REGION_MAP
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct CountryRegionCode {
    pub region_to_code: HashMap<String, String>,
    pub code_to_region: HashMap<String, String>,
}

impl CountryRegionCode {
    pub fn new() -> Self {
        let region_to_code = get_country_region_map().clone();
        let code_to_region = region_to_code
            .iter()
            .map(|(k, v)| (v.clone(), k.clone()))
            .collect();
        Self {
            region_to_code,
            code_to_region,
        }
    }

    pub fn get_code(&self, region: &str) -> Option<&String> {
        self.region_to_code.get(region)
    }

    pub fn get_region(&self, code: &str) -> Option<&String> {
        self.code_to_region.get(code)
    }

    pub fn to_json(&self) -> String {
        serde_json::to_string(self).expect("Failed to serialize CountryRegionCode")
    }

    pub fn write_to_file<P: AsRef<std::path::Path>>(&self, path: P) {
        let json = self.to_json();
        std::fs::write(path, json).expect("Failed to write CountryRegionCode to file");
    }
}