lang_id/
lib.rs

1// cargo +nightly rustdoc --all-features -- --cfg __unstable_doc
2// --document-private-items; open $CARGO_TARGET_DIR/doc/lang_id/index.html
3#![cfg_attr(__unstable_doc, feature(doc_auto_cfg, doc_notable_trait))]
4#![no_std]
5
6//! This library provides a series of const lang-ids which can be found in the
7//! `consts` module. Additionally, it provides some handy maps.
8//!
9//! # Examples
10//!
11//! Using the result of a const fn as a value:
12//!
13//! ```
14//! use lang_id::LangID;
15//!
16//! const DEFAULT_LANG: LangID = unsafe { lang_id::consts::get_en() };
17//! ```
18//!
19//! Maximize:
20//!
21//! Note: Finding Maximized by `max::map()` does not enumerate all cases.
22//!
23//! ```
24//! let map = lang_id::maps::max::map();
25//! let zh = &map["zh"];
26//! assert_eq!(zh.language, "zh");
27//! assert_eq!(zh.script, "Hans");
28//! assert_eq!(zh.region, "CN");
29//! ```
30//!
31//! Minimize:
32//!
33//! ```
34//! let map = lang_id::maps::min::map();
35//!
36//! let sg = map.get("zh-Hans-SG");
37//! assert_eq!(sg, Some(&"zh-SG"));
38//! ```
39//!
40//! Get description of a language:
41//!
42//! ```
43//! let map = lang_id::maps::description::map();
44//! let gsw_fr = map.get("gsw-FR");
45//! assert_eq!(gsw_fr, Some(&"Schwiizertüütsch, Latiinisch, Frankriich"));
46//!
47//! let ja = map.get("ja");
48//! assert_eq!(ja, Some(&"日本語, 日本語の文字, 日本"));
49//! ```
50pub mod consts;
51mod id;
52pub use unic_langid::LanguageIdentifier as LangID;
53
54pub mod error {
55  pub use unic_langid::LanguageIdentifierError as LangidError;
56}
57
58#[cfg(feature = "map")]
59pub mod maps;
60
61#[cfg(feature = "match")]
62pub mod matches;
63
64/// The sys-locale module is used to get the current system's locale and convert
65/// it into LangID.
66#[cfg(feature = "sys-locale")]
67pub mod sys_locale;
68
69#[cfg(feature = "std")]
70extern crate std;
71
72// #[cfg(feature = "alloc")]
73// extern crate alloc;
74
75#[cfg(feature = "std")]
76#[cfg(test)]
77mod tests {
78  use std::{borrow::ToOwned, boxed::Box, dbg, println};
79
80  use super::*;
81  use crate::matches::match_id;
82
83  // #[test]
84  // #[cfg(feature = "map")]
85  // fn minimize_zh_sg() {
86  //   let map = crate::maps::min::map();
87  //   let sg = map["zh-Hans-SG"];
88  //   assert_eq!(sg, "zh-SG");
89  // }
90  #[test]
91  #[cfg(feature = "map")]
92  fn get_const_ids() {
93    let now = std::time::Instant::now();
94    let map = maps::description::map();
95    for (k, v) in &map {
96      println!("{k} {v}")
97    }
98    // let id = match_id(Box::new("en-GB").as_bytes());
99    // let ja = map.get("ja");
100    // println!("{id:?}");
101    // dbg!(id);
102    println!("{:?}", now.elapsed());
103    // dbg!(id);
104  }
105
106  #[test]
107  fn test_get_en() {
108    let default_lang = unsafe { crate::consts::get_en() };
109    dbg!(default_lang.language);
110  }
111}
112
113// #[ignore]
114#[cfg(test)]
115#[cfg(feature = "std")]
116/// ## Example
117///
118/// ```no_run
119/// simple_benchmark(|| { /* do sth. */ })
120/// ```
121pub(crate) fn simple_benchmark<U, F: FnOnce() -> U>(f: F) {
122  let start = std::time::Instant::now();
123  f();
124  std::eprintln!("Time taken: {:?}", start.elapsed());
125}