Add derive macros for FromValue and IntoValue to ease the use of Values in Rust code (#13031)

# Description
After discussing with @sholderbach the cumbersome usage of
`nu_protocol::Value` in Rust, I created a derive macro to simplify it.
I’ve added a new crate called `nu-derive-value`, which includes two
macros, `IntoValue` and `FromValue`. These are re-exported in
`nu-protocol` and should be encouraged to be used via that re-export.

The macros ensure that all types can easily convert from and into
`Value`. For example, as a plugin author, you can define your plugin
configuration using a Rust struct and easily convert it using
`FromValue`. This makes plugin configuration less of a hassle.

I introduced the `IntoValue` trait for a standardized approach to
converting values into `Value` (and a fallible variant `TryIntoValue`).
This trait could potentially replace existing `into_value` methods.
Along with this, I've implemented `FromValue` for several standard types
and refined other implementations to use blanket implementations where
applicable.

I made these design choices with input from @devyn.

There are more improvements possible, but this is a solid start and the
PR is already quite substantial.

# User-Facing Changes

For `nu-protocol` users, these changes simplify the handling of
`Value`s. There are no changes for end-users of nushell itself.

# Tests + Formatting
Documenting the macros itself is not really possible, as they cannot
really reference any other types since they are the root of the
dependency graph. The standard library has the same problem
([std::Debug](https://doc.rust-lang.org/stable/std/fmt/derive.Debug.html)).
However I documented the `FromValue` and `IntoValue` traits completely.

For testing, I made of use `proc-macro2` in the derive macro code. This
would allow testing the generated source code. Instead I just tested
that the derived functionality is correct. This is done in
`nu_protocol::value::test_derive`, as a consumer of `nu-derive-value`
needs to do the testing of the macro usage. I think that these tests
should provide a stable baseline so that users can be sure that the impl
works.

# After Submitting
With these macros available, we can probably use them in some examples
for plugins to showcase the use of them.
This commit is contained in:
Piepmatz
2024-06-18 01:05:11 +02:00
committed by GitHub
parent 3a6d8aac0b
commit b79a2255d2
24 changed files with 2378 additions and 385 deletions

View File

@ -557,12 +557,12 @@ pub enum ShellError {
/// ## Resolution
///
/// Check the spelling of your column name. Did you forget to rename a column somewhere?
#[error("Cannot find column")]
#[error("Cannot find column '{col_name}'")]
#[diagnostic(code(nu::shell::column_not_found))]
CantFindColumn {
col_name: String,
#[label = "cannot find column '{col_name}'"]
span: Span,
span: Option<Span>,
#[label = "value originates here"]
src_span: Span,
},

View File

@ -39,3 +39,5 @@ pub use span::*;
pub use syntax_shape::*;
pub use ty::*;
pub use value::*;
pub use nu_derive_value::*;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,196 @@
use std::collections::HashMap;
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.
///
/// 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"](convert_case::Case::Snake).
/// 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 = "...")]`](https://serde.rs/container-attrs.html#rename_all) are valid
/// here.
///
/// ```
/// # use nu_protocol::{IntoValue, Value, Span};
/// #[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")
/// );
/// ```
pub trait IntoValue: Sized {
/// Converts the given value to a [`Value`].
fn into_value(self, span: Span) -> Value;
}
// Primitive Types
impl<T, const N: usize> IntoValue for [T; N]
where
T: IntoValue,
{
fn into_value(self, span: Span) -> Value {
Vec::from(self).into_value(span)
}
}
macro_rules! primitive_into_value {
($type:ty, $method:ident) => {
primitive_into_value!($type => $type, $method);
};
($type:ty => $as_type:ty, $method:ident) => {
impl IntoValue for $type {
fn into_value(self, span: Span) -> Value {
Value::$method(<$as_type>::from(self), span)
}
}
};
}
primitive_into_value!(bool, bool);
primitive_into_value!(char, string);
primitive_into_value!(f32 => f64, float);
primitive_into_value!(f64, float);
primitive_into_value!(i8 => i64, int);
primitive_into_value!(i16 => i64, int);
primitive_into_value!(i32 => i64, int);
primitive_into_value!(i64, int);
primitive_into_value!(u8 => i64, int);
primitive_into_value!(u16 => i64, int);
primitive_into_value!(u32 => i64, int);
// u64 and usize may be truncated as Value only supports i64.
impl IntoValue for isize {
fn into_value(self, span: Span) -> Value {
Value::int(self as i64, span)
}
}
impl IntoValue for () {
fn into_value(self, span: Span) -> Value {
Value::nothing(span)
}
}
macro_rules! tuple_into_value {
($($t:ident:$n:tt),+) => {
impl<$($t),+> IntoValue for ($($t,)+) where $($t: IntoValue,)+ {
fn into_value(self, span: Span) -> Value {
let vals = vec![$(self.$n.into_value(span)),+];
Value::list(vals, span)
}
}
}
}
// Tuples in std are implemented for up to 12 elements, so we do it here too.
tuple_into_value!(T0:0);
tuple_into_value!(T0:0, T1:1);
tuple_into_value!(T0:0, T1:1, T2:2);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9, T10:10);
tuple_into_value!(T0:0, T1:1, T2:2, T3:3, T4:4, T5:5, T6:6, T7:7, T8:8, T9:9, T10:10, T11:11);
// Other std Types
impl IntoValue for String {
fn into_value(self, span: Span) -> Value {
Value::string(self, span)
}
}
impl<T> IntoValue for Vec<T>
where
T: IntoValue,
{
fn into_value(self, span: Span) -> Value {
Value::list(self.into_iter().map(|v| v.into_value(span)).collect(), span)
}
}
impl<T> IntoValue for Option<T>
where
T: IntoValue,
{
fn into_value(self, span: Span) -> Value {
match self {
Some(v) => v.into_value(span),
None => Value::nothing(span),
}
}
}
impl<V> IntoValue for HashMap<String, V>
where
V: IntoValue,
{
fn into_value(self, span: Span) -> Value {
let mut record = Record::new();
for (k, v) in self.into_iter() {
// Using `push` is fine as a hashmaps have unique keys.
// To ensure this uniqueness, we only allow hashmaps with strings as
// keys and not keys which implement `Into<String>` or `ToString`.
record.push(k, v.into_value(span));
}
Value::record(record, span)
}
}
// Nu Types
impl IntoValue for Value {
fn into_value(self, span: Span) -> Value {
self.with_span(span)
}
}
// TODO: use this type for all the `into_value` methods that types implement but return a Result
/// A trait for trying to convert a value into a `Value`.
///
/// Types like streams may fail while collecting the `Value`,
/// for these types it is useful to implement a fallible variant.
///
/// This conversion is fallible, for infallible conversions use [`IntoValue`].
/// All types that implement `IntoValue` will automatically implement this trait.
pub trait TryIntoValue: Sized {
// TODO: instead of ShellError, maybe we could have a IntoValueError that implements Into<ShellError>
/// Tries to convert the given value into a `Value`.
fn try_into_value(self, span: Span) -> Result<Value, ShellError>;
}
impl<T> TryIntoValue for T
where
T: IntoValue,
{
fn try_into_value(self, span: Span) -> Result<Value, ShellError> {
Ok(self.into_value(span))
}
}

View File

@ -4,7 +4,10 @@ mod filesize;
mod from;
mod from_value;
mod glob;
mod into_value;
mod range;
#[cfg(test)]
mod test_derive;
pub mod record;
pub use custom_value::CustomValue;
@ -12,6 +15,7 @@ pub use duration::*;
pub use filesize::*;
pub use from_value::FromValue;
pub use glob::*;
pub use into_value::{IntoValue, TryIntoValue};
pub use range::{FloatRange, IntRange, Range};
pub use record::Record;
@ -1089,7 +1093,7 @@ impl Value {
} else {
return Err(ShellError::CantFindColumn {
col_name: column_name.clone(),
span: *origin_span,
span: Some(*origin_span),
src_span: span,
});
}
@ -1126,7 +1130,7 @@ impl Value {
} else {
Err(ShellError::CantFindColumn {
col_name: column_name.clone(),
span: *origin_span,
span: Some(*origin_span),
src_span: val_span,
})
}
@ -1136,7 +1140,7 @@ impl Value {
}
_ => Err(ShellError::CantFindColumn {
col_name: column_name.clone(),
span: *origin_span,
span: Some(*origin_span),
src_span: val_span,
}),
}
@ -1237,7 +1241,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1262,7 +1266,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1342,7 +1346,7 @@ impl Value {
} else {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1351,7 +1355,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1364,7 +1368,7 @@ impl Value {
} else {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1373,7 +1377,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1427,7 +1431,7 @@ impl Value {
if record.to_mut().remove(col_name).is_none() && !optional {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1435,7 +1439,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1447,7 +1451,7 @@ impl Value {
if record.to_mut().remove(col_name).is_none() && !optional {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1455,7 +1459,7 @@ impl Value {
}
v => Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
}),
},
@ -1504,7 +1508,7 @@ impl Value {
} else if !optional {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1512,7 +1516,7 @@ impl Value {
v => {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
});
}
@ -1526,7 +1530,7 @@ impl Value {
} else if !optional {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v_span,
});
}
@ -1534,7 +1538,7 @@ impl Value {
}
v => Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: *span,
span: Some(*span),
src_span: v.span(),
}),
},

View File

@ -0,0 +1,386 @@
use crate::{record, FromValue, IntoValue, Record, Span, Value};
use std::collections::HashMap;
// Make nu_protocol available in this namespace, consumers of this crate will
// have this without such an export.
// The derive macro fully qualifies paths to "nu_protocol".
use crate as nu_protocol;
trait IntoTestValue {
fn into_test_value(self) -> Value;
}
impl<T> IntoTestValue for T
where
T: IntoValue,
{
fn into_test_value(self) -> Value {
self.into_value(Span::test_data())
}
}
#[derive(IntoValue, FromValue, Debug, PartialEq)]
struct NamedFieldsStruct<T>
where
T: IntoValue + FromValue,
{
array: [u16; 4],
bool: bool,
char: char,
f32: f32,
f64: f64,
i8: i8,
i16: i16,
i32: i32,
i64: i64,
isize: isize,
u16: u16,
u32: u32,
unit: (),
tuple: (u32, bool),
some: Option<u32>,
none: Option<u32>,
vec: Vec<T>,
string: String,
hashmap: HashMap<String, u32>,
nested: Nestee,
}
#[derive(IntoValue, FromValue, Debug, PartialEq)]
struct Nestee {
u32: u32,
some: Option<u32>,
none: Option<u32>,
}
impl NamedFieldsStruct<u32> {
fn make() -> Self {
Self {
array: [1, 2, 3, 4],
bool: true,
char: 'a',
f32: std::f32::consts::PI,
f64: std::f64::consts::E,
i8: 127,
i16: -32768,
i32: 2147483647,
i64: -9223372036854775808,
isize: 2,
u16: 65535,
u32: 4294967295,
unit: (),
tuple: (1, true),
some: Some(123),
none: None,
vec: vec![10, 20, 30],
string: "string".to_string(),
hashmap: HashMap::from_iter([("a".to_string(), 10), ("b".to_string(), 20)]),
nested: Nestee {
u32: 3,
some: Some(42),
none: None,
},
}
}
fn value() -> Value {
Value::test_record(record! {
"array" => Value::test_list(vec![
Value::test_int(1),
Value::test_int(2),
Value::test_int(3),
Value::test_int(4)
]),
"bool" => Value::test_bool(true),
"char" => Value::test_string('a'),
"f32" => Value::test_float(std::f32::consts::PI.into()),
"f64" => Value::test_float(std::f64::consts::E),
"i8" => Value::test_int(127),
"i16" => Value::test_int(-32768),
"i32" => Value::test_int(2147483647),
"i64" => Value::test_int(-9223372036854775808),
"isize" => Value::test_int(2),
"u16" => Value::test_int(65535),
"u32" => Value::test_int(4294967295),
"unit" => Value::test_nothing(),
"tuple" => Value::test_list(vec![
Value::test_int(1),
Value::test_bool(true)
]),
"some" => Value::test_int(123),
"none" => Value::test_nothing(),
"vec" => Value::test_list(vec![
Value::test_int(10),
Value::test_int(20),
Value::test_int(30)
]),
"string" => Value::test_string("string"),
"hashmap" => Value::test_record(record! {
"a" => Value::test_int(10),
"b" => Value::test_int(20)
}),
"nested" => Value::test_record(record! {
"u32" => Value::test_int(3),
"some" => Value::test_int(42),
"none" => Value::test_nothing(),
})
})
}
}
#[test]
fn named_fields_struct_into_value() {
let expected = NamedFieldsStruct::value();
let actual = NamedFieldsStruct::make().into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn named_fields_struct_from_value() {
let expected = NamedFieldsStruct::make();
let actual = NamedFieldsStruct::from_value(NamedFieldsStruct::value()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn named_fields_struct_roundtrip() {
let expected = NamedFieldsStruct::make();
let actual =
NamedFieldsStruct::from_value(NamedFieldsStruct::make().into_test_value()).unwrap();
assert_eq!(expected, actual);
let expected = NamedFieldsStruct::value();
let actual = NamedFieldsStruct::<u32>::from_value(NamedFieldsStruct::value())
.unwrap()
.into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn named_fields_struct_missing_value() {
let value = Value::test_record(Record::new());
let res: Result<NamedFieldsStruct<u32>, _> = NamedFieldsStruct::from_value(value);
assert!(res.is_err());
}
#[test]
fn named_fields_struct_incorrect_type() {
// Should work for every type that is not a record.
let value = Value::test_nothing();
let res: Result<NamedFieldsStruct<u32>, _> = NamedFieldsStruct::from_value(value);
assert!(res.is_err());
}
#[derive(IntoValue, FromValue, Debug, PartialEq)]
struct UnnamedFieldsStruct<T>(u32, String, T)
where
T: IntoValue + FromValue;
impl UnnamedFieldsStruct<f64> {
fn make() -> Self {
UnnamedFieldsStruct(420, "Hello, tuple!".to_string(), 33.33)
}
fn value() -> Value {
Value::test_list(vec![
Value::test_int(420),
Value::test_string("Hello, tuple!"),
Value::test_float(33.33),
])
}
}
#[test]
fn unnamed_fields_struct_into_value() {
let expected = UnnamedFieldsStruct::value();
let actual = UnnamedFieldsStruct::make().into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn unnamed_fields_struct_from_value() {
let expected = UnnamedFieldsStruct::make();
let value = UnnamedFieldsStruct::value();
let actual = UnnamedFieldsStruct::from_value(value).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn unnamed_fields_struct_roundtrip() {
let expected = UnnamedFieldsStruct::make();
let actual =
UnnamedFieldsStruct::from_value(UnnamedFieldsStruct::make().into_test_value()).unwrap();
assert_eq!(expected, actual);
let expected = UnnamedFieldsStruct::value();
let actual = UnnamedFieldsStruct::<f64>::from_value(UnnamedFieldsStruct::value())
.unwrap()
.into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn unnamed_fields_struct_missing_value() {
let value = Value::test_list(vec![]);
let res: Result<UnnamedFieldsStruct<f64>, _> = UnnamedFieldsStruct::from_value(value);
assert!(res.is_err());
}
#[test]
fn unnamed_fields_struct_incorrect_type() {
// Should work for every type that is not a record.
let value = Value::test_nothing();
let res: Result<UnnamedFieldsStruct<f64>, _> = UnnamedFieldsStruct::from_value(value);
assert!(res.is_err());
}
#[derive(IntoValue, FromValue, Debug, PartialEq)]
struct UnitStruct;
#[test]
fn unit_struct_into_value() {
let expected = Value::test_nothing();
let actual = UnitStruct.into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn unit_struct_from_value() {
let expected = UnitStruct;
let actual = UnitStruct::from_value(Value::test_nothing()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn unit_struct_roundtrip() {
let expected = UnitStruct;
let actual = UnitStruct::from_value(UnitStruct.into_test_value()).unwrap();
assert_eq!(expected, actual);
let expected = Value::test_nothing();
let actual = UnitStruct::from_value(Value::test_nothing())
.unwrap()
.into_test_value();
assert_eq!(expected, actual);
}
#[derive(IntoValue, FromValue, Debug, PartialEq)]
enum Enum {
AlphaOne,
BetaTwo,
CharlieThree,
}
impl Enum {
fn make() -> [Self; 3] {
[Enum::AlphaOne, Enum::BetaTwo, Enum::CharlieThree]
}
fn value() -> Value {
Value::test_list(vec![
Value::test_string("alpha_one"),
Value::test_string("beta_two"),
Value::test_string("charlie_three"),
])
}
}
#[test]
fn enum_into_value() {
let expected = Enum::value();
let actual = Enum::make().into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn enum_from_value() {
let expected = Enum::make();
let actual = <[Enum; 3]>::from_value(Enum::value()).unwrap();
assert_eq!(expected, actual);
}
#[test]
fn enum_roundtrip() {
let expected = Enum::make();
let actual = <[Enum; 3]>::from_value(Enum::make().into_test_value()).unwrap();
assert_eq!(expected, actual);
let expected = Enum::value();
let actual = <[Enum; 3]>::from_value(Enum::value())
.unwrap()
.into_test_value();
assert_eq!(expected, actual);
}
#[test]
fn enum_unknown_variant() {
let value = Value::test_string("delta_four");
let res = Enum::from_value(value);
assert!(res.is_err());
}
#[test]
fn enum_incorrect_type() {
// Should work for every type that is not a record.
let value = Value::test_nothing();
let res = Enum::from_value(value);
assert!(res.is_err());
}
// Generate the `Enum` from before but with all possible `rename_all` variants.
macro_rules! enum_rename_all {
($($ident:ident: $case:literal => [$a1:literal, $b2:literal, $c3:literal]),*) => {
$(
#[derive(Debug, PartialEq, IntoValue, FromValue)]
#[nu_value(rename_all = $case)]
enum $ident {
AlphaOne,
BetaTwo,
CharlieThree
}
impl $ident {
fn make() -> [Self; 3] {
[Self::AlphaOne, Self::BetaTwo, Self::CharlieThree]
}
fn value() -> Value {
Value::test_list(vec![
Value::test_string($a1),
Value::test_string($b2),
Value::test_string($c3),
])
}
}
)*
#[test]
fn enum_rename_all_into_value() {$({
let expected = $ident::value();
let actual = $ident::make().into_test_value();
assert_eq!(expected, actual);
})*}
#[test]
fn enum_rename_all_from_value() {$({
let expected = $ident::make();
let actual = <[$ident; 3]>::from_value($ident::value()).unwrap();
assert_eq!(expected, actual);
})*}
}
}
enum_rename_all! {
Upper: "UPPER CASE" => ["ALPHA ONE", "BETA TWO", "CHARLIE THREE"],
Lower: "lower case" => ["alpha one", "beta two", "charlie three"],
Title: "Title Case" => ["Alpha One", "Beta Two", "Charlie Three"],
Camel: "camelCase" => ["alphaOne", "betaTwo", "charlieThree"],
Pascal: "PascalCase" => ["AlphaOne", "BetaTwo", "CharlieThree"],
Snake: "snake_case" => ["alpha_one", "beta_two", "charlie_three"],
UpperSnake: "UPPER_SNAKE_CASE" => ["ALPHA_ONE", "BETA_TWO", "CHARLIE_THREE"],
Kebab: "kebab-case" => ["alpha-one", "beta-two", "charlie-three"],
Cobol: "COBOL-CASE" => ["ALPHA-ONE", "BETA-TWO", "CHARLIE-THREE"],
Train: "Train-Case" => ["Alpha-One", "Beta-Two", "Charlie-Three"],
Flat: "flatcase" => ["alphaone", "betatwo", "charliethree"],
UpperFlat: "UPPERFLATCASE" => ["ALPHAONE", "BETATWO", "CHARLIETHREE"]
}