1use core::fmt;
2
3use tinystr::TinyAsciiStr;
4pub mod description;
6pub mod max;
7pub mod min;
8
9mod max_id;
10pub use max_id::MaxLangID;
11
12pub mod en_001_territory_id;
14pub mod en_001_territory_name;
15
16pub use phf;
17type PhfTinyidMap<'a> = phf::OrderedMap<&'a str, TinyID>;
19type PhfOrderedMap<'a> = phf::OrderedMap<&'a str, &'a str>;
20pub 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#[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
59impl 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
66impl TinyID {
68 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 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}