use core::fmt;
use tinystr::TinyAsciiStr;
pub mod description;
pub mod max;
pub mod min;
mod max_id;
pub use max_id::MaxLangID;
pub mod en_001_territory_id;
pub mod en_001_territory_name;
pub use phf;
type PhfTinyidMap<'a> = phf::OrderedMap<&'a str, TinyID>;
type PhfOrderedMap<'a> = phf::OrderedMap<&'a str, &'a str>;
pub const fn as_tiny<const N: usize>(s: &str) -> TinyAsciiStr<N> {
match TinyAsciiStr::try_from_str(s) {
Ok(x) => x,
_ => panic!("Failed to convert as tinystr"),
}
}
type TinyAsciiID = TinyAsciiStr<4>;
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct TinyID {
pub language: TinyAsciiID,
pub script: TinyAsciiID,
pub region: TinyAsciiID,
}
impl Default for TinyID {
fn default() -> Self {
Self {
language: as_tiny(""),
script: as_tiny(""),
region: as_tiny(""),
}
}
}
impl fmt::Display for TinyID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}-{}", self.language, self.script, self.region)
}
}
impl TinyID {
pub const fn new(language: &str, script: &str, region: &str) -> Self {
Self {
language: as_tiny::<4>(language),
script: as_tiny::<4>(script),
region: as_tiny::<4>(region),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_description_map() {
let map = description::map();
let gsw_fr = map.get("gsw-FR");
assert_eq!(gsw_fr, Some(&"Schwiizertüütsch, Latiinisch, Frankriich"));
let ja = map.get("ja");
assert_eq!(ja, Some(&"日本語, 日本語の文字, 日本"));
}
fn generate_id(slice: &[&str]) {
for k in slice.iter() {
let tiny: TinyAsciiStr<4> = TinyAsciiStr::try_from_str(k).unwrap();
let byte = tiny.all_bytes();
println!("{k}: {}", u32::from_le_bytes(byte.to_owned()));
}
}
#[ignore]
#[test]
fn generate_zh_latn_cn_id() {
generate_id(&["zh", "Latn", "CN"])
}
#[ignore]
#[test]
fn generate_lzh_id() {
generate_id(&["lzh", "Hans", "CN"])
}
#[ignore]
#[test]
fn generate_ja_romaji() {
generate_id(&["ja", "Latn", "JP"])
}
}