use fluent_bundle::FluentValue;
#[derive(Clone, PartialEq, Debug)]
pub struct Message {
pub id: &'static str,
pub args: Vec<(&'static str, Arg)>,
}
impl Message {
pub fn new(id: &'static str) -> Message {
Message {
id,
args: Vec::new(),
}
}
pub fn arg(mut self, name: &'static str, value: impl Into<Arg>) -> Message {
self.args.push((name, value.into()));
self
}
pub fn text(&self, ctx: &egui::Context) -> String {
super::tr_args(ctx, self.id, &self.args)
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum Arg {
Str(String),
Num(f64),
}
impl Arg {
pub(super) fn to_fluent(&self) -> FluentValue<'_> {
match self {
Arg::Str(s) => FluentValue::from(s.as_str()),
Arg::Num(n) => FluentValue::from(*n),
}
}
}
impl From<String> for Arg {
fn from(s: String) -> Arg {
Arg::Str(s)
}
}
impl From<&str> for Arg {
fn from(s: &str) -> Arg {
Arg::Str(s.to_owned())
}
}
impl From<f64> for Arg {
fn from(n: f64) -> Arg {
Arg::Num(n)
}
}
macro_rules! from_number {
($($t:ty),*) => {$(
impl From<$t> for Arg {
#[allow(clippy::cast_precision_loss)]
fn from(n: $t) -> Arg {
Arg::Num(n as f64)
}
}
)*};
}
from_number!(i32, i64, u32, u64, usize);