Skip to content

Commit

Permalink
♻️ Make (send|receive)_* attributes Option<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Nov 16, 2024
1 parent 0b05b72 commit 5216e23
Showing 1 changed file with 47 additions and 70 deletions.
117 changes: 47 additions & 70 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,39 +402,26 @@ impl TryFrom<PolicyElement> for OptionalPolicy {
}
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct ReceiveOperation {
pub error: String,
pub interface: String,
pub max_fds: u32,
pub min_fds: u32,
pub path: String,
pub sender: String,
pub r#type: MessageType,
}
impl Default for ReceiveOperation {
fn default() -> Self {
Self {
error: String::from("*"),
interface: String::from("*"),
max_fds: u32::MAX,
min_fds: 0,
path: String::from("*"),
r#type: MessageType::Any,
sender: String::default(),
}
}
pub error: Option<String>,
pub interface: Option<String>,
pub max_fds: Option<u32>,
pub min_fds: Option<u32>,
pub path: Option<String>,
pub sender: Option<String>,
pub r#type: Option<MessageType>,
}
impl From<RuleAttributes> for ReceiveOperation {
fn from(value: RuleAttributes) -> Self {
Self {
error: value.receive_error.unwrap_or(String::from("*")),
interface: value.receive_interface.unwrap_or(String::from("*")),
max_fds: value.max_fds.unwrap_or(u32::MAX),
min_fds: value.min_fds.unwrap_or(0),
path: value.receive_path.unwrap_or(String::from("*")),
sender: value.receive_sender.unwrap_or(String::from("*")),
r#type: value.receive_type.unwrap_or_default(),
error: value.receive_error,
interface: value.receive_interface,
max_fds: value.max_fds,
min_fds: value.min_fds,
path: value.receive_path,
sender: value.receive_sender,
r#type: value.receive_type,
}
}
}
Expand Down Expand Up @@ -544,30 +531,16 @@ fn rules_try_from_rule_elements(value: Vec<RuleElement>) -> Result<Vec<Rule>> {

pub type Rule = (Access, Operation);

#[derive(Clone, Debug, Deserialize, PartialEq)]
#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
pub struct SendOperation {
pub broadcast: Option<bool>,
pub destination: Option<Destination>,
pub error: String,
pub interface: String,
pub max_fds: u32,
pub min_fds: u32,
pub path: String,
pub r#type: MessageType,
}
impl Default for SendOperation {
fn default() -> Self {
Self {
broadcast: None,
destination: None,
error: String::from("*"),
interface: String::from("*"),
max_fds: u32::MAX,
min_fds: 0,
path: String::from("*"),
r#type: MessageType::default(),
}
}
pub error: Option<String>,
pub interface: Option<String>,
pub max_fds: Option<u32>,
pub min_fds: Option<u32>,
pub path: Option<String>,
pub r#type: Option<MessageType>,
}
impl From<RuleAttributes> for SendOperation {
fn from(value: RuleAttributes) -> Self {
Expand All @@ -592,12 +565,12 @@ impl From<RuleAttributes> for SendOperation {
Self {
broadcast: value.send_broadcast,
destination,
error: value.send_error.unwrap_or(String::from("*")),
interface: value.send_interface.unwrap_or(String::from("*")),
max_fds: value.max_fds.unwrap_or(u32::MAX),
min_fds: value.min_fds.unwrap_or(0),
path: value.send_path.unwrap_or(String::from("*")),
r#type: value.send_type.unwrap_or_default(),
error: value.send_error,
interface: value.send_interface,
max_fds: value.max_fds,
min_fds: value.min_fds,
path: value.send_path,
r#type: value.send_type,
}
}
}
Expand Down Expand Up @@ -840,24 +813,28 @@ mod tests {
destination: Some(Destination::Name(String::from(
"org.freedesktop.DBus"
))),
error: String::from("something bad"),
interface: String::from("org.freedesktop.systemd1.Activator"),
max_fds: 128,
min_fds: 12,
path: String::from("/org/freedesktop"),
r#type: MessageType::Signal
error: Some(String::from("something bad")),
interface: Some(String::from(
"org.freedesktop.systemd1.Activator"
)),
max_fds: Some(128),
min_fds: Some(12),
path: Some(String::from("/org/freedesktop")),
r#type: Some(MessageType::Signal),
})
),
(
Access::Allow,
Operation::Receive(ReceiveOperation {
error: String::from("something bad"),
interface: String::from("org.freedesktop.systemd1.Activator"),
max_fds: 128,
min_fds: 12,
path: String::from("/org/freedesktop"),
sender: String::from("org.freedesktop.DBus"),
r#type: MessageType::Signal
error: Some(String::from("something bad")),
interface: Some(String::from(
"org.freedesktop.systemd1.Activator"
)),
max_fds: Some(128),
min_fds: Some(12),
path: Some(String::from("/org/freedesktop")),
sender: Some(String::from("org.freedesktop.DBus")),
r#type: Some(MessageType::Signal),
})
)
],
Expand All @@ -878,7 +855,7 @@ mod tests {
(
Access::Allow,
Operation::Receive(ReceiveOperation {
sender: String::from("org.freedesktop.Avahi"),
sender: Some(String::from("org.freedesktop.Avahi")),
..Default::default()
})
),
Expand Down Expand Up @@ -948,7 +925,7 @@ mod tests {
destination: Some(Destination::Name(String::from(
"org.gnome.DisplayManager"
))),
interface: String::from("org.gnome.DisplayManager.Manager"),
interface: Some(String::from("org.gnome.DisplayManager.Manager")),
// `member=... is dropped`
..Default::default()
}),
Expand Down

0 comments on commit 5216e23

Please sign in to comment.