From 5eb8499793c8adab82e7907e83c1725949000212 Mon Sep 17 00:00:00 2001 From: ByteOtter Date: Tue, 12 Sep 2023 16:11:41 +0200 Subject: [PATCH] test basic serialization --- src/collectors/network_collector.rs | 15 +++++++++++++++ src/main.rs | 5 +++++ src/translator/translator.rs | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/collectors/network_collector.rs b/src/collectors/network_collector.rs index d47f2d4..4073c15 100644 --- a/src/collectors/network_collector.rs +++ b/src/collectors/network_collector.rs @@ -45,6 +45,21 @@ pub struct NetworkInformation { is_connected: bool, } +impl NetworkInformation { + fn serialize_to_json(&self) -> String { + match serde_json::to_string(&self) { + Ok(json) => json, + Err(err) => { + println!( + "Warning: Could not serialize interface '{}'. ({})", + &self.name, err + ); + String::new() + } + } + } +} + /// Collect information about all network interfaces. /// /// ### Returns diff --git a/src/main.rs b/src/main.rs index 738c272..d3b9b85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod translator; use clap::Parser; use collectors::{dmi_collector, network_collector}; use configuration::config_parser::set_up_configuration; +use translator::translator::serialize_to_json; /// The arguments that netbox-sync expects to get via the cli. /// @@ -52,4 +53,8 @@ fn main() { }; println!("Configuration: \n{:#?}", config); + + let json = serialize_to_json(output, output2); + + println!("JSON ----- \n{}", json); } diff --git a/src/translator/translator.rs b/src/translator/translator.rs index 316b730..283aac7 100644 --- a/src/translator/translator.rs +++ b/src/translator/translator.rs @@ -5,6 +5,22 @@ use collectors::{dmi_collector, network_collector}; use serde::{Deserialize, Serialize}; -use serde_json::{Result, Value}; use crate::collectors; + +pub fn serialize_to_json( + dmi_info: dmi_collector::DmiInformation, + network_info: Vec, +) -> String { + let system = match serde_json::to_string(&dmi_info) { + Ok(json) => json, + Err(err) => panic!("{}", err), + }; + let network = match serde_json::to_string(&network_info) { + Ok(json) => json, + Err(err) => panic!("{}", err), + }; + let result: String = system + &network; + + result +}