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§

§

String(Cow<'source, str>)

§

Number(FluentNumber)

§

Custom(Box<dyn FluentType + Send>)

§

None

§

Error

Implementations§

source§

impl<'source> FluentValue<'source>

source

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())
);
source

pub fn matches<R: Borrow<FluentResource>, M>( &self, other: &FluentValue<'_>, scope: &Scope<'_, '_, '_, '_, R, M> ) -> bool
where 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),);
source

pub fn write<W, R, M>( &self, w: &mut W, scope: &Scope<'_, '_, '_, '_, R, M> ) -> Result
where W: Write, R: Borrow<FluentResource>, M: MemoizerKind,

Write out a string version of the FluentValue to W.

source

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.

source

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.

source

pub fn into_owned<'a>(&self) -> FluentValue<'a>

Trait Implementations§

source§

impl<'s> Clone for FluentValue<'s>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'source> Debug for FluentValue<'source>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'source> From<&'source String> for FluentValue<'source>

source§

fn from(s: &'source String) -> Self

Converts to this type from the input type.
source§

impl From<&f32> for FluentValue<'_>

source§

fn from(n: &f32) -> Self

Converts to this type from the input type.
source§

impl From<&f64> for FluentValue<'_>

source§

fn from(n: &f64) -> Self

Converts to this type from the input type.
source§

impl From<&i128> for FluentValue<'_>

source§

fn from(n: &i128) -> Self

Converts to this type from the input type.
source§

impl From<&i16> for FluentValue<'_>

source§

fn from(n: &i16) -> Self

Converts to this type from the input type.
source§

impl From<&i32> for FluentValue<'_>

source§

fn from(n: &i32) -> Self

Converts to this type from the input type.
source§

impl From<&i64> for FluentValue<'_>

source§

fn from(n: &i64) -> Self

Converts to this type from the input type.
source§

impl From<&i8> for FluentValue<'_>

source§

fn from(n: &i8) -> Self

Converts to this type from the input type.
source§

impl From<&isize> for FluentValue<'_>

source§

fn from(n: &isize) -> Self

Converts to this type from the input type.
source§

impl<'source> From<&'source str> for FluentValue<'source>

source§

fn from(s: &'source str) -> Self

Converts to this type from the input type.
source§

impl From<&u128> for FluentValue<'_>

source§

fn from(n: &u128) -> Self

Converts to this type from the input type.
source§

impl From<&u16> for FluentValue<'_>

source§

fn from(n: &u16) -> Self

Converts to this type from the input type.
source§

impl From<&u32> for FluentValue<'_>

source§

fn from(n: &u32) -> Self

Converts to this type from the input type.
source§

impl From<&u64> for FluentValue<'_>

source§

fn from(n: &u64) -> Self

Converts to this type from the input type.
source§

impl From<&u8> for FluentValue<'_>

source§

fn from(n: &u8) -> Self

Converts to this type from the input type.
source§

impl From<&usize> for FluentValue<'_>

source§

fn from(n: &usize) -> Self

Converts to this type from the input type.
source§

impl<'source> From<Cow<'source, str>> for FluentValue<'source>

source§

fn from(s: Cow<'source, str>) -> Self

Converts to this type from the input type.
source§

impl<'l> From<FluentNumber> for FluentValue<'l>

source§

fn from(input: FluentNumber) -> Self

Converts to this type from the input type.
source§

impl<'source, T> From<Option<T>> for FluentValue<'source>
where T: Into<FluentValue<'source>>,

source§

fn from(v: Option<T>) -> Self

Converts to this type from the input type.
source§

impl<'source> From<String> for FluentValue<'source>

source§

fn from(s: String) -> Self

Converts to this type from the input type.
source§

impl From<f32> for FluentValue<'_>

source§

fn from(n: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for FluentValue<'_>

source§

fn from(n: f64) -> Self

Converts to this type from the input type.
source§

impl From<i128> for FluentValue<'_>

source§

fn from(n: i128) -> Self

Converts to this type from the input type.
source§

impl From<i16> for FluentValue<'_>

source§

fn from(n: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for FluentValue<'_>

source§

fn from(n: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for FluentValue<'_>

source§

fn from(n: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for FluentValue<'_>

source§

fn from(n: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for FluentValue<'_>

source§

fn from(n: isize) -> Self

Converts to this type from the input type.
source§

impl From<u128> for FluentValue<'_>

source§

fn from(n: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for FluentValue<'_>

source§

fn from(n: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for FluentValue<'_>

source§

fn from(n: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for FluentValue<'_>

source§

fn from(n: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for FluentValue<'_>

source§

fn from(n: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for FluentValue<'_>

source§

fn from(n: usize) -> Self

Converts to this type from the input type.
source§

impl<'s> PartialEq for FluentValue<'s>

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<'source> Freeze for FluentValue<'source>

§

impl<'source> !RefUnwindSafe for FluentValue<'source>

§

impl<'source> Send for FluentValue<'source>

§

impl<'source> !Sync for FluentValue<'source>

§

impl<'source> Unpin for FluentValue<'source>

§

impl<'source> !UnwindSafe for FluentValue<'source>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> AnyEq for T
where T: Any + PartialEq,

source§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.