json_gettext/key_string/
mod.rs

1mod json_get_text_builder;
2mod json_gettext;
3
4#[cfg(feature = "rocket")]
5mod rocket_feature;
6
7use std::{
8    borrow::Borrow,
9    collections::HashMap,
10    fmt::{self, Display, Formatter},
11    ops::Deref,
12};
13
14pub use json_get_text_builder::*;
15#[cfg(feature = "rocket")]
16pub use rocket_feature::*;
17
18pub use self::json_gettext::*;
19use crate::JSONGetTextValue;
20
21#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
22pub struct Key(pub String);
23
24impl Display for Key {
25    #[inline]
26    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
27        f.write_str(self.0.as_str())
28    }
29}
30
31impl PartialEq<String> for Key {
32    #[inline]
33    fn eq(&self, other: &String) -> bool {
34        self.0.eq(other)
35    }
36}
37
38impl PartialEq<Key> for String {
39    #[inline]
40    fn eq(&self, other: &Key) -> bool {
41        self.eq(&other.0)
42    }
43}
44
45impl From<String> for Key {
46    #[inline]
47    fn from(s: String) -> Self {
48        Key(s)
49    }
50}
51
52impl Borrow<str> for Key {
53    #[inline]
54    fn borrow(&self) -> &str {
55        self.0.as_str()
56    }
57}
58
59impl Borrow<String> for Key {
60    #[inline]
61    fn borrow(&self) -> &String {
62        &self.0
63    }
64}
65
66impl Deref for Key {
67    type Target = String;
68
69    #[inline]
70    fn deref(&self) -> &Self::Target {
71        &self.0
72    }
73}
74
75pub type Context<'a> = HashMap<Key, HashMap<String, JSONGetTextValue<'a>>>;
76
77/**
78Create a literal key.
79
80```rust
81#[macro_use] extern crate json_gettext;
82
83let key = key!("en_US");
84
85assert_eq!(String::from("en_US"), key);
86```
87*/
88#[macro_export]
89macro_rules! key {
90    ($key:expr) => {
91        format!($key)
92    };
93}