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
use crate::iso_compat::{Err as IsoErr, IsoCompat};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;
include!(concat!(env!("OUT_DIR"), "/iso_639.rs"));
impl IsoCompat for Iso639 {
fn name(&self) -> &str {
TO_NAME[&(*self as i32)]
}
fn iso639_3(&self) -> &str {
TO_ISO639_3[&(*self as i32)]
}
fn iso639_1(&self) -> Option<&str> {
Some(*(TO_ISO639_1.get(&(*self as i32))?))
}
fn autonym(&self) -> Option<&str> {
Some(*(TO_AUTONYM.get(&(*self as i32))?))
}
fn from_name(name: &str) -> Result<Self, IsoErr> {
FROM_NAMES
.get(name)
.map_or(Err(IsoErr::UnknownName(name.to_string())), |s| Ok(*s))
}
fn from_iso639_3(code: &str) -> Result<Self, IsoErr> {
FROM_ISO639_3.get(code).map_or(
Err(IsoErr::UnknownLanguage(code.to_ascii_lowercase())),
|s| Ok(*s),
)
}
fn from_iso639_1(code: &str) -> Result<Self, IsoErr> {
FROM_ISO639_1.get(code).map_or(
Err(IsoErr::UnknownIso639_1(code.to_ascii_lowercase())),
|s| Ok(*s),
)
}
fn from_autonym(autonym: &str) -> Result<Self, IsoErr> {
FROM_AUTONYM
.get(autonym)
.map_or(Err(IsoErr::UnknownAutonym(autonym.to_string())), |s| Ok(*s))
}
}
impl FromStr for Iso639 {
type Err = IsoErr;
fn from_str(s: &str) -> Result<Self, IsoErr> {
Iso639::from_iso639_3(s)
}
}
impl Serialize for Iso639 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.iso639_3())
}
}
impl<'de> Deserialize<'de> for Iso639 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
Iso639::from_str(&s).map_err(serde::de::Error::custom)
}
}