pub enum Value<'a> {
    None,
    Unit,
    Bool(bool),
    Integer(Integer),
    Float(Float),
    Bytes(Cow<'a, [u8]>),
    String(Cow<'a, str>),
    Sequence(Vec<Self>),
    Mappings(Vec<(Self, Self)>),
}
Expand description

A Pot encoded value. This type can be used to deserialize to and from Pot without knowing the original data structure.

Variants

None

A value representing None.

Unit

A value representing a Unit (()).

Bool(bool)

A boolean value

Integer(Integer)

An integer value.

Float(Float)

A floating point value.

Bytes(Cow<'a, [u8]>)

A value containing arbitrary bytes.

String(Cow<'a, str>)

A string value.

Sequence(Vec<Self>)

A sequence of values.

Mappings(Vec<(Self, Self)>)

A sequence of key-value mappings.

Implementations

Returns a new value from an interator of items that can be converted into a value.

let mappings = Value::from_sequence(Vec::<String>::new());
assert!(matches!(mappings, Value::Sequence(_)));

Returns a new value from an interator of 2-element tuples representing key-value pairs.

let mappings = Value::from_mappings(HashMap::<String, u32>::new());
assert!(matches!(mappings, Value::Mappings(_)));

Returns true if the value contained is considered empty.

// Value::None is always empty.
assert_eq!(Value::None.is_empty(), true);

// All primitive values, including Unit, are always not empty, even if they contain the value 0.
assert_eq!(Value::Unit.is_empty(), false);
assert_eq!(Value::from(false).is_empty(), false);
assert_eq!(Value::from(0_u8).is_empty(), false);
assert_eq!(Value::from(0_f32).is_empty(), false);

// For all other types, having a length of 0 will result in is_empty returning true.
assert_eq!(Value::from(Vec::<u8>::new()).is_empty(), true);
assert_eq!(Value::from(b"").is_empty(), true);
assert_eq!(Value::from(vec![0_u8]).is_empty(), false);

assert_eq!(Value::from("").is_empty(), true);
assert_eq!(Value::from("hi").is_empty(), false);

assert_eq!(Value::Sequence(Vec::new()).is_empty(), true);
assert_eq!(Value::from(vec![Value::None]).is_empty(), false);

assert_eq!(Value::Mappings(Vec::new()).is_empty(), true);
assert_eq!(Value::from(vec![(Value::None, Value::None)]).is_empty(), false);

Returns the value represented as a value.

// Value::None is always false.
assert_eq!(Value::None.as_bool(), false);

// Value::Unit is always true.
assert_eq!(Value::Unit.as_bool(), true);

// Value::Bool will return the contained value
assert_eq!(Value::from(false).as_bool(), false);
assert_eq!(Value::from(true).as_bool(), true);

// All primitive values return true if the value is non-zero.
assert_eq!(Value::from(0_u8).as_bool(), false);
assert_eq!(Value::from(1_u8).as_bool(), true);
assert_eq!(Value::from(0_f32).as_bool(), false);
assert_eq!(Value::from(1_f32).as_bool(), true);

// For all other types, as_bool() returns the result of `!is_empty()`.
assert_eq!(Value::from(Vec::<u8>::new()).as_bool(), false);
assert_eq!(Value::from(b"").as_bool(), false);
assert_eq!(Value::from(vec![0_u8]).as_bool(), true);

assert_eq!(Value::from("").as_bool(), false);
assert_eq!(Value::from("hi").as_bool(), true);

assert_eq!(Value::Sequence(Vec::new()).as_bool(), false);
assert_eq!(Value::from(vec![Value::None]).as_bool(), true);

assert_eq!(Value::Mappings(Vec::new()).as_bool(), false);
assert_eq!(Value::from(vec![(Value::None, Value::None)]).as_bool(), true);

Returns the value as an Integer. Returns None if the value is not a Self::Float or Self::Integer. Also returns None if the value is a float, but cannot be losslessly converted to an integer.

Returns the value as an Float. Returns None if the value is not a Self::Float or Self::Integer. Also returns None if the value is an integer, but cannot be losslessly converted to a float.

Returns the value as a string, or None if the value is not representable by a string. This will only return a value with variants Self::String and Self::Bytes. Bytes will only be returned if the contained bytes can be safely interpretted as utf-8.

Returns the value’s bytes, or None if the value is not stored as a representation of bytes. This will only return a value with variants Self::String and Self::Bytes.

Returns an interator that iterates over all values contained inside of this value. Returns an empty iterator if not a Self::Sequence or Self::Mappings. If a Self::Mappings, only the value portion of the mapping is returned.

Returns an interator that iterates over all mappings contained inside of this value. Returns an empty iterator if not a Self::Sequence or Self::Mappings. If a Self::Sequence, the key will always be Self::None.

Converts self to a static lifetime by cloning any borrowed data.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.