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
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LanguageCode {
English,
Japanese,
SimplifiedChinese,
TraditionalChinese,
}
impl FromStr for LanguageCode {
type Err = ();
fn from_str(s: &str) -> Result<Self, ()> {
match s {
"english" | "en" => Ok(Self::English),
"japanese" | "ja" | "jp" => Ok(Self::Japanese),
"simplified_chinese" | "zh-CN" => Ok(Self::SimplifiedChinese),
"traditional_chinese" | "zh-TW" => Ok(Self::TraditionalChinese),
_ => Err(()),
}
}
}
impl From<LanguageCode> for &str {
fn from(code: LanguageCode) -> Self {
match code {
LanguageCode::English => "english",
LanguageCode::Japanese => "japanese",
LanguageCode::SimplifiedChinese => "simplified_chinese",
LanguageCode::TraditionalChinese => "traditional_chinese",
}
}
}
impl LanguageCode {
pub const fn en_patterns() -> [&'static str; 2] {
["en", "english"]
}
pub const fn ja_patterns() -> [&'static str; 2] {
["ja", "japanese"]
}
pub const fn zh_cn_patterns() -> [&'static str; 2] {
["zh-CN", "simplified_chinese"]
}
pub const fn zh_tw_patterns() -> [&'static str; 2] {
["zh-TW", "traditional_chinese"]
}
pub const fn patterns(&self) -> [&'static str; 2] {
match self {
Self::English => Self::en_patterns(),
Self::Japanese => Self::ja_patterns(),
Self::SimplifiedChinese => Self::zh_cn_patterns(),
Self::TraditionalChinese => Self::zh_tw_patterns(),
}
}
pub const fn is_en(&self) -> bool {
matches!(self, Self::English)
}
pub const fn is_ja(&self) -> bool {
matches!(self, Self::Japanese)
}
pub const fn is_zh_cn(&self) -> bool {
matches!(self, Self::SimplifiedChinese)
}
pub const fn is_zh_tw(&self) -> bool {
matches!(self, Self::TraditionalChinese)
}
pub const fn matches_feature(&self) -> bool {
match self {
Self::English => {
!cfg!(feature = "japanese")
&& !cfg!(feature = "simplified_chinese")
&& !cfg!(feature = "traditional_chinese")
}
Self::Japanese => cfg!(feature = "japanese"),
Self::SimplifiedChinese => cfg!(feature = "simplified_chinese"),
Self::TraditionalChinese => cfg!(feature = "traditional_chinese"),
}
}
pub fn as_str(&self) -> &str {
<&str>::from(*self)
}
}