lang_id/maps/
mod.rs

1use core::fmt;
2
3use tinystr::TinyAsciiStr;
4// mod const_ids;
5pub mod description;
6pub mod max;
7pub mod min;
8
9mod max_id;
10pub use max_id::MaxLangID;
11
12// territories
13pub mod en_001_territory_id;
14pub mod en_001_territory_name;
15
16pub use phf;
17// type PhfMap<'a> = phf::Map<&'a str, &'a str>;
18type PhfTinyidMap<'a> = phf::OrderedMap<&'a str, TinyID>;
19type PhfOrderedMap<'a> = phf::OrderedMap<&'a str, &'a str>;
20// type PhfLangidMap<'a> = phf::Map<&'static str, crate::LangID>;
21
22/// Function to convert a string slice to a TinyAsciiStr with a given length N
23///
24/// ## Example
25///
26/// ```
27/// use lang_id::maps::as_tiny;
28///
29/// let latin = as_tiny::<4>("Latn");
30/// assert_eq!(latin, "Latn");
31/// ```
32pub const fn as_tiny<const N: usize>(s: &str) -> TinyAsciiStr<N> {
33  match TinyAsciiStr::try_from_str(s) {
34    Ok(x) => x,
35    _ => panic!("Failed to convert as tinystr"),
36  }
37}
38
39type TinyAsciiID = TinyAsciiStr<4>;
40/// Struct representing a language identification code with tiny string
41/// components
42#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
43pub struct TinyID {
44  pub language: TinyAsciiID,
45  pub script: TinyAsciiID,
46  pub region: TinyAsciiID,
47}
48
49impl Default for TinyID {
50  fn default() -> Self {
51    Self {
52      language: as_tiny(""),
53      script: as_tiny(""),
54      region: as_tiny(""),
55    }
56  }
57}
58
59/// Implementing the Display trait to enable printing of a TinyID struct
60impl fmt::Display for TinyID {
61  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62    write!(f, "{}-{}-{}", self.language, self.script, self.region)
63  }
64}
65
66/// Implementation block for the TinyID struct
67impl TinyID {
68  /// Constructor function for the TinyID struct
69  pub const fn new(language: &str, script: &str, region: &str) -> Self {
70    Self {
71      language: as_tiny::<4>(language),
72      script: as_tiny::<4>(script),
73      region: as_tiny::<4>(region),
74    }
75  }
76}
77
78#[cfg(test)]
79mod tests {
80  use super::*;
81
82  #[test]
83  // #[ignore]
84  fn test_description_map() {
85    let map = description::map();
86    let gsw_fr = map.get("gsw-FR");
87    assert_eq!(gsw_fr, Some(&"Schwiizertüütsch, Latiinisch, Frankriich"));
88
89    let ja = map.get("ja");
90    assert_eq!(ja, Some(&"日本語, 日本語の文字, 日本"));
91  }
92
93  fn generate_id(slice: &[&str]) {
94    for k in slice.iter() {
95      let tiny: TinyAsciiStr<4> = TinyAsciiStr::try_from_str(k).unwrap();
96      let byte = tiny.all_bytes();
97
98      println!("{k}: {}", u32::from_le_bytes(byte.to_owned()));
99    }
100  }
101
102  #[ignore]
103  #[test]
104  fn generate_zh_latn_cn_id() {
105    generate_id(&["zh", "Latn", "CN"])
106  }
107
108  #[ignore]
109  #[test]
110  fn generate_lzh_id() {
111    generate_id(&["lzh", "Hans", "CN"])
112  }
113
114  #[ignore]
115  #[test]
116  fn generate_ja_romaji() {
117    generate_id(&["ja", "Latn", "JP"])
118  }
119}