Enum fluent_bundle::FluentValue
source · pub enum FluentValue<'source> {
String(Cow<'source, str>),
Number(FluentNumber),
Custom(Box<dyn FluentType + Send>),
None,
Error,
}
Expand description
The FluentValue
enum represents values which can be formatted to a String.
Those values are either passed as arguments to FluentBundle::format_pattern
or
produced by functions, or generated in the process of pattern resolution.
Variants§
Implementations§
source§impl<'source> FluentValue<'source>
impl<'source> FluentValue<'source>
sourcepub fn try_number(value: &'source str) -> Self
pub fn try_number(value: &'source str) -> Self
Attempts to parse the string representation of a value
that supports
ToString
into a FluentValue::Number
. If it fails, it will instead
convert it to a FluentValue::String
.
use fluent_bundle::types::{FluentNumber, FluentNumberOptions, FluentValue};
// "2" parses into a `FluentNumber`
assert_eq!(
FluentValue::try_number("2"),
FluentValue::Number(FluentNumber::new(2.0, FluentNumberOptions::default()))
);
// Floats can be parsed as well.
assert_eq!(
FluentValue::try_number("3.141569"),
FluentValue::Number(FluentNumber::new(
3.141569,
FluentNumberOptions {
minimum_fraction_digits: Some(6),
..Default::default()
}
))
);
// When a value is not a valid number, it falls back to a `FluentValue::String`
assert_eq!(
FluentValue::try_number("A string"),
FluentValue::String("A string".into())
);
sourcepub fn matches<R: Borrow<FluentResource>, M>(
&self,
other: &FluentValue<'_>,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> boolwhere
M: MemoizerKind,
pub fn matches<R: Borrow<FluentResource>, M>(
&self,
other: &FluentValue<'_>,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> boolwhere
M: MemoizerKind,
Checks to see if two FluentValues
match each other by having the
same type and contents. The special exception is in the case of a string being
compared to a number. Here attempt to check that the plural rule category matches.
use fluent_bundle::resolver::Scope;
use fluent_bundle::{types::FluentValue, FluentBundle, FluentResource};
use unic_langid::langid;
let langid_ars = langid!("en");
let bundle: FluentBundle<FluentResource> = FluentBundle::new(vec![langid_ars]);
let scope = Scope::new(&bundle, None, None);
// Matching examples:
assert!(FluentValue::try_number("2").matches(&FluentValue::try_number("2"), &scope));
assert!(FluentValue::from("fluent").matches(&FluentValue::from("fluent"), &scope));
assert!(
FluentValue::from("one").matches(&FluentValue::try_number("1"), &scope),
"Plural rules are matched."
);
// Non-matching examples:
assert!(!FluentValue::try_number("2").matches(&FluentValue::try_number("3"), &scope));
assert!(!FluentValue::from("fluent").matches(&FluentValue::from("not fluent"), &scope));
assert!(!FluentValue::from("two").matches(&FluentValue::try_number("100"), &scope),);
sourcepub fn write<W, R, M>(
&self,
w: &mut W,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> Result
pub fn write<W, R, M>( &self, w: &mut W, scope: &Scope<'_, '_, '_, '_, R, M> ) -> Result
Write out a string version of the FluentValue
to W
.
sourcepub fn as_string<R: Borrow<FluentResource>, M>(
&self,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> Cow<'source, str>where
M: MemoizerKind,
pub fn as_string<R: Borrow<FluentResource>, M>(
&self,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> Cow<'source, str>where
M: MemoizerKind,
Converts the FluentValue
to a string.
Clones inner values when owned, borrowed data is not cloned.
Prefer using FluentValue::into_string()
when possible.
sourcepub fn into_string<R: Borrow<FluentResource>, M>(
self,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> Cow<'source, str>where
M: MemoizerKind,
pub fn into_string<R: Borrow<FluentResource>, M>(
self,
scope: &Scope<'_, '_, '_, '_, R, M>
) -> Cow<'source, str>where
M: MemoizerKind,
Converts the FluentValue
to a string.
Takes self by-value to be able to skip expensive clones.
Prefer this method over FluentValue::as_string()
when possible.