json_gettext/key_copy/keys/
locale.rs1use std::{
2 fmt::{self, Display, Formatter, Write},
3 str::FromStr,
4};
5
6use crate::unic_langid::{
7 LanguageIdentifier, LanguageIdentifierError,
8 subtags::{Language, Region},
9};
10
11#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
12pub struct Key(pub Language, pub Option<Region>);
13
14impl Display for Key {
15 #[inline]
16 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
17 f.write_str(self.0.as_str())?;
18
19 if let Some(region) = self.1 {
20 f.write_char('_')?;
21 f.write_str(region.as_str())?;
22 }
23
24 Ok(())
25 }
26}
27
28impl FromStr for Key {
29 type Err = LanguageIdentifierError;
30
31 #[inline]
32 fn from_str(s: &str) -> Result<Self, Self::Err> {
33 let langid = LanguageIdentifier::from_str(s)?;
34
35 let language = langid.language;
36 let region = langid.region;
37
38 Ok(Key(language, region))
39 }
40}
41
42#[macro_export]
58macro_rules! key {
59 ($key:expr) => {{
60 let langid = $crate::unic_langid::langid!($key);
61
62 $crate::Key(langid.language, langid.region)
63 }};
64}