From 14d995b7cd5970219690b534f073b1dd8eec04c4 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 25 Jan 2026 09:12:28 +0000 Subject: [PATCH] refactor: polkit message formatting --- --- a/crates/polkit-shared/Cargo.toml +++ b/crates/polkit-shared/Cargo.toml @@ -4,6 +4,6 @@ edition = "2021" [dependencies] -nix = "0.29.0" -serde = { version = "1.0.204", features = ["derive"] } -zbus = "4.4.0" +nix.workspace = true +serde = { workspace = true, features = ["derive"] } +zbus.workspace = true --- a/crates/polkit-shared/src/lib.rs +++ b/crates/polkit-shared/src/lib.rs @@ -6,108 +6,108 @@ use zbus::zvariant::{OwnedValue, Type, Value}; pub fn emphasize(s: impl AsRef) -> String { - format!("<{}>", escape(s),) + format!("<{}>", escape(s),) } fn command(s: impl AsRef) -> String { - format!("{}", s.as_ref()) + format!("{}", s.as_ref()) } fn escape(s: impl AsRef) -> String { - s.as_ref() - .replace("&", """) - .replace("<", "<") - .replace(">", ">") + s.as_ref() + .replace("&", """) + .replace("<", "<") + .replace(">", ">") } pub struct PidDisplay(pub u32); impl fmt::Display for PidDisplay { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.0 == 1 { - emphasize("init").fmt(f) - } else if let Ok(proc) = fs::read_to_string(format!("/proc/{}/cmdline", self.0)) { - write!( - f, - "command{}", - command( - proc.replace("\0", " ") - .strip_suffix(" ") - .expect("cmdline should end with NUL") - ) - ) - } else if let Ok(proc) = fs::read_to_string(format!("/proc/{}/comm", self.0)) { - write!(f, "process{}", command(proc.replace("\0", " "))) - } else { - emphasize("unknown process").fmt(f) - } - } + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.0 == 1 { + emphasize("init").fmt(f) + } else if let Ok(proc) = fs::read_to_string(format!("/proc/{}/cmdline", self.0)) { + write!( + f, + "command: {}", + command( + proc.replace("\0", " ") + .strip_suffix(" ") + .expect("cmdline should end with NUL") + ) + ) + } else if let Ok(proc) = fs::read_to_string(format!("/proc/{}/comm", self.0)) { + write!(f, "process: {}", command(proc.replace("\0", " "))) + } else { + emphasize("unknown process").fmt(f) + } + } } #[derive(Serialize, Deserialize, Type, PartialEq, Debug)] pub struct Identity { - pub kind: String, - pub details: HashMap, + pub kind: String, + pub details: HashMap, } impl Identity { - pub fn uid(&self) -> Option { - if self.kind != "unix-user" { - return None; - } - let uid = self.details.get("uid")?; - let Value::U32(uid) = &**uid else { - return None; - }; - Some(Uid::from_raw(*uid)) - } + pub fn uid(&self) -> Option { + if self.kind != "unix-user" { + return None; + } + let uid = self.details.get("uid")?; + let Value::U32(uid) = &**uid else { + return None; + }; + Some(Uid::from_raw(*uid)) + } } impl fmt::Display for Identity { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.kind.as_str() { - "unix-user" => match self.details.get("uid").map(|v| &**v) { - Some(Value::U32(uid)) => match User::from_uid(Uid::from_raw(*uid)) { - Ok(Some(u)) => write!( - f, - "user{}{}{}", - u.name, - u.uid, - if u.gecos.is_empty() { - "".to_owned() - } else { - format!(": {}", escape(u.gecos.to_string_lossy())) - } - ), - Ok(None) => emphasize("not found").fmt(f), - Err(e) => { - let user = format!("could not get user: {e}"); - emphasize(&user).fmt(f)?; - Ok(()) - } - }, + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.kind.as_str() { + "unix-user" => match self.details.get("uid").map(|v| &**v) { + Some(Value::U32(uid)) => match User::from_uid(Uid::from_raw(*uid)) { + Ok(Some(u)) => write!( + f, + "user: {} {} {}", + u.name, + u.uid, + if u.gecos.is_empty() { + "".to_owned() + } else { + format!(": {}", escape(u.gecos.to_string_lossy())) + } + ), + Ok(None) => emphasize("not found").fmt(f), + Err(e) => { + let user = format!("could not get user: {e}"); + emphasize(&user).fmt(f)?; + Ok(()) + } + }, - _ => emphasize("unknown uid").fmt(f), - }, - _ => emphasize(format!("identity of unknown kind: {}", self.kind)).fmt(f), - } - } + _ => emphasize("unknown uid").fmt(f), + }, + _ => emphasize(format!("identity of unknown kind: {}", self.kind)).fmt(f), + } + } } impl Clone for Identity { - fn clone(&self) -> Self { - Self { - kind: self.kind.clone(), - details: self - .details - .iter() - .map(|(k, v)| (k.clone(), v.try_clone().expect("no fds are expected"))) - .collect(), - } - } + fn clone(&self) -> Self { + Self { + kind: self.kind.clone(), + details: self + .details + .iter() + .map(|(k, v)| (k.clone(), v.try_clone().expect("no fds are expected"))) + .collect(), + } + } } #[derive(Serialize, Deserialize, Type, PartialEq, Debug)] pub struct BackendRequest { - pub cookie: String, - pub environment: HashMap, - pub prompter_path: String, - pub identity: Identity, + pub cookie: String, + pub environment: HashMap, + pub prompter_path: String, + pub identity: Identity, } -- gitstuff