Skip to content

Commit

Permalink
fix(xml): simplify PathBuf fields on Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Oct 7, 2024
1 parent ae79bf0 commit f88031f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
31 changes: 11 additions & 20 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use serde::Deserialize;

mod raw;

#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum ApparmorMode {
Disabled,
#[default]
Enabled,
Required,
}
Expand All @@ -27,22 +28,20 @@ pub struct Associate {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Configuration {
allow_anonymous: Option<bool>,
apparmor: Option<ApparmorMode>,
apparmor: ApparmorMode,
auth: Vec<String>,
fork: Option<bool>,
// TODO: consider processing `include` more to remove XML-specific structure
include: Vec<IncludeElement>,
// TODO: consider processing `include` more to remove XML-specific structure
includedir: Vec<PathBufElement>,
includedir: Vec<PathBuf>,
keep_umask: Option<bool>,
// TODO: consider processing `include` more to remove XML-specific structure
limit: Vec<LimitElement>,
listen: Vec<String>,
pidfile: Option<PathBuf>,
policy: Vec<Policy>,
selinux: Vec<Associate>,
// TODO: consider processing `include` more to remove XML-specific structure
servicedir: Vec<PathBufElement>,
servicedir: Vec<PathBuf>,
servicehelper: Option<PathBuf>,
standard_session_servicedirs: Option<bool>,
standard_system_servicedirs: Option<bool>,
Expand Down Expand Up @@ -78,11 +77,12 @@ impl TryFrom<RawConfiguration> for Configuration {
apparmor: match value.apparmor {
Some(a) => a.mode,
None => None,
},
}
.unwrap_or_default(),
auth: value.auth,
fork: value.fork.map(|_| true),
include: value.include,
includedir: value.includedir,
includedir: value.includedir.into_iter().map(|pb| pb.text).collect(),
keep_umask: value.keep_umask.map(|_| true),
limit: value.limit,
listen: value.listen,
Expand All @@ -94,7 +94,7 @@ impl TryFrom<RawConfiguration> for Configuration {
Some(s) => s.associate,
None => vec![],
},
servicedir: value.servicedir,
servicedir: value.servicedir.into_iter().map(|pb| pb.text).collect(),
servicehelper: value.servicehelper,
standard_session_servicedirs: value.standard_session_servicedirs.map(|_| true),
standard_system_servicedirs: value.standard_system_servicedirs.map(|_| true),
Expand Down Expand Up @@ -180,15 +180,6 @@ pub struct OwnRule {
own_prefix: Option<String>,
}

// reuse this between Vec<PathBuf> fields,
// except those with field-specific attributes
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub struct PathBufElement {
#[serde(rename = "$text")]
text: PathBuf,
}

#[derive(Clone, Debug, PartialEq)]
pub enum Policy {
Console { rules: Vec<Rule> },
Expand Down Expand Up @@ -635,7 +626,7 @@ mod tests {
"-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<apparmor mode="enabled" />
<apparmor mode="required" />
<selinux>
<associate own="org.freedesktop.Foobar" context="foo_t" />
</selinux>
Expand All @@ -647,7 +638,7 @@ mod tests {
assert_eq!(
got,
Configuration {
apparmor: Some(ApparmorMode::Enabled),
apparmor: ApparmorMode::Required,
selinux: vec![Associate {
context: String::from("foo_t"),
own: String::from("org.freedesktop.Foobar")
Expand Down
17 changes: 13 additions & 4 deletions src/configuration/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::{path::PathBuf, str::FromStr};
use serde::Deserialize;

use super::{
ApparmorMode, Associate, IncludeElement, LimitElement, PathBufElement, Principal, RuleMatch,
RuleMatchType, Type,
ApparmorMode, Associate, IncludeElement, LimitElement, Principal, RuleMatch, RuleMatchType,
Type,
};

#[derive(Clone, Debug, Deserialize, PartialEq)]
Expand All @@ -24,14 +24,14 @@ pub(super) struct RawConfiguration {
pub auth: Vec<String>,
pub fork: Option<()>,
pub include: Vec<IncludeElement>,
pub includedir: Vec<PathBufElement>,
pub includedir: Vec<RawPathBufElement>,
pub keep_umask: Option<()>,
pub limit: Vec<LimitElement>,
pub listen: Vec<String>,
pub pidfile: Option<PathBuf>,
pub policy: Vec<RawPolicy>,
pub selinux: Option<RawSelinux>,
pub servicedir: Vec<PathBufElement>,
pub servicedir: Vec<RawPathBufElement>,
pub servicehelper: Option<PathBuf>,
pub standard_session_servicedirs: Option<()>,
pub standard_system_servicedirs: Option<()>,
Expand Down Expand Up @@ -144,3 +144,12 @@ pub(super) struct RawUserElement {
#[serde(rename = "$text")]
pub text: Principal,
}

// reuse this between Vec<PathBuf> fields,
// except those with field-specific attributes
#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub(super) struct RawPathBufElement {
#[serde(rename = "$text")]
pub text: PathBuf,
}

0 comments on commit f88031f

Please sign in to comment.