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§
Sourcefn into_value(self, span: Span) -> Value
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 DateTime<FixedOffset>
impl IntoValue for DateTime<FixedOffset>
fn into_value(self, span: Span) -> Value
Source§impl<B> IntoValue for Cow<'_, B>
This blanket implementation permits the use of Cow<'_, B>
(Cow<'_, str>
etc) based on
the IntoValue implementation of B
’s owned form (str => String).
impl<B> IntoValue for Cow<'_, B>
This blanket implementation permits the use of Cow<'_, B>
(Cow<'_, str>
etc) based on
the IntoValue implementation of B
’s owned form (str => String).