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
extern crate phf;

mod data;

use data::DIVISIONS;

const CURRENT_REVISION: &str = "201607";

/// The administrative division
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Division {
    /// The six-digit number of the specific administrative division
    pub code: &'static str,
    /// The Chinese name of the specific administrative division
    pub name: &'static str,
    /// The revision year (month)
    pub revision: &'static str,
}

impl Division {

    /// Return the division of the given code
    pub fn get(code: &str) -> Option<Self> {
        Self::get_by_revision(code, CURRENT_REVISION)
    }

    /// Return the division of the given code of the given revision
    pub fn get_by_revision(code: &str, revision: &str) -> Option<Self> {
        DIVISIONS.get_entry(revision).and_then(|(rev, data)| {
            data.get_entry(code).map(|(key, name)| {
                Division {
                    code: key,
                    name: name,
                    revision: rev,
                }
            })
        })
    }

    /// Searches administrative division by its code in all revision
    pub fn search(code: &str) -> Option<Self> {
        fn parse_rev_year(rev: &str) -> u32 {
            let parts: Vec<&str> = rev.split('-').collect();
            if parts.len() == 2 {
                parts[1].parse().unwrap()
            } else {
                rev.parse().unwrap()
            }
        }

        let mut revisions = Self::revisions();
        revisions.sort_unstable_by(|a, b| {
            let year_a = parse_rev_year(a);
            let year_b = parse_rev_year(b);
            year_b.cmp(&year_a)
        });
        for rev in revisions {
            let division = Self::get_by_revision(code, rev);
            if division.is_some() {
                return division;
            }
        }
        None
    }

    /// List all revisions supported by GB2260
    pub fn revisions() -> Vec<&'static str> {
        DIVISIONS.keys().cloned().collect()
    }

    /// Return province level division of current division
    pub fn province(&self) -> Self {
        let code = format!("{}0000", &self.code[..2]);
        Self::get_by_revision(&code, self.revision).unwrap()
    }

    pub fn is_province(&self) -> bool {
        *self == self.province()
    }

    /// Return prefecture level division of current division
    pub fn prefecture(&self) -> Option<Self> {
        if self.is_province() {
            return None;
        }
        let code = format!("{}00", &self.code[..4]);
        Self::get_by_revision(&code, self.revision)
    }

    pub fn is_prefecture(&self) -> bool {
        if let Some(pref) = self.prefecture() {
            pref == *self
        } else {
            false
        }
    }

    /// Return county level division of current division
    pub fn county(&self) -> Option<&Division> {
        if self.is_province() || self.is_prefecture() {
            return None;
        }
        Some(self)
    }

    pub fn is_county(&self) -> bool {
        self.county().is_some()
    }

    pub fn stack(&self) -> Vec<Self> {
        let mut res = Vec::with_capacity(3);
        res.push(self.province());
        if self.is_prefecture() || self.is_county() {
            res.push(self.prefecture().unwrap());
        }
        if self.is_county() {
            res.push(self.clone());
        }
        res
    }
}

#[cfg(test)]
mod tests {
    use super::Division;

    #[test]
    fn test_division() {
        let division = Division::get("110000").unwrap();
        assert_eq!(division.code, "110000");
        assert_eq!(division.name, "北京市");
        assert_eq!(division.revision, "201607");
        assert!(division.is_province());
        assert!(!division.is_prefecture());
        assert!(!division.is_county());
        assert_eq!(division.stack().len(), 1);

        let division = Division::get("110100").unwrap();
        assert_eq!(division.code, "110100");
        assert_eq!(division.name, "市辖区");
        assert_eq!(division.revision, "201607");
        assert!(!division.is_province());
        assert!(division.is_prefecture());
        assert!(!division.is_county());
        assert_eq!(division.stack().len(), 2);

        let division = Division::get("110101").unwrap();
        assert_eq!(division.code, "110101");
        assert_eq!(division.name, "东城区");
        assert_eq!(division.revision, "201607");
        assert!(!division.is_province());
        assert!(!division.is_prefecture());
        assert!(division.is_county());
        assert_eq!(division.stack().len(), 3);

        let division_search = Division::search("110000").unwrap();
        let division_get = Division::get("110000").unwrap();
        assert_eq!(division_search, division_get);
    }
}