Skip to content

Commit

Permalink
test basic serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteOtter committed Sep 12, 2023
1 parent 0fb2671 commit 5aef5f5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/collectors/network_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ 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
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -52,4 +53,9 @@ fn main() {
};

println!("Configuration: \n{:#?}", config);


let json = serialize_to_json(output, output2);

println!("JSON ----- \n{}", json);
}
15 changes: 14 additions & 1 deletion src/translator/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
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<network_collector::NetworkInformation>) -> 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
}

0 comments on commit 5aef5f5

Please sign in to comment.