Properly parse optional config elements

This commit is contained in:
Nadja Reitzenstein 2022-01-05 21:01:16 +01:00
parent 70bfdbbf4e
commit 17005c0536
2 changed files with 14 additions and 5 deletions

View File

@ -33,7 +33,8 @@
] ]
, machines = , machines =
{ Testmachine = { Testmachine =
{ description = Some "A test machine" { description = "A test machine"
, wiki = "test"
, disclose = "lab.test.read" , disclose = "lab.test.read"
, manage = "lab.test.admin" , manage = "lab.test.admin"
, name = "MachineA" , name = "MachineA"
@ -41,7 +42,7 @@
, write = "lab.test.write" , write = "lab.test.write"
}, },
Another = Another =
{ description = Some "Another test machine" { wiki = "test_another"
, disclose = "lab.test.read" , disclose = "lab.test.read"
, manage = "lab.test.admin" , manage = "lab.test.admin"
, name = "Another" , name = "Another"
@ -49,7 +50,7 @@
, write = "lab.test.write" , write = "lab.test.write"
}, },
Yetmore = Yetmore =
{ description = Some "Yet more test machines" { description = "Yet more test machines"
, disclose = "lab.test.read" , disclose = "lab.test.read"
, manage = "lab.test.admin" , manage = "lab.test.admin"
, name = "Yetmore" , name = "Yetmore"

View File

@ -352,6 +352,7 @@ impl Inner {
} }
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
/// A description of a machine /// A description of a machine
/// ///
/// This is the struct that a machine is serialized to/from. /// This is the struct that a machine is serialized to/from.
@ -359,11 +360,12 @@ impl Inner {
pub struct MachineDescription { pub struct MachineDescription {
/// The name of the machine. Doesn't need to be unique but is what humans will be presented. /// The name of the machine. Doesn't need to be unique but is what humans will be presented.
pub name: String, pub name: String,
/// An optional description of the Machine. /// An optional description of the Machine.
#[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
pub description: Option<String>, pub description: Option<String>,
#[serde(default)] #[serde(default, skip_serializing_if = "Option::is_none", deserialize_with = "deser_option")]
#[serde(flatten)]
pub wiki: Option<String>, pub wiki: Option<String>,
/// The permission required /// The permission required
@ -371,6 +373,12 @@ pub struct MachineDescription {
pub privs: access::PrivilegesBuf, pub privs: access::PrivilegesBuf,
} }
fn deser_option<'de, D, T>(d: D) -> std::result::Result<Option<T>, D::Error>
where D: serde::Deserializer<'de>, T: serde::Deserialize<'de>,
{
Ok(T::deserialize(d).ok())
}
impl MachineDescription { impl MachineDescription {
pub fn load_file<P: AsRef<Path>>(path: P) -> Result<HashMap<MachineIdentifier, MachineDescription>> { pub fn load_file<P: AsRef<Path>>(path: P) -> Result<HashMap<MachineIdentifier, MachineDescription>> {
let content = fs::read(path)?; let content = fs::read(path)?;