use compact_str::ToCompactString;
use tap::{Pipe, Tap};
use crate::{LangID, maps::TinyID};
#[derive(Debug, Clone)]
pub enum MaxLangID {
Regular(LangID),
Tiny(TinyID),
}
impl core::fmt::Display for MaxLangID {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}-{}-{}",
self.get_language(),
self.get_script(),
self.get_region()
)
}
}
impl MaxLangID {
pub fn new(language: &LangID) -> Self {
let map = crate::maps::max::map();
match map.get(&language.to_compact_string()) {
Some(id) => Self::Tiny(*id),
_ => language
.clone()
.tap_mut(|x| {
x.maximize();
})
.pipe(MaxLangID::Regular),
}
}
pub fn get_language(&self) -> &str {
match self {
Self::Regular(id) => id.language.as_str(),
Self::Tiny(id) => id.language.as_str(),
}
}
pub fn get_script(&self) -> &str {
match self {
Self::Regular(id) => match &id.script {
Some(s) => s.as_str(),
_ => "",
},
Self::Tiny(id) => id.script.as_str(),
}
}
pub fn get_region(&self) -> &str {
match self {
Self::Regular(id) => match &id.region {
Some(s) => s.as_str(),
_ => "",
},
Self::Tiny(id) => id.region.as_str(),
}
}
}