0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-22 17:51:53 +00:00

YANG: trivial rust implementation of show memory

This commit is contained in:
Vojtech Vilimek 2024-02-08 10:43:54 +01:00
parent 1c4995f9d2
commit 1a06773cf7
3 changed files with 71 additions and 0 deletions

2
yang/rust/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
Cargo.lock
target/

9
yang/rust/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = "1.0.196"
serde_cbor = "0.11.2"
serde_derive = "1.0.196"

60
yang/rust/src/main.rs Normal file
View File

@ -0,0 +1,60 @@
use std::env;
use std::fs::File;
use serde_derive::Deserialize;
#[derive(Debug, Deserialize)]
struct Memory {
effective: u32,
overhead: u32,
}
#[derive(Debug, Deserialize)]
struct ShowMemoryBody {
routing_tables: Memory,
route_attributes: Memory,
protocols: Memory,
current_config: Memory,
standby_memory: Memory,
total: Memory,
}
#[derive(Debug, Deserialize)]
struct ShowMemory {
header: String,
body: ShowMemoryBody,
}
#[derive(Debug, Deserialize)]
struct ShowMemoryMessage {
/* serde does not support prefix, so we use rename work-around */
#[serde(rename(deserialize = "show_memory:message"))]
message: ShowMemory,
/* for prefixing both Serializator and Deserializator, use:
#[serde(rename(serialize = "show_memory:message", deserialize = "show_memory:mesage"))]
*/
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Usage: ./program {{show-memory-cbor-file}}");
return;
}
let mut file = File::open(&args[1]);
if file.is_err() {
println!("Error during openning file {} occured!", args[1].clone());
return;
}
let rep : Result<ShowMemoryMessage, _> = serde_cbor::from_reader(file.unwrap());
if let Ok(report) = rep {
println!("{:?}", report);
} else {
let err = rep.err().unwrap();
println!("Error {}", err);
}
}