Skip to content

Commit

Permalink
🩹 zb: make address parsing target-specific
Browse files Browse the repository at this point in the history
Some addresses should only be used on specific target OS. This makes the
address handling specific to the target, which will make its usage
impractical for some use cases (web, configuration etc)

As requested by Zeeshan.
  • Loading branch information
elmarco committed Sep 24, 2024
1 parent b17e61c commit f3e5d25
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 7 additions & 1 deletion zbus/src/address/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{borrow::Cow, ffi::OsStr};

#[cfg(target_os = "windows")]
use super::transport::AutolaunchScope;
use super::{
transport::{AutolaunchScope, TcpFamily, Transport, UnixAddrKind},
transport::{TcpFamily, Transport, UnixAddrKind},
Address,
};

Expand Down Expand Up @@ -75,6 +77,7 @@ fn parse_unix() {
Address::try_from(String::from("unix:path=/tmp/foo")).unwrap();
}

#[cfg(target_os = "macos")]
#[test]
fn parse_launchd() {
let addr = Address::try_from("launchd:env=FOOBAR").unwrap();
Expand All @@ -89,6 +92,7 @@ fn parse_launchd() {
);
}

#[cfg(target_os = "linux")]
#[test]
fn parse_systemd() {
let addr = Address::try_from("systemd:").unwrap();
Expand Down Expand Up @@ -152,9 +156,11 @@ fn parse_unixexec() {
#[test]
fn parse_autolaunch() {
let addr = Address::try_from("autolaunch:scope=*user").unwrap();
#[allow(unused)]
let Transport::Autolaunch(t) = addr.transport().unwrap() else {
panic!();
};
#[cfg(target_os = "windows")]
assert_eq!(t.scope().unwrap(), &AutolaunchScope::User);
}

Expand Down
18 changes: 16 additions & 2 deletions zbus/src/address/transport/autolaunch.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use std::marker::PhantomData;
#[cfg(target_os = "windows")]
use std::{borrow::Cow, fmt};

use super::{percent::decode_percents_str, Address, Error, KeyValFmt, KeyValFmtAdd, Result};
#[cfg(target_os = "windows")]
use super::percent::decode_percents_str;
use super::{Address, Error, KeyValFmt, KeyValFmtAdd, Result};

/// Scope of autolaunch (Windows only)
#[cfg(target_os = "windows")]
#[derive(Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum AutolaunchScope<'a> {
Expand All @@ -14,6 +19,7 @@ pub enum AutolaunchScope<'a> {
Other(Cow<'a, str>),
}

#[cfg(target_os = "windows")]
impl fmt::Display for AutolaunchScope<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Expand All @@ -24,6 +30,7 @@ impl fmt::Display for AutolaunchScope<'_> {
}
}

#[cfg(target_os = "windows")]
impl<'a> TryFrom<Cow<'a, str>> for AutolaunchScope<'a> {
type Error = Error;

Expand All @@ -41,10 +48,13 @@ impl<'a> TryFrom<Cow<'a, str>> for AutolaunchScope<'a> {
/// <https://dbus.freedesktop.org/doc/dbus-specification.html#meta-transports-autolaunch>
#[derive(Debug, PartialEq, Eq, Default)]
pub struct Autolaunch<'a> {
#[cfg(target_os = "windows")]
scope: Option<AutolaunchScope<'a>>,
phantom: PhantomData<&'a ()>,
}

impl<'a> Autolaunch<'a> {
#[cfg(target_os = "windows")]
/// Scope of autolaunch (Windows only)
pub fn scope(&self) -> Option<&AutolaunchScope<'a>> {
self.scope.as_ref()
Expand All @@ -55,10 +65,12 @@ impl<'a> TryFrom<&'a Address<'a>> for Autolaunch<'a> {
type Error = Error;

fn try_from(s: &'a Address<'a>) -> Result<Self> {
#[allow(unused_mut)]
let mut res = Autolaunch::default();

for (k, v) in s.key_val_iter() {
match (k, v) {
#[cfg(target_os = "windows")]
("scope", Some(v)) => {
res.scope = Some(decode_percents_str(v)?.try_into()?);
}
Expand All @@ -72,6 +84,8 @@ impl<'a> TryFrom<&'a Address<'a>> for Autolaunch<'a> {

impl KeyValFmtAdd for Autolaunch<'_> {
fn key_val_fmt_add<'a: 'b, 'b>(&'a self, kv: KeyValFmt<'b>) -> KeyValFmt<'b> {
kv.add("scope", self.scope())
#[cfg(target_os = "windows")]
let kv = kv.add("scope", self.scope());
kv
}
}
16 changes: 15 additions & 1 deletion zbus/src/address/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ use super::{
};

mod autolaunch;
pub use autolaunch::{Autolaunch, AutolaunchScope};
pub use autolaunch::Autolaunch;
#[cfg(target_os = "windows")]
pub use autolaunch::AutolaunchScope;

#[cfg(target_os = "macos")]
mod launchd;
#[cfg(target_os = "macos")]
pub use launchd::Launchd;

mod nonce_tcp;
pub use nonce_tcp::NonceTcp;

#[cfg(target_os = "linux")]
mod systemd;
#[cfg(target_os = "linux")]
pub use systemd::Systemd;

mod tcp;
Expand All @@ -38,7 +44,9 @@ pub use vsock::Vsock;
#[non_exhaustive]
pub enum Transport<'a> {
Unix(unix::Unix<'a>),
#[cfg(target_os = "macos")]
Launchd(launchd::Launchd<'a>),
#[cfg(target_os = "linux")]
Systemd(systemd::Systemd<'a>),
Tcp(tcp::Tcp<'a>),
NonceTcp(nonce_tcp::NonceTcp<'a>),
Expand All @@ -53,7 +61,9 @@ impl fmt::Display for Transport<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unix(_) => write!(f, "unix"),
#[cfg(target_os = "macos")]
Self::Launchd(_) => write!(f, "launchd"),
#[cfg(target_os = "linux")]
Self::Systemd(_) => write!(f, "systemd"),
Self::Tcp(_) => write!(f, "tcp"),
Self::NonceTcp(_) => write!(f, "nonce-tcp"),
Expand All @@ -70,7 +80,9 @@ impl KeyValFmtAdd for Transport<'_> {
fn key_val_fmt_add<'a: 'b, 'b>(&'a self, kv: KeyValFmt<'b>) -> KeyValFmt<'b> {
match self {
Self::Unix(t) => t.key_val_fmt_add(kv),
#[cfg(target_os = "macos")]
Self::Launchd(t) => t.key_val_fmt_add(kv),
#[cfg(target_os = "linux")]
Self::Systemd(t) => t.key_val_fmt_add(kv),
Self::Tcp(t) => t.key_val_fmt_add(kv),
Self::NonceTcp(t) => t.key_val_fmt_add(kv),
Expand All @@ -90,7 +102,9 @@ impl<'a> TryFrom<&'a Address<'a>> for Transport<'a> {
let col = s.addr.find(':').ok_or(Error::MissingTransport)?;
match &s.addr[..col] {
"unix" => Ok(Self::Unix(s.try_into()?)),
#[cfg(target_os = "macos")]
"launchd" => Ok(Self::Launchd(s.try_into()?)),
#[cfg(target_os = "linux")]
"systemd" => Ok(Self::Systemd(s.try_into()?)),
"tcp" => Ok(Self::Tcp(s.try_into()?)),
"nonce-tcp" => Ok(Self::NonceTcp(s.try_into()?)),
Expand Down

0 comments on commit f3e5d25

Please sign in to comment.