erg_common/
lang.rs

1use std::str::FromStr;
2
3use crate::consts::{ERG_MODE, PYTHON_MODE};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum LanguageCode {
7    English,
8    Japanese,
9    SimplifiedChinese,
10    TraditionalChinese,
11    Erg,
12    Python,
13    ErgOrPython,
14}
15
16impl FromStr for LanguageCode {
17    type Err = ();
18    fn from_str(s: &str) -> Result<Self, ()> {
19        match s {
20            "english" | "en" => Ok(Self::English),
21            "japanese" | "ja" | "jp" => Ok(Self::Japanese),
22            "simplified_chinese" | "zh-CN" => Ok(Self::SimplifiedChinese),
23            "traditional_chinese" | "zh-TW" => Ok(Self::TraditionalChinese),
24            "erg" => Ok(Self::Erg),
25            "python" => Ok(Self::Python),
26            "erg,python" | "python,erg" => Ok(Self::ErgOrPython),
27            _ => Err(()),
28        }
29    }
30}
31
32impl From<LanguageCode> for &str {
33    fn from(code: LanguageCode) -> Self {
34        match code {
35            LanguageCode::English => "english",
36            LanguageCode::Japanese => "japanese",
37            LanguageCode::SimplifiedChinese => "simplified_chinese",
38            LanguageCode::TraditionalChinese => "traditional_chinese",
39            LanguageCode::Erg => "erg",
40            LanguageCode::Python => "python",
41            LanguageCode::ErgOrPython => "erg,python",
42        }
43    }
44}
45
46impl LanguageCode {
47    pub const fn en_patterns() -> [&'static str; 2] {
48        ["en", "english"]
49    }
50    pub const fn ja_patterns() -> [&'static str; 2] {
51        ["ja", "japanese"]
52    }
53    pub const fn zh_cn_patterns() -> [&'static str; 2] {
54        ["zh-CN", "simplified_chinese"]
55    }
56    pub const fn zh_tw_patterns() -> [&'static str; 2] {
57        ["zh-TW", "traditional_chinese"]
58    }
59    pub const fn erg_patterns() -> [&'static str; 2] {
60        ["erg", "erg"]
61    }
62    pub const fn python_patterns() -> [&'static str; 2] {
63        ["python", "python"]
64    }
65    pub const fn erg_or_python_patterns() -> [&'static str; 2] {
66        ["erg,python", "python,erg"]
67    }
68    pub const fn patterns(&self) -> [&'static str; 2] {
69        match self {
70            Self::English => Self::en_patterns(),
71            Self::Japanese => Self::ja_patterns(),
72            Self::SimplifiedChinese => Self::zh_cn_patterns(),
73            Self::TraditionalChinese => Self::zh_tw_patterns(),
74            Self::Erg => Self::erg_patterns(),
75            Self::Python => Self::python_patterns(),
76            Self::ErgOrPython => Self::erg_or_python_patterns(),
77        }
78    }
79
80    pub const fn is_en(&self) -> bool {
81        matches!(self, Self::English)
82    }
83    pub const fn is_ja(&self) -> bool {
84        matches!(self, Self::Japanese)
85    }
86    pub const fn is_zh_cn(&self) -> bool {
87        matches!(self, Self::SimplifiedChinese)
88    }
89    pub const fn is_zh_tw(&self) -> bool {
90        matches!(self, Self::TraditionalChinese)
91    }
92    pub const fn is_erg(&self) -> bool {
93        matches!(self, Self::Erg | Self::ErgOrPython)
94    }
95    pub const fn is_python(&self) -> bool {
96        matches!(self, Self::Python | Self::ErgOrPython)
97    }
98    pub const fn is_pl(&self) -> bool {
99        matches!(self, Self::Erg | Self::Python | Self::ErgOrPython)
100    }
101
102    pub const fn matches_feature(&self) -> bool {
103        match self {
104            Self::English => {
105                !cfg!(feature = "japanese")
106                    && !cfg!(feature = "simplified_chinese")
107                    && !cfg!(feature = "traditional_chinese")
108            }
109            Self::Japanese => cfg!(feature = "japanese"),
110            Self::SimplifiedChinese => cfg!(feature = "simplified_chinese"),
111            Self::TraditionalChinese => cfg!(feature = "traditional_chinese"),
112            Self::Erg => ERG_MODE,
113            Self::Python => PYTHON_MODE,
114            Self::ErgOrPython => true,
115        }
116    }
117    pub fn as_str(&self) -> &str {
118        <&str>::from(*self)
119    }
120}