Skip to main content

FactValue

Enum FactValue 

Source
pub enum FactValue {
    String(String),
    Integer(i64),
    Float(f64),
    Boolean(bool),
    Time(DateTime<FixedOffset>),
    Object(HashMap<String, FactValue>),
    Array(Vec<FactValue>),
    Null,
}
Expand description

A value rules can read and write. Integers and floats are distinct, as in Grule: integer literals and arithmetic stay integers, and / always produces a float.

With serde, a FactValue uses the plain representation of its contents: strings, numbers, booleans, null, arrays and maps, with times as RFC 3339 strings. When deserializing, integers that fit in an i64 become Integer and other numbers Float; strings always stay strings.

Variants§

§

String(String)

A string.

§

Integer(i64)

An integer.

§

Float(f64)

A floating-point number.

§

Boolean(bool)

true or false.

§

Time(DateTime<FixedOffset>)

A point in time, with its UTC offset.

§

Object(HashMap<String, FactValue>)

Named fields, like a struct or map.

§

Array(Vec<FactValue>)

A list of values.

§

Null

nil: no value.

Implementations§

Source§

impl FactValue

Source

pub fn as_number(&self) -> Option<f64>

The value as a float, for either kind of number.

Source

pub fn as_integer(&self) -> Option<i64>

The value if it is an integer.

Source

pub fn as_string(&self) -> Option<&str>

The value if it is a string.

Source

pub fn as_boolean(&self) -> Option<bool>

The value if it is a boolean.

Source

pub fn as_time(&self) -> Option<&DateTime<FixedOffset>>

The value if it is a time.

Source

pub fn as_object(&self) -> Option<&HashMap<String, FactValue>>

The fields if the value is an object.

Source

pub fn as_array(&self) -> Option<&[FactValue]>

The items if the value is an array.

Source

pub fn type_name(&self) -> &'static str

The value’s type as rules and error messages describe it: "string", "integer", "float", "boolean", "time", "object", "array" or "nil".

Source

pub fn is_truthy(&self) -> bool

Whether the value is “truthy”: not nil, false, zero, or empty. (Rule conditions themselves require booleans, as in Grule.)

Source

pub fn from_serialize<T: Serialize + ?Sized>( value: &T, ) -> Result<FactValue, String>

Converts any serializable value (typically a struct) into a fact value. Structs and maps become objects, sequences become arrays, and integers that fit in an i64 become integers. Time types serialize as strings.

Source

pub fn deserialize<T: DeserializeOwned>(&self) -> Result<T, String>

Converts this value back into a deserializable type. Times become RFC 3339 strings, and non-finite floats become null.

Trait Implementations§

Source§

impl Clone for FactValue

Source§

fn clone(&self) -> FactValue

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for FactValue

Source§

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

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

impl<'de> Deserialize<'de> for FactValue

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<&str> for FactValue

Source§

fn from(value: &str) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime<FixedOffset>> for FactValue

Source§

fn from(value: DateTime<FixedOffset>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for FactValue

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for FactValue

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for FactValue

Source§

fn from(value: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for FactValue

Source§

fn from(value: i64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for FactValue

Numbers compare by value across integer and float (Integer(2) == Float(2.0)), matching how == behaves in rules. Times compare by the instant they denote.

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for FactValue

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> 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, dest: *mut u8)

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

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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,

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.