1#![doc = include_str!("../README.md")]
4#![no_std]
5
6#[cfg(feature = "serde")]
7mod serde_;
8
9use core::{
10 fmt::{Display, Formatter, Result as FmtResult},
11 str::FromStr,
12};
13
14iso3166_macros::generate!(lukes_json = "all.json");
15
16impl Display for Error {
17 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
18 match self {
19 Error::UnknownCode => f.write_str("Unknown Code"),
20 Error::UserAssigned => f.write_str("User Assigned"),
21 Error::InvalidLength => f.write_str("Invalid Length"),
22 Error::InvalidCharset => f.write_str("Invalid Character Set"),
23 }
24 }
25}
26
27impl Display for Numeric {
28 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
29 write!(f, "{}", *self as u16)
30 }
31}
32
33impl From<Numeric> for u16 {
34 fn from(value: Numeric) -> Self {
35 value as u16
36 }
37}
38
39impl TryFrom<u16> for Numeric {
40 type Error = Error;
41
42 fn try_from(value: u16) -> Result<Self, Self::Error> {
43 Self::from_u16(value)
44 }
45}
46
47impl TryFrom<Alpha2> for Numeric {
48 type Error = Error;
49
50 fn try_from(value: Alpha2) -> Result<Self, Self::Error> {
51 Self::from_alpha2(value)
52 }
53}
54
55impl TryFrom<Alpha3> for Numeric {
56 type Error = Error;
57
58 fn try_from(value: Alpha3) -> Result<Self, Self::Error> {
59 Self::from_alpha3(value)
60 }
61}
62
63impl Display for Alpha2 {
64 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
65 f.write_str(self.as_str())
66 }
67}
68
69impl TryFrom<Numeric> for Alpha2 {
70 type Error = Error;
71
72 fn try_from(value: Numeric) -> Result<Self, Self::Error> {
73 Self::from_numeric(value)
74 }
75}
76
77impl TryFrom<Alpha3> for Alpha2 {
78 type Error = Error;
79
80 fn try_from(value: Alpha3) -> Result<Self, Self::Error> {
81 Self::from_alpha3(value)
82 }
83}
84
85impl TryFrom<&str> for Alpha2 {
86 type Error = Error;
87
88 fn try_from(value: &str) -> Result<Self, Self::Error> {
89 Self::from_str_slice(value)
90 }
91}
92
93impl FromStr for Alpha2 {
94 type Err = Error;
95
96 fn from_str(s: &str) -> Result<Self, Self::Err> {
97 Self::from_str_slice(s)
98 }
99}
100
101impl Display for Alpha3 {
102 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
103 f.write_str(self.as_str())
104 }
105}
106
107impl TryFrom<Numeric> for Alpha3 {
108 type Error = Error;
109
110 fn try_from(value: Numeric) -> Result<Self, Self::Error> {
111 Self::from_numeric(value)
112 }
113}
114
115impl TryFrom<Alpha2> for Alpha3 {
116 type Error = Error;
117
118 fn try_from(value: Alpha2) -> Result<Self, Self::Error> {
119 Self::from_alpha2(value)
120 }
121}
122
123impl TryFrom<&str> for Alpha3 {
124 type Error = Error;
125
126 fn try_from(value: &str) -> Result<Self, Self::Error> {
127 Self::from_str_slice(value)
128 }
129}
130
131impl FromStr for Alpha3 {
132 type Err = Error;
133
134 fn from_str(s: &str) -> Result<Self, Self::Err> {
135 Self::from_str_slice(s)
136 }
137}
138
139#[cfg(test)]
140mod test {
141 extern crate std;
142
143 use super::*;
144 use std::string::ToString;
145
146 const USA_EXPECTED2: &str = "US";
147 const USA_EXPECTED3: &str = "USA";
148 const USA_EXPECTED_U16: u16 = 840;
149
150 #[test]
151 fn numeric_display() {
152 let src = Numeric::UnitedStatesOfAmerica;
153
154 assert_eq!("840", src.to_string());
155 }
156
157 #[test]
158 fn numeric_u16_roundtrip() {
159 let actual = Numeric::try_from(USA_EXPECTED_U16).expect("valid u16");
160 assert_eq!(USA_EXPECTED_U16, u16::from(actual));
161 }
162
163 #[yare::parameterized(
164 pass = {USA_EXPECTED_U16, Ok(Numeric::UnitedStatesOfAmerica)},
165 unknown = {u16::MAX, Err(Error::UnknownCode)},
166 unknown2 = {123, Err(Error::UnknownCode)},
167 )]
168 fn numeric_from_u16(input: u16, expected: Result<Numeric, Error>) {
169 let actual = Numeric::try_from(input);
170 assert_eq!(expected, actual);
171 }
172
173 #[yare::parameterized(
174 pass = {Alpha2::UnitedStatesOfAmerica, Ok(Numeric::UnitedStatesOfAmerica)},
175 user = {Alpha2::UserZZ, Err(Error::UserAssigned)},
176 )]
177 fn numeric_from_alpha2(input: Alpha2, expected: Result<Numeric, Error>) {
178 let actual = Numeric::try_from(input);
179 assert_eq!(expected, actual);
180 }
181
182 #[yare::parameterized(
183 pass = {Alpha3::UnitedStatesOfAmerica, Ok(Numeric::UnitedStatesOfAmerica)},
184 user = {Alpha3::UserZZZ, Err(Error::UserAssigned)},
185 )]
186 fn numeric_from_alpha3(input: Alpha3, expected: Result<Numeric, Error>) {
187 let actual = Numeric::try_from(input);
188 assert_eq!(expected, actual);
189 }
190
191 #[yare::parameterized(
192 pass = {USA_EXPECTED2, Ok(Alpha2::UnitedStatesOfAmerica)},
193 unknown = {"QB", Err(Error::UnknownCode)},
194 length = {"USA", Err(Error::InvalidLength)},
195 poop = {"💩", Err(Error::InvalidCharset)},
196 )]
197 fn alpha2_from_str(input: &str, expected: Result<Alpha2, Error>) {
198 let actual = Alpha2::from_str(input);
199 assert_eq!(expected, actual);
200 }
201
202 #[yare::parameterized(
203 pass = {USA_EXPECTED2, Ok(Alpha2::UnitedStatesOfAmerica)},
204 unknown = {"QB", Err(Error::UnknownCode)},
205 )]
206 fn alpha2_try_from(input: &str, expected: Result<Alpha2, Error>) {
207 let actual = Alpha2::try_from(input);
208 assert_eq!(expected, actual);
209 }
210
211 #[yare::parameterized(
212 pass = {USA_EXPECTED3, Ok(Alpha3::UnitedStatesOfAmerica)},
213 fail = {"BBB", Err(Error::UnknownCode)},
214 length = {"USAID", Err(Error::InvalidLength)},
215 poop = {"💩", Err(Error::InvalidCharset)},
216 )]
217 fn alpha3_from_str(input: &str, expected: Result<Alpha3, Error>) {
218 let actual = Alpha3::from_str(input);
219 assert_eq!(expected, actual);
220 }
221
222 #[yare::parameterized(
223 pass = {USA_EXPECTED3, Ok(Alpha3::UnitedStatesOfAmerica)},
224 fail = {"BBB", Err(Error::UnknownCode)},
225 )]
226 fn alpha3_try_from(input: &str, expected: Result<Alpha3, Error>) {
227 let actual = Alpha3::try_from(input);
228 assert_eq!(expected, actual);
229 }
230}