From ac238794ffdbf03617595a74da80542584126000 Mon Sep 17 00:00:00 2001 From: Zeeshan Ali Khan Date: Wed, 31 Jan 2024 23:09:49 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20zb:=20Split=20Display?= =?UTF-8?q?=20impl=20of=20address::Transport?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Let each transport impl have its own Display impl. This not only makes the code more readbable, but also makes it possible for users to display the transport types independently. --- zbus/src/address/transport/autolaunch.rs | 11 ++++ zbus/src/address/transport/launchd.rs | 6 ++ zbus/src/address/transport/mod.rs | 80 ++---------------------- zbus/src/address/transport/tcp.rs | 30 +++++++++ zbus/src/address/transport/unix.rs | 55 +++++++++++++++- zbus/src/address/transport/vsock.rs | 6 ++ 6 files changed, 113 insertions(+), 75 deletions(-) diff --git a/zbus/src/address/transport/autolaunch.rs b/zbus/src/address/transport/autolaunch.rs index d04245995..1276391d0 100644 --- a/zbus/src/address/transport/autolaunch.rs +++ b/zbus/src/address/transport/autolaunch.rs @@ -7,6 +7,17 @@ pub struct Autolaunch { pub(super) scope: Option, } +impl std::fmt::Display for Autolaunch { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "autolaunch:")?; + if let Some(scope) = &self.scope { + write!(f, "scope={}", scope)?; + } + + Ok(()) + } +} + impl Autolaunch { /// Create a new autolaunch transport. pub fn new() -> Self { diff --git a/zbus/src/address/transport/launchd.rs b/zbus/src/address/transport/launchd.rs index 7475d7511..f87b1c4cb 100644 --- a/zbus/src/address/transport/launchd.rs +++ b/zbus/src/address/transport/launchd.rs @@ -52,3 +52,9 @@ impl Launchd { }) } } + +impl std::fmt::Display for Launchd { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "launchd:env={}", self.env) + } +} diff --git a/zbus/src/address/transport/mod.rs b/zbus/src/address/transport/mod.rs index 986dd22e2..6315364a6 100644 --- a/zbus/src/address/transport/mod.rs +++ b/zbus/src/address/transport/mod.rs @@ -7,11 +7,11 @@ use crate::win32::windows_autolaunch_bus_address; use crate::{Error, Result}; #[cfg(not(feature = "tokio"))] use async_io::Async; +use std::collections::HashMap; #[cfg(not(feature = "tokio"))] use std::net::TcpStream; #[cfg(unix)] use std::os::unix::net::{SocketAddr, UnixStream}; -use std::{collections::HashMap, ffi::OsStr}; #[cfg(feature = "tokio")] use tokio::net::TcpStream; #[cfg(feature = "tokio-vsock")] @@ -330,86 +330,18 @@ pub(super) fn encode_percents(f: &mut Formatter<'_>, mut value: &[u8]) -> std::f impl Display for Transport { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - fn fmt_unix_path(f: &mut Formatter<'_>, path: &OsStr) -> std::fmt::Result { - #[cfg(unix)] - { - use std::os::unix::ffi::OsStrExt; - - encode_percents(f, path.as_bytes())?; - } - - #[cfg(windows)] - write!(f, "{}", path.to_str().ok_or(std::fmt::Error)?)?; - - Ok(()) - } - match self { - Self::Tcp(addr) => { - match addr.nonce_file() { - Some(nonce_file) => { - f.write_str("nonce-tcp:noncefile=")?; - encode_percents(f, nonce_file)?; - f.write_str(",")?; - } - None => f.write_str("tcp:")?, - } - f.write_str("host=")?; - - encode_percents(f, addr.host().as_bytes())?; - - write!(f, ",port={}", addr.port())?; - - if let Some(bind) = addr.bind() { - f.write_str(",bind=")?; - encode_percents(f, bind.as_bytes())?; - } - - if let Some(family) = addr.family() { - write!(f, ",family={family}")?; - } - } - - Self::Unix(unix) => match unix.path() { - UnixPath::File(path) => { - f.write_str("unix:path=")?; - fmt_unix_path(f, path)?; - } - #[cfg(target_os = "linux")] - UnixPath::Abstract(name) => { - f.write_str("unix:abstract=")?; - encode_percents(f, name)?; - } - UnixPath::Dir(path) => { - f.write_str("unix:dir=")?; - fmt_unix_path(f, path)?; - } - UnixPath::TmpDir(path) => { - f.write_str("unix:tmpdir=")?; - fmt_unix_path(f, path)?; - } - }, - + Self::Tcp(tcp) => write!(f, "{}", tcp)?, + Self::Unix(unix) => write!(f, "{}", unix)?, #[cfg(any( all(feature = "vsock", not(feature = "tokio")), feature = "tokio-vsock" ))] - Self::Vsock(addr) => { - write!(f, "vsock:cid={},port={}", addr.cid(), addr.port())?; - } - + Self::Vsock(vsock) => write!(f, "{}", vsock)?, #[cfg(windows)] - Self::Autolaunch(autolaunch) => { - write!(f, "autolaunch:")?; - if let Some(scope) = autolaunch.scope() { - write!(f, "scope={scope}")?; - } - } - + Self::Autolaunch(autolaunch) => write!(f, "{}", autolaunch)?, #[cfg(target_os = "macos")] - Self::Launchd(launchd) => { - write!(f, "launchd:env={}", launchd.env())?; - } + Self::Launchd(launchd) => write!(f, "{}", launchd)?, } Ok(()) diff --git a/zbus/src/address/transport/tcp.rs b/zbus/src/address/transport/tcp.rs index 18610700a..81288d9a4 100644 --- a/zbus/src/address/transport/tcp.rs +++ b/zbus/src/address/transport/tcp.rs @@ -1,3 +1,4 @@ +use super::encode_percents; use crate::{Error, Result}; #[cfg(not(feature = "tokio"))] use async_io::Async; @@ -168,6 +169,35 @@ impl Tcp { } } +impl Display for Tcp { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self.nonce_file() { + Some(nonce_file) => { + f.write_str("nonce-tcp:noncefile=")?; + encode_percents(f, nonce_file)?; + f.write_str(",")?; + } + None => f.write_str("tcp:")?, + } + f.write_str("host=")?; + + encode_percents(f, self.host().as_bytes())?; + + write!(f, ",port={}", self.port())?; + + if let Some(bind) = self.bind() { + f.write_str(",bind=")?; + encode_percents(f, bind.as_bytes())?; + } + + if let Some(family) = self.family() { + write!(f, ",family={family}")?; + } + + Ok(()) + } +} + /// A `tcp:` address family. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum TcpTransportFamily { diff --git a/zbus/src/address/transport/unix.rs b/zbus/src/address/transport/unix.rs index 513b7c0ec..9e9a8c9be 100644 --- a/zbus/src/address/transport/unix.rs +++ b/zbus/src/address/transport/unix.rs @@ -1,4 +1,11 @@ -use std::{collections::HashMap, ffi::OsString}; +use std::{ + collections::HashMap, + ffi::{OsStr, OsString}, + fmt::{Display, Formatter}, +}; + +#[cfg(unix)] +use super::encode_percents; /// A Unix domain socket transport in a D-Bus address. #[derive(Clone, Debug, PartialEq, Eq)] @@ -49,6 +56,12 @@ impl Unix { } } +impl Display for Unix { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "unix:{}", self.path) + } +} + /// A Unix domain socket path in a D-Bus address. #[derive(Clone, Debug, PartialEq, Eq)] #[non_exhaustive] @@ -73,3 +86,43 @@ pub enum UnixPath { /// This address is mostly relevant to server (typically bus broker) implementations. TmpDir(OsString), } + +impl Display for UnixPath { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt_unix_path(f: &mut Formatter<'_>, path: &OsStr) -> std::fmt::Result { + #[cfg(unix)] + { + use std::os::unix::ffi::OsStrExt; + + encode_percents(f, path.as_bytes())?; + } + + #[cfg(windows)] + write!(f, "{}", path.to_str().ok_or(std::fmt::Error)?)?; + + Ok(()) + } + + match self { + UnixPath::File(path) => { + f.write_str("path=")?; + fmt_unix_path(f, path)?; + } + #[cfg(target_os = "linux")] + UnixPath::Abstract(name) => { + f.write_str("abstract=")?; + encode_percents(f, name)?; + } + UnixPath::Dir(path) => { + f.write_str("dir=")?; + fmt_unix_path(f, path)?; + } + UnixPath::TmpDir(path) => { + f.write_str("tmpdir=")?; + fmt_unix_path(f, path)?; + } + } + + Ok(()) + } +} diff --git a/zbus/src/address/transport/vsock.rs b/zbus/src/address/transport/vsock.rs index 2d920e6bc..4b590192b 100644 --- a/zbus/src/address/transport/vsock.rs +++ b/zbus/src/address/transport/vsock.rs @@ -41,3 +41,9 @@ impl Vsock { Ok(Self { cid, port }) } } + +impl std::fmt::Display for Vsock { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "vsock:cid={},port={}", self.cid, self.port) + } +}