rustc_error_messages

Enum 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) -> FluentValue<'source>

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, M>( &self, other: &FluentValue<'_>, scope: &Scope<'_, '_, '_, '_, R, M>, ) -> bool

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<(), Error>

Write out a string version of the FluentValue to W.

Source

pub fn as_string<R, M>( &self, scope: &Scope<'_, '_, '_, '_, R, M>, ) -> Cow<'source, str>

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, M>( self, scope: &Scope<'_, '_, '_, '_, R, M>, ) -> Cow<'source, str>

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) -> FluentValue<'s>

Returns a copy of the value. Read more
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<(), Error>

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

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &f32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &f64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &i128) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &i16) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &i32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &i64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &i8) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &isize) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &u128) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &u16) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &u32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &u64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &u8) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn from(n: &usize) -> FluentValue<'_>

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>) -> FluentValue<'source>

Converts to this type from the input type.
Source§

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

Source§

fn from(input: FluentNumber) -> FluentValue<'l>

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>) -> FluentValue<'source>

Converts to this type from the input type.
Source§

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

Source§

fn from(s: String) -> FluentValue<'source>

Converts to this type from the input type.
Source§

impl From<f32> for FluentValue<'_>

Source§

fn from(n: f32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<f64> for FluentValue<'_>

Source§

fn from(n: f64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<i128> for FluentValue<'_>

Source§

fn from(n: i128) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<i16> for FluentValue<'_>

Source§

fn from(n: i16) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<i32> for FluentValue<'_>

Source§

fn from(n: i32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<i64> for FluentValue<'_>

Source§

fn from(n: i64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<i8> for FluentValue<'_>

Source§

fn from(n: i8) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<isize> for FluentValue<'_>

Source§

fn from(n: isize) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<u128> for FluentValue<'_>

Source§

fn from(n: u128) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<u16> for FluentValue<'_>

Source§

fn from(n: u16) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<u32> for FluentValue<'_>

Source§

fn from(n: u32) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<u64> for FluentValue<'_>

Source§

fn from(n: u64) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<u8> for FluentValue<'_>

Source§

fn from(n: u8) -> FluentValue<'_>

Converts to this type from the input type.
Source§

impl From<usize> for FluentValue<'_>

Source§

fn from(n: usize) -> FluentValue<'_>

Converts to this type from the input type.
Source§

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

Source§

fn eq(&self, other: &FluentValue<'s>) -> bool

Tests for self and other values to be equal, and is used by ==.
Source§

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

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

Auto Trait Implementations§

§

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

§

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

§

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> Aligned for T

Source§

const ALIGN: Alignment = _

Alignment of Self.
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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> Filterable for T

Source§

fn filterable( self, filter_name: &'static str, ) -> RequestFilterDataProvider<T, fn(_: DataRequest<'_>) -> bool>

Creates a filterable data provider with the given name for debugging. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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>,

Source§

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>,

Source§

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<'a, T> Captures<'a> for T
where T: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 120 bytes

Size for each variant:

  • String: 32 bytes
  • Number: 120 bytes
  • Custom: 24 bytes
  • None: 0 bytes
  • Error: 0 bytes