#value #nu #protocols #tags #convert #turn #dictionary

nu-serde

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

8 breaking releases

0.44.0 Feb 8, 2022
0.42.0 Dec 28, 2021
0.40.0 Nov 16, 2021

#2271 in Encoding

Download history 10/week @ 2024-04-08 7/week @ 2024-04-15 9/week @ 2024-04-22 7/week @ 2024-04-29 7/week @ 2024-05-06 7/week @ 2024-05-13 13/week @ 2024-05-20 22/week @ 2024-05-27 23/week @ 2024-06-03 30/week @ 2024-06-10 32/week @ 2024-06-17 22/week @ 2024-06-24 22/week @ 2024-07-01 20/week @ 2024-07-08 17/week @ 2024-07-15 22/week @ 2024-07-22

84 downloads per month

MIT license

1MB
22K SLoC

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

~5–13MB
~153K SLoC