pub trait FromValue: Sized {
// Required method
fn from_value(v: Value) -> Result<Self, ShellError>;
// Provided method
fn expected_type() -> Type { ... }
}
Expand description
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.
- If
#[nu_value(rename = "...")]
is applied to a field, that name will be used as the key in the record. - If
#[nu_value(rename_all = "...")]
is applied on the container (struct) the key of the field will be case-converted accordingly. - If neither attribute is applied, the field name is used as is.
- If
#[nu_value(default)]
is applied to a field, the field type’sDefault
implementation will be used if the corresponding record field is missing
Supported case conversions include those provided by heck
, such as
“snake_case”, “kebab-case”, “PascalCase”, and others.
Additionally, all values accepted by
#[serde(rename_all = "...")]
are valid here.
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.
Only enums with no fields may derive this trait.
The expected value representation will be the name of the variant as a Value::String
.
- If
#[nu_value(rename = "...")]
is applied to a variant, that name will be used. - If
#[nu_value(rename_all = "...")]
is applied on the enum container, the name of variant will be case-converted accordingly. - If neither attribute is applied, the variant name will default to “snake_case”.
Additionally, you can use #[nu_value(type_name = "...")]
in the derive macro to set a custom type name
for FromValue::expected_type
. This will result in a Type::Custom
with the specified type name.
This can be useful in situations where the default type name is not desired.
§Enum Example
#[derive(FromValue, Debug, PartialEq)]
#[nu_value(rename_all = "COBOL-CASE", type_name = "birb")]
enum Bird {
MountainEagle,
ForestOwl,
#[nu_value(rename = "RIVER-QUACK")]
RiverDuck,
}
assert_eq!(
Bird::from_value(Value::string("FOREST-OWL", span)).unwrap(),
Bird::ForestOwl
);
assert_eq!(
Bird::from_value(Value::string("RIVER-QUACK", span)).unwrap(),
Bird::RiverDuck
);
assert_eq!(
&Bird::expected_type().to_string(),
"birb"
);
§Struct Example
#[derive(FromValue, PartialEq, Eq, Debug)]
#[nu_value(rename_all = "kebab-case")]
struct Person {
first_name: String,
last_name: String,
#[nu_value(rename = "age")]
age_years: u32,
}
let value = Value::record(record! {
"first-name" => Value::string("John", span),
"last-name" => Value::string("Doe", span),
"age" => Value::int(42, span),
}, span);
assert_eq!(
Person::from_value(value).unwrap(),
Person {
first_name: "John".into(),
last_name: "Doe".into(),
age_years: 42,
}
);
Required Methods§
Sourcefn from_value(v: Value) -> Result<Self, ShellError>
fn from_value(v: Value) -> Result<Self, ShellError>
Provided Methods§
Sourcefn expected_type() -> Type
fn expected_type() -> Type
Expected Value
type.
This is used to print out errors of what type of value is expected for conversion.
Even if not used in from_value
this should still be implemented
so that other implementations like Option
or Vec
can make use of it.
It is advised to call this method in from_value
to ensure that expected type in the error
is consistent.
Unlike the default implementation, derived implementations explicitly reveal the concrete
type, such as Type::Record
or Type::List
, instead of an opaque type.
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 FromValue for bool
impl FromValue for bool
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for char
impl FromValue for char
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for f32
impl FromValue for f32
fn from_value(v: Value) -> Result<Self, ShellError>
Source§impl FromValue for f64
impl FromValue for f64
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for i8
impl FromValue for i8
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for i16
impl FromValue for i16
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for i32
impl FromValue for i32
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for i64
impl FromValue for i64
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for isize
impl FromValue for isize
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for u16
impl FromValue for u16
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for u32
impl FromValue for u32
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for u64
impl FromValue for u64
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for ()
impl FromValue for ()
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for usize
impl FromValue for usize
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for String
impl FromValue for String
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for Vec<u8>
impl FromValue for Vec<u8>
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for Duration
This implementation supports positive durations only.
impl FromValue for Duration
This implementation supports positive durations only.
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for PathBuf
impl FromValue for PathBuf
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for Bytes
impl FromValue for Bytes
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for DateTime<FixedOffset>
impl FromValue for DateTime<FixedOffset>
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroI8
impl FromValue for NonZeroI8
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroI16
impl FromValue for NonZeroI16
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroI32
impl FromValue for NonZeroI32
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroI64
impl FromValue for NonZeroI64
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroIsize
impl FromValue for NonZeroIsize
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroU16
impl FromValue for NonZeroU16
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroU32
impl FromValue for NonZeroU32
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroU64
impl FromValue for NonZeroU64
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl FromValue for NonZeroUsize
impl FromValue for NonZeroUsize
fn from_value(v: Value) -> Result<Self, ShellError>
fn expected_type() -> Type
Source§impl<B> FromValue for Cow<'_, B>
This blanket implementation permits the use of Cow<'_, B>
(Cow<'_, str>
etc) based on
the FromValue implementation of B
’s owned form (str => String).
impl<B> FromValue for Cow<'_, B>
This blanket implementation permits the use of Cow<'_, B>
(Cow<'_, str>
etc) based on
the FromValue implementation of B
’s owned form (str => String).