#shell #serde

nu-serde

Turn any value into a nu-protocol::Value with serde

5 releases (breaking)

0.40.0 Nov 16, 2021
0.39.0 Oct 27, 2021
0.38.0 Oct 5, 2021
0.37.0 Sep 14, 2021
0.36.0 Aug 24, 2021

#44 in #shell

Download history 433/week @ 2021-08-22 333/week @ 2021-08-29 277/week @ 2021-09-05 777/week @ 2021-09-12 440/week @ 2021-09-19 354/week @ 2021-09-26 588/week @ 2021-10-03 357/week @ 2021-10-10 330/week @ 2021-10-17 501/week @ 2021-10-24 324/week @ 2021-10-31 311/week @ 2021-11-07 573/week @ 2021-11-14 416/week @ 2021-11-21

1,707 downloads per month
Used in 3 crates (via nu-command)

MIT license

17KB
393 lines

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()
}

lib.rs:

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()
}

Dependencies

~5MB
~108K SLoC

Ja