Automatically generate yaml output
This commit is contained in:
55
src/main.rs
55
src/main.rs
@@ -24,6 +24,7 @@ use log::{
|
||||
Level,
|
||||
};
|
||||
use pnet;
|
||||
use serde::Serialize;
|
||||
use std::net::SocketAddrV6;
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
@@ -39,6 +40,40 @@ struct Cli {
|
||||
interface: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct MessageMap {
|
||||
server_id: Option<String>,
|
||||
domain_name_servers: Option<Vec<String>>,
|
||||
domain_search_list: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
impl MessageMap {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
server_id: None,
|
||||
domain_name_servers: None,
|
||||
domain_search_list: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn message_to_map(msg: &Message) -> MessageMap {
|
||||
let mut map = MessageMap::new();
|
||||
|
||||
if let Some(DhcpOption::ServerId(server_id)) = &msg.opts().get(OptionCode::ServerId) {
|
||||
map.server_id = Some(hex::encode(&server_id));
|
||||
}
|
||||
|
||||
if let Some(DhcpOption::DomainNameServers(domain_name_servers)) = &msg.opts().get(OptionCode::DomainNameServers) {
|
||||
map.domain_name_servers = Some(domain_name_servers.iter().map(|a| a.to_string()).collect());
|
||||
}
|
||||
if let Some(DhcpOption::DomainSearchList(domain_search_list)) = &msg.opts().get(OptionCode::DomainSearchList) {
|
||||
map.domain_search_list = Some(domain_search_list.iter().map(|n| n.to_string()).collect());
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()>{
|
||||
env_logger::init();
|
||||
@@ -115,22 +150,10 @@ async fn main() -> Result<()>{
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(DhcpOption::ServerId(server_id)) = &response_msg.opts().get(OptionCode::ServerId) {
|
||||
println!("server_id: {}", hex::encode(&server_id));
|
||||
}
|
||||
|
||||
if let Some(DhcpOption::DomainNameServers(domain_name_servers)) = &response_msg.opts().get(OptionCode::DomainNameServers) {
|
||||
println!("domain_name_servers:");
|
||||
for domain_name_server in domain_name_servers {
|
||||
println!(" - {}", domain_name_server);
|
||||
}
|
||||
}
|
||||
if let Some(DhcpOption::DomainSearchList(domain_search_list)) = &response_msg.opts().get(OptionCode::DomainSearchList) {
|
||||
println!("domain_search_list:");
|
||||
for search_domain_name in domain_search_list {
|
||||
println!(" - {}", search_domain_name);
|
||||
}
|
||||
}
|
||||
let message_map = message_to_map(&response_msg);
|
||||
let message_map_yaml = serde_yaml::to_string(&message_map)
|
||||
.context("Unable to format message to yaml")?;
|
||||
println!("{}", message_map_yaml);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user