Skip to content

Commit

Permalink
♻️ Reorder declarations of Access and other items
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme authored and zeenix committed Nov 27, 2024
1 parent 16bd143 commit 07832eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ use xml::{
Document, Element, PolicyContext, PolicyElement, RuleAttributes, RuleElement, TypeElement,
};

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub enum Access {
Allow,
Deny,
}

/// The bus configuration.
///
/// This is currently only loaded from the [XML configuration files] defined by the specification.
Expand Down Expand Up @@ -182,6 +176,7 @@ pub struct ConnectOperation {
pub group: Option<String>,
pub user: Option<String>,
}

impl From<RuleAttributes> for ConnectOperation {
fn from(value: RuleAttributes) -> Self {
Self {
Expand Down Expand Up @@ -230,7 +225,9 @@ pub enum Operation {
/// rules that are checked when a connection attempts to send a message
Send(SendOperation),
}

type OptionalOperation = Option<Operation>;

impl TryFrom<RuleAttributes> for OptionalOperation {
type Error = Error;

Expand Down Expand Up @@ -295,6 +292,7 @@ impl TryFrom<RuleAttributes> for OptionalOperation {
pub struct OwnOperation {
pub own: Option<Name>,
}

impl From<RuleAttributes> for OwnOperation {
fn from(value: RuleAttributes) -> Self {
let own = match value {
Expand Down Expand Up @@ -328,7 +326,9 @@ pub enum Policy {
}
// TODO: implement Cmp/Ord to help stable-sort Policy values:
// DefaultContext < Group < User < MandatoryContext

type OptionalPolicy = Option<Policy>;

impl TryFrom<PolicyElement> for OptionalPolicy {
type Error = Error;

Expand Down Expand Up @@ -393,6 +393,7 @@ pub struct ReceiveOperation {
pub sender: Option<String>,
pub r#type: Option<MessageType>,
}

impl From<RuleAttributes> for ReceiveOperation {
fn from(value: RuleAttributes) -> Self {
Self {
Expand All @@ -409,6 +410,7 @@ impl From<RuleAttributes> for ReceiveOperation {
}

type OptionalRule = Option<Rule>;

impl TryFrom<RuleElement> for OptionalRule {
type Error = Error;

Expand Down Expand Up @@ -485,19 +487,14 @@ impl TryFrom<RuleElement> for OptionalRule {
}
}

fn rules_try_from_rule_elements(value: Vec<RuleElement>) -> Result<Vec<Rule>> {
let mut rules = vec![];
for rule in value {
let rule = OptionalRule::try_from(rule)?;
if let Some(some) = rule {
rules.push(some);
}
}
Ok(rules)
}

pub type Rule = (Access, Operation);

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub enum Access {
Allow,
Deny,
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
pub struct SendOperation {
pub broadcast: Option<bool>,
Expand All @@ -510,6 +507,7 @@ pub struct SendOperation {
pub path: Option<String>,
pub r#type: Option<MessageType>,
}

impl From<RuleAttributes> for SendOperation {
fn from(value: RuleAttributes) -> Self {
let destination = match value {
Expand Down Expand Up @@ -546,19 +544,30 @@ impl From<RuleAttributes> for SendOperation {

const DEFAULT_DATA_DIRS: &[&str] = &["/usr/local/share", "/usr/share"];

const STANDARD_SYSTEM_SERVICEDIRS: &[&str] = &[
"/usr/local/share/dbus-1/system-services",
"/usr/share/dbus-1/system-services",
"/lib/dbus-1/system-services",
];

fn rules_try_from_rule_elements(value: Vec<RuleElement>) -> Result<Vec<Rule>> {
let mut rules = vec![];
for rule in value {
let rule = OptionalRule::try_from(rule)?;
if let Some(some) = rule {
rules.push(some);
}
}
Ok(rules)
}

fn xdg_data_dirs() -> Vec<PathBuf> {
if let Ok(ok) = var("XDG_DATA_DIRS") {
return ok.split(":").map(PathBuf::from).collect();
}
DEFAULT_DATA_DIRS.iter().map(PathBuf::from).collect()
}

const STANDARD_SYSTEM_SERVICEDIRS: &[&str] = &[
"/usr/local/share/dbus-1/system-services",
"/usr/share/dbus-1/system-services",
"/lib/dbus-1/system-services",
];

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions src/config/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub struct Document {
pub busconfig: Vec<Element>,
file_path: Option<PathBuf>,
}

impl FromStr for Document {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
quick_xml::de::from_str(s).map_err(Error::msg)
}
}

impl Document {
pub fn read_file(file_path: impl AsRef<Path>) -> Result<Document> {
let text = read_to_string(file_path.as_ref())?;
Expand Down

0 comments on commit 07832eb

Please sign in to comment.