-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathlang.rs
120 lines (113 loc) · 3.79 KB
/
lang.rs
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
use std::str::FromStr;
use crate::consts::{ERG_MODE, PYTHON_MODE};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LanguageCode {
English,
Japanese,
SimplifiedChinese,
TraditionalChinese,
Erg,
Python,
ErgOrPython,
}
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),
"erg" => Ok(Self::Erg),
"python" => Ok(Self::Python),
"erg,python" | "python,erg" => Ok(Self::ErgOrPython),
_ => 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",
LanguageCode::Erg => "erg",
LanguageCode::Python => "python",
LanguageCode::ErgOrPython => "erg,python",
}
}
}
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 erg_patterns() -> [&'static str; 2] {
["erg", "erg"]
}
pub const fn python_patterns() -> [&'static str; 2] {
["python", "python"]
}
pub const fn erg_or_python_patterns() -> [&'static str; 2] {
["erg,python", "python,erg"]
}
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(),
Self::Erg => Self::erg_patterns(),
Self::Python => Self::python_patterns(),
Self::ErgOrPython => Self::erg_or_python_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 is_erg(&self) -> bool {
matches!(self, Self::Erg | Self::ErgOrPython)
}
pub const fn is_python(&self) -> bool {
matches!(self, Self::Python | Self::ErgOrPython)
}
pub const fn is_pl(&self) -> bool {
matches!(self, Self::Erg | Self::Python | Self::ErgOrPython)
}
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"),
Self::Erg => ERG_MODE,
Self::Python => PYTHON_MODE,
Self::ErgOrPython => true,
}
}
pub fn as_str(&self) -> &str {
<&str>::from(*self)
}
}