Trait nu_protocol::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.
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.
Only enums with no fields may derive this trait.
The resulting value representation will be the name of the variant as a Value::String
.
By default, variant names will be converted to “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(IntoValue)]
#[nu_value(rename_all = "COBOL-CASE")]
enum Bird {
MountainEagle,
ForestOwl,
RiverDuck,
}
assert_eq!(
Bird::RiverDuck.into_value(Span::unknown()),
Value::test_string("RIVER-DUCK")
);
Required Methods§
sourcefn into_value(self, span: Span) -> Value
fn into_value(self, span: Span) -> Value
Converts the given value to a Value
.