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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
////  Simplified replacement for FluentValue. Derived from https://github.com/projectfluent/fluent-rs/blob/master/fluent-bundle/src/types.rs

use core::fmt::Write;

/// Max number of arg values
type MaxArgValues = heapless::consts::U2;
/// Hash map of arg names to arg values
pub type ArgValues = heapless::FnvIndexMap<&'static str, ArgValue, MaxArgValues>;

type MaxLocalizedString = heapless::consts::U20; //// Max length of localized strings
type String = heapless::String::<MaxLocalizedString>; ////

#[derive(Debug, PartialEq, Clone)]
pub enum ArgValue {
    String(String),
    U32(u32),
    I32(i32),
    Error(String),
    None,
}

impl ArgValue {
    /*
        pub fn into_number(v: String) -> Self {
            let s = v.to_string();
            match f64::from_str(&s) {
                Ok(_) => ArgValue::Number(s.into()),
                Err(_) => ArgValue::String(s.into()),
            }
        }
    */

    /*
        pub fn matches<R: Borrow<FluentResource>>(
            &self,
            other: &ArgValue,
            scope: &Scope<R>,
        ) -> bool {
            match (self, other) {
                (&ArgValue::String(ref a), &ArgValue::String(ref b)) => a == b,
                (&ArgValue::Number(ref a), &ArgValue::Number(ref b)) => a == b,
                (&ArgValue::String(ref a), &ArgValue::Number(ref b)) => {
                    let cat = match a.as_ref() {
                        "zero" => PluralCategory::ZERO,
                        "one" => PluralCategory::ONE,
                        "two" => PluralCategory::TWO,
                        "few" => PluralCategory::FEW,
                        "many" => PluralCategory::MANY,
                        "other" => PluralCategory::OTHER,
                        _ => return false,
                    };
                    let pr = &scope.bundle.plural_rules;
                    pr.select(b.as_ref()) == Ok(cat)
                }
                _ => false,
            }
        }
    */

    pub fn to_string(&self) -> String {
        match self {
            ArgValue::String(s) => s.clone(),
            ArgValue::U32(v) => {
                let mut buffer = String::new();
                write!(&mut buffer, "{}", v)
                    .expect("arg fail");
                buffer
            }
            ArgValue::I32(v) => {
                let mut buffer = String::new();
                write!(&mut buffer, "{}", v)
                    .expect("arg fail");
                buffer
            }
            ArgValue::Error(_s) => "Error".into(),
            ArgValue::None => "???".into(),
        }
    }
}

/*
    impl<'source> fmt::Display for ArgValue<'source> {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                ArgValue::String(s) => f.write_str(s),
                ArgValue::Number(n) => f.write_str(n),
                ArgValue::Error(d) => write!(f, "{{{}}}", d),
                ArgValue::None => f.write_str("???"),
            }
        }
    }
*/

impl From<String> for ArgValue {
    fn from(s: String) -> Self {
        ArgValue::String(s.into())
    }
}

impl From<&str> for ArgValue {
    fn from(s: &str) -> Self {
        ArgValue::String(s.into())
    }
}

impl From<u32> for ArgValue {
    fn from(v: u32) -> Self {
        ArgValue::U32(v)
    }
}

impl From<i32> for ArgValue {
    fn from(v: i32) -> Self {
        ArgValue::I32(v)
    }
}

/*
    macro_rules! from_num {
        ($num:ty) => {
            impl From<$num> for ArgValue {
                fn from(n: $num) -> Self {
                    ArgValue::Number(n.to_string().into())
                }
            }
            impl From<& $num> for ArgValue {
                fn from(n: & $num) -> Self {
                    ArgValue::Number(n.to_string().into())
                }
            }
        };
    }
    from_num!(i8);
    from_num!(i16);
    from_num!(i32);
    from_num!(i64);
    from_num!(i128);
    from_num!(isize);
    from_num!(u8);
    from_num!(u16);
    from_num!(u32);
    from_num!(u64);
    from_num!(u128);
    from_num!(usize);
    from_num!(f32);
    from_num!(f64);
*/