Skip to content

Commit

Permalink
🔥 Remove default_address()
Browse files Browse the repository at this point in the history
We can rely on `TryFrom<BusType> for Address` to produce the default
address for `--session` and `--system`.
  • Loading branch information
jokeyrhyme committed Dec 14, 2024
1 parent 008ab64 commit f004d14
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/bin/busd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<()> {
Address::try_from(bus_type)?
};

let mut bus = bus::Bus::for_address(Some(&format!("{address}"))).await?;
let mut bus = bus::Bus::for_address(&format!("{address}")).await?;

#[cfg(unix)]
if let Some(fd) = args.ready_fd {
Expand Down
29 changes: 2 additions & 27 deletions src/bus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use anyhow::{bail, Ok, Result};
#[cfg(unix)]
use std::{env, path::Path};
use std::{str::FromStr, sync::Arc};
#[cfg(unix)]
use tokio::fs::remove_file;
Expand Down Expand Up @@ -45,11 +43,8 @@ enum Listener {
}

impl Bus {
pub async fn for_address(address: Option<&str>) -> Result<Self> {
let mut address = match address {
Some(address) => Address::from_str(address)?,
None => Address::from_str(&default_address())?,
};
pub async fn for_address(address: &str) -> Result<Self> {
let mut address = Address::from_str(address)?;
let guid: OwnedGuid = match address.guid() {
Some(guid) => guid.to_owned().into(),
None => {
Expand Down Expand Up @@ -246,23 +241,3 @@ impl Bus {
self.inner.next_id
}
}

#[cfg(unix)]
fn default_address() -> String {
let runtime_dir = env::var("XDG_RUNTIME_DIR")
.as_ref()
.map(|s| Path::new(s).to_path_buf())
.ok()
.unwrap_or_else(|| {
Path::new("/run")
.join("user")
.join(format!("{}", nix::unistd::Uid::current()))
});

format!("unix:dir={}", runtime_dir.display())
}

#[cfg(not(unix))]
fn default_address() -> String {
"tcp:host=127.0.0.1,port=4242".to_string()
}
2 changes: 1 addition & 1 deletion tests/fdo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async fn name_ownership_changes() {
}

async fn name_ownership_changes_(address: &str) {
let mut bus = Bus::for_address(Some(address)).await.unwrap();
let mut bus = Bus::for_address(address).await.unwrap();
let (tx, rx) = tokio::sync::oneshot::channel();

let handle = tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion tests/greet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async fn greet() {
}

async fn greet_(socket_addr: &str) {
let mut bus = Bus::for_address(Some(socket_addr)).await.unwrap();
let mut bus = Bus::for_address(socket_addr).await.unwrap();
let (tx, mut rx) = channel(1);

let handle = tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion tests/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn become_monitor() {
busd::tracing_subscriber::init();

let address = "tcp:host=127.0.0.1,port=4242".to_string();
let mut bus = Bus::for_address(Some(&address)).await.unwrap();
let mut bus = Bus::for_address(&address).await.unwrap();
let (tx, rx) = tokio::sync::oneshot::channel();

let handle = tokio::spawn(async move {
Expand Down
2 changes: 1 addition & 1 deletion tests/multiple_conns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn multi_conenct() {
}

async fn multi_conenct_(socket_addr: &str) {
let mut bus = Bus::for_address(Some(socket_addr)).await.unwrap();
let mut bus = Bus::for_address(socket_addr).await.unwrap();
let (tx, rx) = channel();

let handle = tokio::spawn(async move {
Expand Down

0 comments on commit f004d14

Please sign in to comment.