diff --git a/crates/nu-protocol/src/value/from_value.rs b/crates/nu-protocol/src/value/from_value.rs index 57f8c41ad7..642d032a5f 100644 --- a/crates/nu-protocol/src/value/from_value.rs +++ b/crates/nu-protocol/src/value/from_value.rs @@ -13,6 +13,17 @@ use std::{ }; /// 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. +// TODO: explain derive for enums pub trait FromValue: Sized { // TODO: instead of ShellError, maybe we could have a FromValueError that implements Into /// Loads a value from a [`Value`]. diff --git a/crates/nu-protocol/src/value/into_value.rs b/crates/nu-protocol/src/value/into_value.rs index 03e2675613..5d1c170452 100644 --- a/crates/nu-protocol/src/value/into_value.rs +++ b/crates/nu-protocol/src/value/into_value.rs @@ -5,6 +5,17 @@ use crate::{Record, ShellError, Span, Value}; /// 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. +/// 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. +// TODO: explain derive for enums pub trait IntoValue: Sized { /// Converts the given value to a [`Value`]. fn into_value(self, span: Span) -> Value;