Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ zb: Split Display impl of address::Transport #560

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions zbus/src/address/transport/autolaunch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ pub struct Autolaunch {
pub(super) scope: Option<AutolaunchScope>,
}

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 {
Expand Down
6 changes: 6 additions & 0 deletions zbus/src/address/transport/launchd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
80 changes: 6 additions & 74 deletions zbus/src/address/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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(())
Expand Down
30 changes: 30 additions & 0 deletions zbus/src/address/transport/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::encode_percents;
use crate::{Error, Result};
#[cfg(not(feature = "tokio"))]
use async_io::Async;
Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 54 additions & 1 deletion zbus/src/address/transport/unix.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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]
Expand All @@ -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(())
}
}
6 changes: 6 additions & 0 deletions zbus/src/address/transport/vsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading