pub mod manager;
pub mod service;
pub mod unit;
use zbus::OwnedObjectPath;
use crate::error::SystemdTransientError;
const UNIT_PATH_PREFIX: &str = "/org/freedesktop/systemd1/unit/";
fn unit_path(name: &str) -> String {
let mut path = String::from(UNIT_PATH_PREFIX);
if name.is_empty() {
path.push('_');
return path;
}
for (i, b) in name.bytes().enumerate() {
if b.is_ascii_alphabetic() || (i > 0 && b.is_ascii_digit()) {
path.push(b as char);
} else {
path.push_str(&format!("_{:02x}", b));
}
}
path
}
pub(crate) fn unit_object_path(name: &str) -> Result<OwnedObjectPath, SystemdTransientError> {
Ok(OwnedObjectPath::try_from(unit_path(name))?)
}
#[cfg(test)]
mod tests {
use super::unit_path;
#[test]
fn escapes_like_systemd() {
assert_eq!(
unit_path("dbus.service"),
"/org/freedesktop/systemd1/unit/dbus_2eservice"
);
assert_eq!(
unit_path("my-job.service"),
"/org/freedesktop/systemd1/unit/my_2djob_2eservice"
);
assert_eq!(
unit_path("1x.service"),
"/org/freedesktop/systemd1/unit/_31x_2eservice"
);
}
}