mirror of
https://github.com/erg-lang/erg.git
synced 2025-08-04 18:58:30 +00:00
Rename dir: compiler
-> crates
This commit is contained in:
parent
e1004b538d
commit
a127564b31
221 changed files with 17 additions and 19 deletions
85
crates/erg_common/lang.rs
Normal file
85
crates/erg_common/lang.rs
Normal file
|
@ -0,0 +1,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)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue