mirror of
https://github.com/nushell/nushell.git
synced 2024-12-29 02:19:44 +01:00
0a1cdc5107
* Add the nu-serde crate nu-serde is a crate that can be used to turn a value implementing `serde::Serialize` into a `nu-protocol::Value`. This has the potential to significantly simplify plugin authorship. This crate was the previously independent [serde-nu](https://github.com/lily-mara/serde-nu) but the nushell maintainers expressed an interest in having it added to the mainline nushell repository. * fixup! Add the nu-serde crate
994 B
994 B
serde-nu
Convert any value implementing serde::Serialize
into a
nu_protocol::Value
using nu_serde::to_value
. Compare the below manual
implemeentation and the one using nu_serde
.
use nu_protocol::{Dictionary, Primitive, UntaggedValue, Value};
use nu_source::Tag;
use serde::Serialize;
#[derive(Serialize)]
struct MyStruct {
index: usize,
name: String,
}
fn manual(s: MyStruct, tag: Tag) -> Value {
let mut dict = Dictionary::default();
dict.insert(
"index".into(),
Value {
value: UntaggedValue::Primitive(Primitive::Int(s.index as i64)),
tag: tag.clone(),
},
);
dict.insert(
"name".into(),
Value {
value: UntaggedValue::Primitive(Primitive::String(s.name)),
tag: tag.clone(),
},
);
Value {
value: UntaggedValue::Row(dict),
tag,
}
}
fn auto(s: &MyStruct, tag: Tag) -> Value {
nu_serde::to_value(s, tag).unwrap()
}