Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Bool(bool),
    Int(i64),
    Str(String),
    List(Vec<Value>),
    Record(BTreeMap<String, Value>),
}
Expand description

A value shaped by a Schema. Mirrors lattice_config::ConfigValue.

Record is a BTreeMap for the same reason the host’s is: a value written in TOML (unordered) and the same value built from a struct must compare equal, or “the same config” is two values.

§Examples

use lattice_plugin_sdk::shape::Value;

let v = Value::record([
    ("width".to_string(), Value::Int(80)),
    ("wrap".to_string(), Value::Bool(true)),
]);
assert_eq!(v.field("width").and_then(Value::as_int), Some(80));
// Accessors are kind-checked: the wrong kind is `None`, not a coercion.
assert_eq!(v.field("wrap").and_then(Value::as_int), None);
assert_eq!(v.kind_label(), "record");

Variants§

§

Bool(bool)

A boolean leaf.

§

Int(i64)

An integer leaf.

§

Str(String)

A string leaf — also how a Schema::Enum member is carried.

§

List(Vec<Value>)

An ordered list of values.

§

Record(BTreeMap<String, Value>)

Named fields, key-ordered (see the type docs for why). An optional field that is absent has no entry at all.

Implementations§

Source§

impl Value

Source

pub fn record(fields: impl IntoIterator<Item = (String, Value)>) -> Self

Build a Value::Record from (key, value) pairs. A repeated key keeps the last value (it is collected into a BTreeMap).

Source

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

The human name of this value’s kind — "boolean", "integer", "string", "list" or "record" — as used in ShapeError messages (expected integer, got string).

Source

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

The boolean, if this is a Value::Bool; None for any other kind.

Source

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

The integer, if this is a Value::Int; None for any other kind.

Source

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

The string, if this is a Value::Str; None for any other kind.

Source

pub fn as_list(&self) -> Option<&[Value]>

The elements, if this is a Value::List; None for any other kind.

Source

pub fn field(&self, name: &str) -> Option<&Value>

The field named name (its wire, kebab-case spelling), if this is a Value::Record that has it. None both for a missing field and for a value that is not a record at all.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

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 Value

Source§

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

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

impl Eq for Value

Source§

impl PartialEq for Value

Source§

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

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

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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