country_code/iso3166_2/
cn.rs

1//! [ISO 3166-2:CN](https://en.wikipedia.org/wiki/ISO_3166-2:CN)
2
3use crate::iso3166_1::alpha_2::CountryCode;
4
5//
6country_subdivision_code! {
7    CountryCode, CountryCode::CN;
8
9    #[derive(Debug, Clone)]
10    pub enum CountrySubdivisionCode {
11        AH,
12        BJ,
13        CQ,
14        FJ,
15        GD,
16        GS,
17        GX,
18        GZ,
19        HA,
20        HB,
21        HE,
22        HI,
23        HK,
24        HL,
25        HN,
26        JL,
27        JS,
28        JX,
29        LN,
30        MO,
31        NM,
32        NX,
33        QH,
34        SC,
35        SD,
36        SH,
37        SN,
38        SX,
39        TJ,
40        TW,
41        XJ,
42        XZ,
43        YN,
44        ZJ,
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    use alloc::string::ToString as _;
53
54    use csv::Reader;
55
56    #[test]
57    fn test_country_subdivision_code() {
58        // Wikipedia
59        let mut rdr = Reader::from_reader(include_str!("../../tests/ISO_3166-2/CN.csv").as_bytes());
60
61        let mut n = 0;
62        for record in rdr.records() {
63            let record = record.unwrap();
64            let code = &record[0];
65            assert_eq!(
66                code.parse::<CountrySubdivisionCode>().unwrap().to_string(),
67                code
68            );
69            n += 1;
70        }
71
72        assert_eq!(CountrySubdivisionCode::COUNTRY_CODE, CountryCode::CN);
73
74        assert_eq!(CountrySubdivisionCode::VARS.len(), n);
75
76        // FromStr
77        assert_eq!(
78            "CN-ZZ".parse::<CountrySubdivisionCode>().unwrap(),
79            CountrySubdivisionCode::Other("ZZ".into())
80        );
81        assert_eq!(
82            "x-y".parse::<CountrySubdivisionCode>().err().unwrap(),
83            crate::error::CountrySubdivisionCodeParseError::CountryCodeInvalid("x".into())
84        );
85        assert_eq!(
86            "ZZ-y".parse::<CountrySubdivisionCode>().err().unwrap(),
87            crate::error::CountrySubdivisionCodeParseError::CountryCodeMismatch("ZZ".into())
88        );
89        assert_eq!(
90            "CN-".parse::<CountrySubdivisionCode>().err().unwrap(),
91            crate::error::CountrySubdivisionCodeParseError::SubdivisionCodeMissing
92        );
93        assert_eq!(
94            "CN-y".parse::<CountrySubdivisionCode>().err().unwrap(),
95            crate::error::CountrySubdivisionCodeParseError::SubdivisionCodeInvalid("y".into())
96        );
97
98        // PartialEq
99        assert_eq!(CountrySubdivisionCode::BJ, CountrySubdivisionCode::BJ);
100        assert_eq!(CountrySubdivisionCode::BJ, "CN-BJ");
101
102        match CountrySubdivisionCode::BJ {
103            x if x == "CN-BJ" => {}
104            _ => panic!(),
105        }
106
107        #[cfg(feature = "std")]
108        {
109            // Hash
110            let mut h = std::collections::HashSet::new();
111            h.insert(CountrySubdivisionCode::BJ);
112            h.insert(CountrySubdivisionCode::Other("BJ".into()));
113            assert_eq!(h.len(), 1);
114        }
115
116        #[cfg(feature = "serde")]
117        {
118            #[derive(serde::Serialize, serde::Deserialize)]
119            struct Foo {
120                code: CountrySubdivisionCode,
121            }
122
123            assert_eq!(
124                serde_json::from_str::<Foo>(r#"{"code":"CN-BJ"}"#)
125                    .unwrap()
126                    .code,
127                CountrySubdivisionCode::BJ
128            );
129            assert_eq!(
130                serde_json::to_string(&Foo {
131                    code: CountrySubdivisionCode::BJ
132                })
133                .unwrap(),
134                r#"{"code":"CN-BJ"}"#
135            );
136        }
137    }
138}