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