language_objects/
region.rs

1use std::{collections::HashMap, fmt::Debug, hash::{Hash, Hasher}};
2
3lazy_static! {
4    static ref REGION_DATA: HashMap<String, String> = {
5        serde_json::from_str::<HashMap<String, String>>(include_str!("../data/regions.json")).unwrap()
6    };
7}
8
9#[derive(Clone)]
10pub struct Region {
11    _abbrev: String,
12    _international_name: String,
13}
14
15impl Region {
16    pub fn parse<T: ToString>(abbrev: T) -> Option<Region> {
17        let abbrev = abbrev.to_string().to_uppercase();
18        let data = REGION_DATA.get(&abbrev);
19        if let Some(international_name) = data { Some(Region { _abbrev: abbrev.clone(), _international_name: international_name.clone() }) } else { None }
20    }
21
22    pub fn international_name(&self) -> String {
23        self._international_name.clone()
24    }
25
26    pub fn id(&self) -> String {
27        self.to_string()
28    }
29}
30
31impl PartialEq for Region {
32    fn eq(&self, other: &Self) -> bool {
33        self._abbrev == other._abbrev
34    }
35}
36
37impl ToString for Region {
38    fn to_string(&self) -> String {
39        self._abbrev.to_lowercase()
40    }
41}
42
43impl Debug for Region {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}", self.to_string())
46    }
47}
48
49impl Hash for Region {
50    fn hash<H: Hasher>(&self, state: &mut H) {
51        self.id().hash(state);
52    }
53}