use super::opnsense::{OPNsense, StaticMap}; use crate::infra::maybe_string::MaybeString; use crate::modules::opnsense::NumberOption; use crate::modules::opnsense::Range; use yaserde_derive::{YaDeserialize, YaSerialize}; pub struct DhcpConfig<'a> { opnsense: &'a mut OPNsense, } impl<'a> DhcpConfig<'a> { pub fn new(opnsense: &'a mut OPNsense) -> Self { Self { opnsense } } pub fn add_static_mapping(&mut self, mac: String, ipaddr: String, hostname: String) { let static_map = StaticMap { mac, ipaddr, hostname, descr: Default::default(), winsserver: Default::default(), dnsserver: Default::default(), ntpserver: Default::default(), }; self.opnsense.dhcpd.lan.staticmaps.push(static_map); } pub fn get_static_mappings(&self) -> &[StaticMap] { &self.opnsense.dhcpd.lan.staticmaps } } #[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)] #[yaserde(rename = "dhcpd")] pub struct Dhcpd { #[yaserde(rename = "lan")] pub lan: DhcpInterface, } #[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)] pub struct DhcpInterface { pub enable: i32, pub gateway: String, pub domain: String, #[yaserde(rename = "ddnsdomainalgorithm")] pub ddns_domain_algorithm: String, #[yaserde(rename = "numberoptions")] pub number_options: Vec, #[yaserde(rename = "range")] pub range: Range, pub winsserver: MaybeString, pub dnsserver: MaybeString, pub ntpserver: MaybeString, #[yaserde(rename = "staticmap")] pub staticmaps: Vec, pub pool: MaybeString, } #[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)] pub struct DhcpRange { #[yaserde(rename = "from")] pub from: String, #[yaserde(rename = "to")] pub to: String, } #[cfg(test)] mod test { use super::*; use pretty_assertions::assert_eq; #[test] fn dhcpd_should_deserialize_serialize_identical() { let dhcpd: Dhcpd = yaserde::de::from_str(SERIALIZED_DHCPD).expect("Deserialize Dhcpd failed"); let yaserde_cfg = yaserde::ser::Config { perform_indent: true, write_document_declaration: false, ..Default::default() }; assert_eq!( yaserde::ser::to_string_with_config(&dhcpd, &yaserde_cfg) .expect("Serialize Dhcpd failed"), SERIALIZED_DHCPD ); } const SERIALIZED_DHCPD: &str = " 1 192.168.20.1 somedomain.yourlocal.mcd hmac-md5 192.168.20.50 192.168.20.200 192.168.20.1 55:55:55:55:55:1c 192.168.20.160 somehost983 someservire8 55:55:55:55:55:1c 192.168.20.155 somehost893 55:55:55:55:55:1c 192.168.20.165 somehost893 55:55:55:55:55:1c 192.168.20.50 hostswitch2 switch-2 (bottom) "; }