forked from NationTech/harmony
feat: DhcpConfig can now effectively manage a config file to add a static map entry
This commit is contained in:
@@ -1,65 +1,106 @@
|
||||
use yaserde::{NamedList, YaDeserialize as YaDeserializeTrait, YaSerialize as YaSerializeTrait};
|
||||
use yaserde_derive::{YaDeserialize, YaSerialize};
|
||||
|
||||
use yaserde::MaybeString;
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
|
||||
pub struct Interfaces {
|
||||
pub interfaces: NamedList<Interface>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct Interface {
|
||||
pub internal_dynamic: Option<MaybeString>,
|
||||
#[yaserde(rename = "if")]
|
||||
pub physical_interface_name: String,
|
||||
pub descr: String,
|
||||
pub enable: MaybeString,
|
||||
pub lock: Option<MaybeString>,
|
||||
#[yaserde(rename = "spoofmac")]
|
||||
pub spoof_mac: Option<MaybeString>,
|
||||
pub internal_dynamic: Option<MaybeString>,
|
||||
pub ipaddr: Option<MaybeString>,
|
||||
#[yaserde(rename = "blockpriv")]
|
||||
pub block_priv: Option<MaybeString>,
|
||||
#[yaserde(rename = "blockbogons")]
|
||||
pub block_bogons: Option<MaybeString>,
|
||||
pub lock: Option<MaybeString>,
|
||||
#[yaserde(rename = "type")]
|
||||
pub r#type: Option<MaybeString>,
|
||||
#[yaserde(rename = "virtual")]
|
||||
pub r#virtual: Option<MaybeString>,
|
||||
pub subnet: Option<MaybeString>,
|
||||
pub ipaddrv6: Option<MaybeString>,
|
||||
pub networks: Option<MaybeString>,
|
||||
pub subnetv6: Option<MaybeString>,
|
||||
pub ipaddrv6: Option<MaybeString>,
|
||||
#[yaserde(rename = "track6-interface")]
|
||||
pub track6_interface: Option<MaybeString>,
|
||||
#[yaserde(rename = "track6-prefix-id")]
|
||||
pub track6_prefix_id: Option<MaybeString>,
|
||||
}
|
||||
|
||||
pub trait MyDeserialize: YaDeserializeTrait {}
|
||||
pub trait MySerialize: YaSerializeTrait {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::xml_utils::to_xml_str;
|
||||
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
use yaserde::NamedList;
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct TestStruct {
|
||||
pub struct InterfacesParent {
|
||||
foo: String,
|
||||
interfaces: NamedList<Interface>,
|
||||
bar: String,
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_deserialize_interfaces() {
|
||||
let test_struct =
|
||||
yaserde::de::from_str::<TestStruct>("<xml><foo>aodisj</foo><bar>barbaba</bar></xml>")
|
||||
.unwrap();
|
||||
println!("test_struct : {:?}", test_struct);
|
||||
let interfaces =
|
||||
yaserde::de::from_str::<NamedList<Interface>>(FULL_INTERFACES_XML).unwrap();
|
||||
assert_eq!(interfaces.elements.len(), 6)
|
||||
}
|
||||
|
||||
let interfaces = yaserde::de::from_str::<Interfaces>(FULL_INTERFACES_XML).unwrap();
|
||||
assert_eq!(interfaces.interfaces.elements.len(), 6)
|
||||
#[test]
|
||||
fn should_serialize_interfaces() {
|
||||
let named_list = NamedList {
|
||||
elements: vec![
|
||||
(String::from("paul"), Interface::default()),
|
||||
(String::from("anotherpaul"), Interface::default()),
|
||||
(String::from("thirdone"), Interface::default()),
|
||||
(String::from("andgofor4"), Interface::default()),
|
||||
],
|
||||
};
|
||||
|
||||
let parent = InterfacesParent {
|
||||
foo: String::from("foo"),
|
||||
interfaces: named_list,
|
||||
bar: String::from("foo"),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
&to_xml_str(&parent).unwrap(),
|
||||
r#"<?xml version="1.0"?>
|
||||
<InterfacesParent>
|
||||
<foo>foo</foo>
|
||||
<interfaces>
|
||||
<paul>
|
||||
<if></if>
|
||||
<descr></descr>
|
||||
<enable/>
|
||||
</paul>
|
||||
<anotherpaul>
|
||||
<if></if>
|
||||
<descr></descr>
|
||||
<enable/>
|
||||
</anotherpaul>
|
||||
<thirdone>
|
||||
<if></if>
|
||||
<descr></descr>
|
||||
<enable/>
|
||||
</thirdone>
|
||||
<andgofor4>
|
||||
<if></if>
|
||||
<descr></descr>
|
||||
<enable/>
|
||||
</andgofor4>
|
||||
</interfaces>
|
||||
<bar>foo</bar>
|
||||
</InterfacesParent>
|
||||
"#
|
||||
)
|
||||
}
|
||||
|
||||
const FULL_INTERFACES_XML: &str = "<interfaces>
|
||||
|
||||
Reference in New Issue
Block a user