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.

Object Safety§

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§