use std::f32;
use std::num::ParseFloatError;
use std::str::FromStr;
use intl_pluralrules::PluralCategory;
use super::context::MessageContext;
#[derive(Clone, Debug, PartialEq)]
pub enum FluentValue {
String(String),
Number(String),
}
impl FluentValue {
pub fn as_number<S: ToString>(v: S) -> Result<Self, ParseFloatError> {
f64::from_str(&v.to_string()).map(|_| FluentValue::Number(v.to_string()))
}
pub fn format(&self, _ctx: &MessageContext) -> String {
match self {
FluentValue::String(s) => s.clone(),
FluentValue::Number(n) => n.clone(),
}
}
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,
(&FluentValue::String(ref a), &FluentValue::Number(ref b)) => {
let cat = match a.as_str() {
"zero" => PluralCategory::ZERO,
"one" => PluralCategory::ONE,
"two" => PluralCategory::TWO,
"few" => PluralCategory::FEW,
"many" => PluralCategory::MANY,
"other" => PluralCategory::OTHER,
_ => return false,
};
let pr = &ctx.plural_rules;
pr.select(&b) == Ok(cat)
}
(&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.to_string())
}
}
impl From<i8> for FluentValue {
fn from(n: i8) -> Self {
FluentValue::Number(n.to_string())
}
}