nu_protocol

Trait IntoValue

Source
pub trait IntoValue: Sized {
    // Required method
    fn into_value(self, span: Span) -> Value;
}
Expand description

A trait for converting a value into a Value.

This conversion is infallible, for fallible conversions use TryIntoValue.

§Derivable

This trait can be used with #[derive]. When derived on structs with named fields, the resulting value representation will use Value::Record, where each field of the record corresponds to a field of the struct.

By default, field names will be used as-is unless specified otherwise:

  • If #[nu_value(rename = "...")] is applied to a specific field, that name is used.
  • If #[nu_value(rename_all = "...")] is applied to the struct, field names will be case-converted accordingly.
  • If neither attribute is used, the original field name will be retained.

For structs with unnamed fields, the value representation will be Value::List, with all fields inserted into a list. Unit structs will be represented as Value::Nothing since they contain no data.

For enums, the resulting value representation depends on the variant name:

  • If #[nu_value(rename = "...")] is applied to a specific variant, that name is used.
  • If #[nu_value(rename_all = "...")] is applied to the enum, variant names will be case-converted accordingly.
  • If neither attribute is used, variant names will default to snake_case.

Only enums with no fields may derive this trait. The resulting value will be the name of the variant as a Value::String.

All case options from heck are supported, as well as the values allowed by #[serde(rename_all)].

§Enum Example

#[derive(IntoValue)]
#[nu_value(rename_all = "COBOL-CASE")]
enum Bird {
    MountainEagle,
    ForestOwl,
    #[nu_value(rename = "RIVER-QUACK")]
    RiverDuck,
}

assert_eq!(
    Bird::ForestOwl.into_value(span),
    Value::string("FOREST-OWL", span)
);

assert_eq!(
    Bird::RiverDuck.into_value(span),
    Value::string("RIVER-QUACK", span)
);

§Struct Example

#[derive(IntoValue)]
#[nu_value(rename_all = "kebab-case")]
struct Person {
    first_name: String,
    last_name: String,
    #[nu_value(rename = "age")]
    age_years: u32,
}

assert_eq!(
    Person {
        first_name: "John".into(),
        last_name: "Doe".into(),
        age_years: 42,
    }.into_value(span),
    Value::record(record! {
        "first-name" => Value::string("John", span),
        "last-name" => Value::string("Doe", span),
        "age" => Value::int(42, span),
    }, span)
);

Required Methods§

Source

fn into_value(self, span: Span) -> Value

Converts the given value to a Value.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl IntoValue for &str

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for bool

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for char

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for f32

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for f64

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for i8

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for i16

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for i32

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for i64

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for isize

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for u8

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for u16

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for u32

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for ()

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for String

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for Bytes

Source§

fn into_value(self, span: Span) -> Value

Source§

impl IntoValue for DateTime<FixedOffset>

Source§

fn into_value(self, span: Span) -> Value

Source§

impl<K, V> IntoValue for HashMap<K, V>
where K: Borrow<str> + Into<String>, V: IntoValue,

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Source§

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

Source§

fn into_value(self, span: Span) -> Value

Implementors§