1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::f32;
use super::context::MessageContext;
use super::intl::PluralRules;
#[derive(Clone, Debug, PartialEq)]
pub enum FluentValue {
String(String),
Number(f32),
}
impl FluentValue {
pub fn format(&self, _ctx: &MessageContext) -> String {
match *self {
FluentValue::String(ref s) => s.clone(),
FluentValue::Number(ref n) => format!("{}", n),
}
}
pub fn matches(&self, ctx: &MessageContext, other: &FluentValue) -> bool {
match (self, other) {
(&FluentValue::String(ref a), &FluentValue::String(ref b)) => a == b,
(&FluentValue::Number(ref a), &FluentValue::Number(ref b)) => {
(a - b).abs() < f32::EPSILON
}
(&FluentValue::String(ref a), &FluentValue::Number(ref b)) => {
let pr = PluralRules::new(ctx.locales);
pr.select(*b) == a
}
(&FluentValue::Number(..), &FluentValue::String(..)) => false,
}
}
}
impl From<String> for FluentValue {
fn from(s: String) -> Self {
FluentValue::String(s)
}
}
impl<'a> From<&'a str> for FluentValue {
fn from(s: &'a str) -> Self {
FluentValue::String(String::from(s))
}
}
impl From<f32> for FluentValue {
fn from(n: f32) -> Self {
FluentValue::Number(n)
}
}
impl From<i8> for FluentValue {
fn from(n: i8) -> Self {
FluentValue::Number(f32::from(n))
}
}