corundum_framework_gui/i18n/
message.rs1use fluent_bundle::FluentValue;
4
5#[derive(Clone, PartialEq, Debug)]
8pub struct Message {
9 pub id: &'static str,
11 pub args: Vec<(&'static str, Arg)>,
13}
14
15impl Message {
16 pub fn new(id: &'static str) -> Message {
18 Message {
19 id,
20 args: Vec::new(),
21 }
22 }
23
24 pub fn arg(mut self, name: &'static str, value: impl Into<Arg>) -> Message {
26 self.args.push((name, value.into()));
27 self
28 }
29
30 pub fn text(&self, ctx: &egui::Context) -> String {
32 super::tr_args(ctx, self.id, &self.args)
33 }
34}
35
36#[derive(Clone, PartialEq, Debug)]
38pub enum Arg {
39 Str(String),
41 Num(f64),
43}
44
45impl Arg {
46 pub(super) fn to_fluent(&self) -> FluentValue<'_> {
47 match self {
48 Arg::Str(s) => FluentValue::from(s.as_str()),
49 Arg::Num(n) => FluentValue::from(*n),
50 }
51 }
52}
53
54impl From<String> for Arg {
55 fn from(s: String) -> Arg {
56 Arg::Str(s)
57 }
58}
59
60impl From<&str> for Arg {
61 fn from(s: &str) -> Arg {
62 Arg::Str(s.to_owned())
63 }
64}
65
66impl From<f64> for Arg {
67 fn from(n: f64) -> Arg {
68 Arg::Num(n)
69 }
70}
71
72macro_rules! from_number {
74 ($($t:ty),*) => {$(
75 impl From<$t> for Arg {
76 #[allow(clippy::cast_precision_loss)]
77 fn from(n: $t) -> Arg {
78 Arg::Num(n as f64)
79 }
80 }
81 )*};
82}
83
84from_number!(i32, i64, u32, u64, usize);