local_fmt/
lib.rs

1#![doc = include_str!(concat!("../", std::env!("CARGO_PKG_README")))]
2#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
3#![allow(clippy::macro_metavars_in_unsafe)]
4
5extern crate self as local_fmt;
6
7use enum_table::{EnumTable, Enumable};
8
9pub mod utils;
10pub use utils::*;
11
12pub mod message;
13pub use message::*;
14
15#[cfg(feature = "serde")]
16mod serde;
17
18#[cfg(feature = "macros")]
19#[doc(inline)]
20pub use local_fmt_macros::{def_local_fmt, gen_alloc_message, gen_static_message};
21#[cfg(feature = "macros")]
22pub mod macros;
23
24/// A struct that holds a message and the language it is in.
25pub struct LocalFmt<L: Enumable + Copy, M, const N: usize> {
26    messages: EnumTable<L, M, N>,
27    lang: fn() -> L,
28}
29
30impl<L: Enumable + Copy, M, const N: usize> LocalFmt<L, M, N> {
31    pub const fn new(messages: EnumTable<L, M, N>, lang: fn() -> L) -> Self {
32        Self { messages, lang }
33    }
34
35    /// Returns the message in the current language.
36    pub fn get_message(&self) -> &M {
37        self.messages.get(&(self.lang)())
38    }
39
40    /// Returns the language.
41    pub fn lang(&self) -> L {
42        (self.lang)()
43    }
44}
45
46impl<L: Enumable + Copy, M, const N: usize> std::ops::Deref for LocalFmt<L, M, N> {
47    type Target = M;
48
49    /// Returns the message in the current language.
50    fn deref(&self) -> &Self::Target {
51        self.get_message()
52    }
53}