Skip to content

Commit 939c721

Browse files
kazimuthZibi Braniecki
authored andcommitted
Add more convenience From impls for FluentValue (#108)
1 parent 8a94187 commit 939c721

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

fluent-bundle/src/types.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,20 +162,57 @@ impl<'source> From<String> for FluentValue<'source> {
162162
}
163163
}
164164

165+
impl<'source> From<&'source String> for FluentValue<'source> {
166+
fn from(s: &'source String) -> Self {
167+
FluentValue::String((&s[..]).into())
168+
}
169+
}
170+
165171
impl<'source> From<&'source str> for FluentValue<'source> {
166172
fn from(s: &'source str) -> Self {
167173
FluentValue::String(s.into())
168174
}
169175
}
170176

171-
impl<'source> From<f64> for FluentValue<'source> {
172-
fn from(n: f64) -> Self {
173-
FluentValue::Number(n.to_string().into())
174-
}
177+
macro_rules! from_num {
178+
($num:ty) => {
179+
impl<'source> From<$num> for FluentValue<'source> {
180+
fn from(n: $num) -> Self {
181+
FluentValue::Number(n.to_string().into())
182+
}
183+
}
184+
impl<'source> From<&'source $num> for FluentValue<'source> {
185+
fn from(n: &'source $num) -> Self {
186+
FluentValue::Number(n.to_string().into())
187+
}
188+
}
189+
};
175190
}
176-
177-
impl<'source> From<isize> for FluentValue<'source> {
178-
fn from(n: isize) -> Self {
179-
FluentValue::Number(n.to_string().into())
191+
from_num!(i8);
192+
from_num!(i16);
193+
from_num!(i32);
194+
from_num!(i64);
195+
from_num!(i128);
196+
from_num!(isize);
197+
from_num!(u8);
198+
from_num!(u16);
199+
from_num!(u32);
200+
from_num!(u64);
201+
from_num!(u128);
202+
from_num!(usize);
203+
from_num!(f32);
204+
from_num!(f64);
205+
206+
#[cfg(test)]
207+
mod tests {
208+
use super::*;
209+
210+
#[test]
211+
fn value_from_copy_ref() {
212+
let x = 1i16;
213+
let y = &x;
214+
let z: FluentValue = y.into();
215+
assert_eq!(z, FluentValue::Number("1".into()));
180216
}
217+
181218
}

0 commit comments

Comments
 (0)