added docs about derive for macros

This commit is contained in:
Tim 'Piepmatz' Hesse 2024-06-03 00:37:38 +02:00
parent cc8c620de8
commit 9c6226184e
2 changed files with 22 additions and 0 deletions

View File

@ -13,6 +13,17 @@ use std::{
}; };
/// A trait for loading a value from a [`Value`]. /// 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 { pub trait FromValue: Sized {
// TODO: instead of ShellError, maybe we could have a FromValueError that implements Into<ShellError> // TODO: instead of ShellError, maybe we could have a FromValueError that implements Into<ShellError>
/// Loads a value from a [`Value`]. /// Loads a value from a [`Value`].

View File

@ -5,6 +5,17 @@ use crate::{Record, ShellError, Span, Value};
/// A trait for converting a value into a [`Value`]. /// A trait for converting a value into a [`Value`].
/// ///
/// This conversion is infallible, for fallible conversions use [`TryIntoValue`]. /// 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 { pub trait IntoValue: Sized {
/// Converts the given value to a [`Value`]. /// Converts the given value to a [`Value`].
fn into_value(self, span: Span) -> Value; fn into_value(self, span: Span) -> Value;