Crates.io | serde_yang |
lib.rs | serde_yang |
version | 0.2.0 |
source | src |
created_at | 2023-11-22 20:34:09.995355 |
updated_at | 2023-11-25 20:25:44.841723 |
description | yang based serializer(so far) for Serde |
homepage | |
repository | https://github.com/Paul-weqe/yang |
max_upload_size | |
id | 1045490 |
size | 28,442 |
FURTHER CHANGES WILL BE UPDATED
Current changes are being made for the serializer, work on the deserializer is still in TODO
Serde is a rust library used for serialization and deserialization. serde YANG is used for serialization/deserialization of the YANG modelling language.
[dependencies]
serde_yang = "0.1.0"
YANG is a data serialization format that is capable of conveying one or multiple documents in a single resource. The format is typically not used to transport actual data but rather to model data that will be sent via other formats e.g JSON, XML etc.
The language is heavily utilized in the networking world with it being used to model data conveyed in NETCONF and RESTCONF.
A sample basic data model by YANG:
leaf host-name {
type string;
description "Hostname for this system";
}
Let us see how we serialize data from Rust data types to yang strings:
use serde_yang::ser::serializer::to_string;
use serde::Serialize;
#[derive(Serialize, Debug)]
#[serde(rename="leaf--message")]
struct MessageLeaf {
#[serde(name="type--")]
message_type: String,
#[serde(name="description--")]
message_description: String
}
fn main() {
let leaf = MessageLeaf {
message_type: String::from("string"),
message_description: String::from(r#"This field will be used to hold the message"#)
};
println!("{}", to_string(&leaf).unwrap().as_str());
/*
* Expected:
* leaf message {
* type string;
* description "This field will be used to hold the message";
* }
*/
}
Let us look at the lines very important for the serde yang operation:
#[serde(rename="leaf--message")]
This field declares a YANG leaf node for us. Leaf nodes will be defined in the form of leaf--{nodename}
where nodename is the name of the node. Which will lead to creation of leaf nodename {
The leaf node is just an example of a node defined in YANG. We have various nodes that are defined by YANG. For a list of all the fields supported by serde_yang, have a look at these node types.
The nodes may hold individual fields in them or may contain other nodes inside them.
While nodes contain either fields or other nodes, fields hold information only about themselves. They may define the type, description and so much more.
In the example above, we have defined the following types:
#[serde(name="type--")]
message_type: String,
#[serde(name="description--")]
message_description: String
You may have noticed that unlike the leaf--{nodename}
, here we just have type--
or description--
. This is a characteristic of fields in serde YANG. Currently the fields being supported are type
and description
but more will be added as time goes.
Major changes are being made for the serializer, work on the deserializer is still in TODO. Version 1.0.0 should have a more comprehensive serde implementation but have fun in the meanwhile. :)