Trait nu_protocol::FromValue

source ·
pub trait FromValue: Sized {
    // Required method
    fn from_value(v: Value) -> Result<Self, ShellError>;

    // Provided method
    fn expected_type() -> Type { ... }
}
Expand description

A trait for loading a value from a Value.

§Derivable

This trait can be used with #[derive]. When derived on structs with named fields, it expects a Value::Record where each field of the struct maps to a corresponding field in the record. For structs with unnamed fields, it expects a Value::List, and the fields are populated in the order they appear in the list. Unit structs expect a Value::Nothing, as they contain no data. Attempting to convert from a non-matching Value type will result in an error.

Only enums with no fields may derive this trait. The expected value representation will be the name of the variant as a Value::String. By default, variant names will be expected in “snake_case”. You can customize the case conversion using #[nu_value(rename_all = "kebab-case")] on the enum. All deterministic and useful case conversions provided by convert_case::Case are supported by specifying the case name followed by “case”. Also all values for #[serde(rename_all = "...")] are valid here.

#[derive(FromValue, Debug, PartialEq)]
#[nu_value(rename_all = "COBOL-CASE")]
enum Bird {
    MountainEagle,
    ForestOwl,
    RiverDuck,
}

assert_eq!(
    Bird::from_value(Value::test_string("RIVER-DUCK")).unwrap(),
    Bird::RiverDuck
);

Required Methods§

source

fn from_value(v: Value) -> Result<Self, ShellError>

Loads a value from a Value.

This method retrieves a value similarly to how strings are parsed using FromStr. The operation might fail if the Value contains unexpected types or structures.

Provided Methods§

source

fn expected_type() -> Type

Expected Value type.

This is used to print out errors of what type of value is expected for conversion. Even if not used in from_value this should still be implemented so that other implementations like Option or Vec can make use of it. It is advised to call this method in from_value to ensure that expected type in the error is consistent.

Unlike the default implementation, derived implementations explicitly reveal the concrete type, such as Type::Record or Type::List, instead of an opaque type.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl FromValue for bool

source§

impl FromValue for char

source§

impl FromValue for f32

source§

impl FromValue for f64

source§

impl FromValue for i8

source§

impl FromValue for i16

source§

impl FromValue for i32

source§

impl FromValue for i64

source§

impl FromValue for isize

source§

impl FromValue for u16

source§

impl FromValue for u32

source§

impl FromValue for u64

source§

impl FromValue for ()

source§

impl FromValue for usize

source§

impl FromValue for String

source§

impl FromValue for Vec<u8>

source§

impl FromValue for PathBuf

source§

impl FromValue for DateTime<FixedOffset>

source§

impl<T0> FromValue for (T0,)
where T0: FromValue,

source§

impl<T0, T1> FromValue for (T0, T1)
where T0: FromValue, T1: FromValue,

source§

impl<T0, T1, T2> FromValue for (T0, T1, T2)
where T0: FromValue, T1: FromValue, T2: FromValue,

source§

impl<T0, T1, T2, T3> FromValue for (T0, T1, T2, T3)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue,

source§

impl<T0, T1, T2, T3, T4> FromValue for (T0, T1, T2, T3, T4)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5> FromValue for (T0, T1, T2, T3, T4, T5)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6> FromValue for (T0, T1, T2, T3, T4, T5, T6)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7> FromValue for (T0, T1, T2, T3, T4, T5, T6, T7)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue, T7: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8> FromValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue, T7: FromValue, T8: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue, T7: FromValue, T8: FromValue, T9: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue, T7: FromValue, T8: FromValue, T9: FromValue, T10: FromValue,

source§

impl<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromValue for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
where T0: FromValue, T1: FromValue, T2: FromValue, T3: FromValue, T4: FromValue, T5: FromValue, T6: FromValue, T7: FromValue, T8: FromValue, T9: FromValue, T10: FromValue, T11: FromValue,

source§

impl<T> FromValue for Option<T>
where T: FromValue,

source§

impl<T> FromValue for Vec<T>
where T: FromValue,

source§

impl<T, const N: usize> FromValue for [T; N]
where T: FromValue,

source§

impl<V> FromValue for HashMap<String, V>
where V: FromValue,

Implementors§