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
macro_rules! mapping {
($($alpha_2_code:ident $alpha_3_code:ident => $numeric_code:expr, $short_name_en:expr, $short_name_uppercase_en:expr, $full_name_en:expr, $short_name_zh:expr;)+) => {
#[derive(Debug, Clone, Copy)]
#[repr(u16)]
/// ISO 3166-1 country codes mapping.
///
/// The Chinese names of countries and regions are determined according to GB/T 2659-2000 (equivalent to ISO 3166-1:1997).
/// For those not included in GB/T 2659-2000 (i.e., countries and regional entries newly added in ISO 3166.1-2006),
/// Chinese names are given according to conventional usage.
pub enum Iso3166 {
$(
$alpha_3_code = $numeric_code,
)+
}
impl Iso3166 {
/// Returns a slice of all ISO 3166-1 country codes.
pub const fn all() -> &'static [Self] {
&[
$(
Self::$alpha_3_code,
)+
]
}
/// Returns the ISO 3166-1 country code from the alpha-2 code.
pub fn from_alpha_2_code(code: &str) -> Option<Self> {
match code {
$(
_ if stringify!($alpha_2_code).eq_ignore_ascii_case(code) => Some(Self::$alpha_3_code),
)+
_ => None,
}
}
/// Returns the ISO 3166-1 country code from the alpha-3 code.
pub fn from_alpha_3_code(code: &str) -> Option<Self> {
match code {
$(
_ if stringify!($alpha_3_code).eq_ignore_ascii_case(code) => Some(Self::$alpha_3_code),
)+
_ => None,
}
}
#[inline]
/// Returns the ISO 3166-1 country code from the numeric code.
pub const fn from_numeric_code(code: u16) -> Option<Self> {
match code {
$(
$numeric_code => Some(Self::$alpha_3_code),
)+
_ => None,
}
}
/// Returns the ISO 3166-1 alpha-2 code.
pub const fn alpha_2_code(self) -> &'static str {
use Iso3166::*;
match self {
$(
$alpha_3_code => stringify!($alpha_2_code),
)+
}
}
/// Returns the ISO 3166-1 alpha-3 code.
pub const fn alpha_3_code(self) -> &'static str {
use Iso3166::*;
match self {
$(
$alpha_3_code => stringify!($alpha_2_code),
)+
}
}
#[inline]
/// Returns the ISO 3166-1 numeric code.
pub const fn numeric_code(self) -> u16 {
self as u16
}
#[cfg(feature = "feat-iso3166-short-name-en")]
/// Returns the short name in English.
pub const fn short_name_en(self) -> Option<&'static str> {
use Iso3166::*;
match self {
$(
$alpha_3_code => $short_name_en,
)+
}
}
#[cfg(feature = "feat-iso3166-short-name-uppercase-en")]
/// Returns the short name in uppercase English.
pub const fn short_name_uppercase_en(self) -> Option<&'static str> {
use Iso3166::*;
match self {
$(
$alpha_3_code => $short_name_uppercase_en,
)+
}
}
#[cfg(feature = "feat-iso3166-full-name-en")]
/// Returns the full name in English.
pub const fn full_name_en(self) -> Option<&'static str> {
use Iso3166::*;
match self {
$(
$alpha_3_code => $full_name_en,
)+
}
}
#[cfg(feature = "feat-iso3166-short-name-zh")]
/// Returns the short name in Chinese.
pub const fn short_name_zh(self) -> Option<&'static str> {
use Iso3166::*;
match self {
$(
$alpha_3_code => $short_name_zh,
)+
}
}
}
}
}