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 =
{ Testmachine =
{ description = Some "A test machine"
{ description = "A test machine"
, wiki = "test"
, disclose = "lab.test.read"
, manage = "lab.test.admin"
, name = "MachineA"
@ -41,7 +42,7 @@
, write = "lab.test.write"
},
Another =
{ description = Some "Another test machine"
{ wiki = "test_another"
, disclose = "lab.test.read"
, manage = "lab.test.admin"
, name = "Another"
@ -49,7 +50,7 @@
, write = "lab.test.write"
},
Yetmore =
{ description = Some "Yet more test machines"
{ description = "Yet more test machines"
, disclose = "lab.test.read"
, manage = "lab.test.admin"
, name = "Yetmore"

View File

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