Skip to content

Commit

Permalink
⚡ Avoid String -> zbus::Address -> String
Browse files Browse the repository at this point in the history
  • Loading branch information
jokeyrhyme committed Jan 11, 2025
1 parent 725b263 commit 7aa216c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/bin/busd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn main() -> Result<()> {
Err(err) => {
if args.config.is_some() || args.session || args.system {
// User expected us to read a specific XML file.
error!("Cannot read {}: {} ", config_path.display(), err);
error!("Cannot read {} ", config_path.display());
return Err(err);
} else {
// No explicit indications from user. Fallback to default.
Expand All @@ -88,7 +88,7 @@ async fn main() -> Result<()> {
let address = if let Some(address) = args.address {
Some(address)
} else {
config.listen.map(|address| format!("{address}"))
config.listen
};

let mut bus = bus::Bus::for_address(address.as_deref()).await?;
Expand Down
20 changes: 7 additions & 13 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use anyhow::{Error, Result};
use policy::OptionalPolicy;
use serde::Deserialize;
use zbus::{Address, AuthMechanism};
use zbus::AuthMechanism;

pub mod policy;
pub mod rule;
Expand Down Expand Up @@ -50,9 +50,9 @@ pub struct Config {
/// The address is in the standard D-Bus format that contains a transport name plus possible
/// parameters/options.
// TODO: warn when multiple `<listen>` elements are defined, as we only support one
// TODO: consider implementing `Deserialize` over in zbus crate, then removing this "skip..."
#[serde(default, skip_deserializing)]
pub listen: Option<Address>,
// NOTE: we're using `String` here as we parse into a `zbus::Address` later
#[serde(default)]
pub listen: Option<String>,

/// The bus daemon will write its pid to the specified file.
pub pidfile: Option<PathBuf>,
Expand Down Expand Up @@ -105,7 +105,7 @@ impl TryFrom<Document> for Config {
// NO-OP: deprecated and ignored
}
Element::Listen(listen) => {
config.listen = Some(Address::from_str(&listen)?);
config.listen = Some(listen);
}
Element::Pidfile(p) => config.pidfile = Some(p),
Element::Policy(pe) => {
Expand Down Expand Up @@ -322,10 +322,7 @@ mod tests {
assert_eq!(
config,
Config {
listen: Some(
Address::from_str("tcp:host=localhost,port=0,family=ipv4")
.expect("should parse address")
),
listen: Some(String::from("tcp:host=localhost,port=0,family=ipv4")),
..Default::default()
}
);
Expand Down Expand Up @@ -361,10 +358,7 @@ mod tests {
config,
Config {
auth: Some(AuthMechanism::External),
listen: Some(
Address::from_str("tcp:host=localhost,port=1234")
.expect("should parse address")
),
listen: Some(String::from("tcp:host=localhost,port=1234")),
policies: vec![
Policy::DefaultContext(vec![
(
Expand Down
15 changes: 5 additions & 10 deletions tests/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{path::PathBuf, str::FromStr};
use std::path::PathBuf;

use busd::config::{
Access, BusType, Config, ConnectOperation, MessageType, Name, NameOwnership, Operation, Policy,
ReceiveOperation, SendOperation,
};
use zbus::{Address, AuthMechanism};
use zbus::AuthMechanism;

#[test]
fn config_read_file_with_includes_ok() {
Expand All @@ -15,7 +15,7 @@ fn config_read_file_with_includes_ok() {
got,
Config {
auth: Some(AuthMechanism::External),
listen: Some(Address::from_str("unix:path=/tmp/a").expect("should parse address")),
listen: Some(String::from("unix:path=/tmp/a")),
policies: vec![
Policy::DefaultContext(vec![
(
Expand Down Expand Up @@ -121,9 +121,7 @@ fn config_read_file_session_conf_ok() {
assert_eq!(
got,
Config {
listen: Some(
Address::from_str("unix:path=/run/user/1000/bus").expect("should parse address")
),
listen: Some(String::from("unix:path=/run/user/1000/bus")),
keep_umask: true,
policies: vec![Policy::DefaultContext(vec![
(
Expand Down Expand Up @@ -158,10 +156,7 @@ fn config_read_file_system_conf_ok() {
let want = Config {
auth: Some(AuthMechanism::External),
fork: true,
listen: Some(
Address::from_str("unix:path=/var/run/dbus/system_bus_socket")
.expect("should parse address"),
),
listen: Some(String::from("unix:path=/var/run/dbus/system_bus_socket")),
pidfile: Some(PathBuf::from("@DBUS_SYSTEM_PID_FILE@")),
policies: vec![
Policy::DefaultContext(vec![
Expand Down

0 comments on commit 7aa216c

Please sign in to comment.