From 1a06773cf7e10847fae7e08d4c223b8effa4e58c Mon Sep 17 00:00:00 2001 From: Vojtech Vilimek Date: Thu, 8 Feb 2024 10:43:54 +0100 Subject: [PATCH] YANG: trivial rust implementation of show memory --- yang/rust/.gitignore | 2 ++ yang/rust/Cargo.toml | 9 +++++++ yang/rust/src/main.rs | 60 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 yang/rust/.gitignore create mode 100644 yang/rust/Cargo.toml create mode 100644 yang/rust/src/main.rs diff --git a/yang/rust/.gitignore b/yang/rust/.gitignore new file mode 100644 index 00000000..1e7caa9e --- /dev/null +++ b/yang/rust/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target/ diff --git a/yang/rust/Cargo.toml b/yang/rust/Cargo.toml new file mode 100644 index 00000000..ce3daa0a --- /dev/null +++ b/yang/rust/Cargo.toml @@ -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" diff --git a/yang/rust/src/main.rs b/yang/rust/src/main.rs new file mode 100644 index 00000000..0890ef72 --- /dev/null +++ b/yang/rust/src/main.rs @@ -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 = 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 = serde_cbor::from_reader(file.unwrap()); + if let Ok(report) = rep { + println!("{:?}", report); + } else { + let err = rep.err().unwrap(); + println!("Error {}", err); + } +}