serde-nu

Crates.ioserde-nu
lib.rsserde-nu
version0.2.0
sourcesrc
created_at2021-07-27 03:07:49.873168
updated_at2021-07-27 05:14:10.495127
descriptionTurn any value into a nu-protocol::Value with serde
homepage
repositoryhttps://github.com/lily-mara/serde-nu
max_upload_size
id427758
size28,662
Lily Mara (lily-mara)

documentation

https://docs.rs/serde-nu

README

serde-nu

Convert any value implementing serde::Serialize into a nu_protocol::Value using serde_nu::to_value. Compare the below manual implemeentation and the one using serde_nu.

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 {
    serde_nu::to_value(s, tag).unwrap()
}
Commit count: 5

cargo fmt