Skip to content
Open
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ keywords = ["systemd", "zbus"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
zbus = { version = "2.0.1" }
zvariant = "3.0.0"
zbus = "3.15"
zvariant = "3.15"
thiserror = "1.0.29"
tracing = "0.1"

Expand Down
2 changes: 1 addition & 1 deletion examples/list_units_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main() -> Result<()> {
let units = client.list_units()?;
for unit in units {
let unit: Unit = unit.into();
println!("{:#?}", unit);
println!("{unit:#?}");
}
Ok(())
}
2 changes: 1 addition & 1 deletion examples/list_units_nonblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub async fn main() -> Result<()> {
let units = client.list_units().await?;
for unit in units {
let unit: Unit = unit.into();
println!("{:#?}", unit);
println!("{unit:#?}");
}
Ok(())
}
10 changes: 5 additions & 5 deletions examples/start_service_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() -> Result<()> {
.unit(unit_builder)
.service(svc_builder)
.build();
let svc_unit_literal = format!("{}", svc_unit);
let svc_unit_literal = format!("{svc_unit}");
// create /etc/systemd/system/test.service
create_unit_configuration_file("test.service", svc_unit_literal.as_bytes())?;
let client = manager::build_blocking_proxy()?;
Expand All @@ -29,16 +29,16 @@ fn main() -> Result<()> {
// verify unit state given unit path
let client = unit::build_blocking_proxy(svc_unit_path)?;
let unit_props = client.get_properties()?;
let unit_props: UnitProps = unit_props.into();
println!("{:?}", unit_props);
let unit_props: UnitProps = unit_props;
println!("{unit_props:?}");
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
std::thread::sleep(std::time::Duration::from_secs(4));
// service should exit after 3 sec
let unit_props = client.get_properties()?;
let unit_props: UnitProps = unit_props.into();
println!("{:?}", unit_props);
let unit_props: UnitProps = unit_props;
println!("{unit_props:?}");
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
Expand Down
6 changes: 3 additions & 3 deletions examples/start_service_nonblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> Result<()> {
.unit(unit_builder)
.service(svc_builder)
.build();
let svc_unit_literal = format!("{}", svc_unit);
let svc_unit_literal = format!("{svc_unit}");
// create /etc/systemd/system/test.service
create_unit_configuration_file("test.service", svc_unit_literal.as_bytes())?;
let client = manager::build_nonblock_proxy().await?;
Expand All @@ -30,14 +30,14 @@ async fn main() -> Result<()> {
// verify unit state given unit path
let client = unit::build_nonblock_proxy(svc_unit_path).await?;
let unit_props = client.get_properties().await?;
println!("{:?}", unit_props);
println!("{unit_props:?}");
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
assert_eq!(unit_props.active_state, UnitActiveStateType::Active);
assert_eq!(unit_props.sub_state, UnitSubStateType::Running);
std::thread::sleep(std::time::Duration::from_secs(4));
// service should exit after 3 sec
let unit_props = client.get_properties().await?;
println!("{:?}", unit_props);
println!("{unit_props:?}");
assert_eq!(unit_props.load_state, UnitLoadStateType::Loaded);
assert_eq!(unit_props.active_state, UnitActiveStateType::Inactive);
assert_eq!(unit_props.sub_state, UnitSubStateType::Dead);
Expand Down
18 changes: 12 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ impl ToString for UnitSubStateType {
UnitSubStateType::Active => String::from("active"),
UnitSubStateType::AutoRestart => String::from("auto-restart"),
UnitSubStateType::Deactivating => String::from("deactivating"),
UnitSubStateType::DeactivatingSigterm => String::from("deactivating-sigkill"),
UnitSubStateType::DeactivatingSigkill => String::from("deactivating-sigkill"),
UnitSubStateType::DeactivatingSigterm | UnitSubStateType::DeactivatingSigkill => {
String::from("deactivating-sigkill")
}
UnitSubStateType::Dead => String::from("dead"),
UnitSubStateType::Elapsed => String::from("elapsed"),
UnitSubStateType::Exited => String::from("exited"),
Expand Down Expand Up @@ -245,10 +246,7 @@ impl From<UnitTuple> for Unit {
let load_state: UnitLoadStateType = t.2.into();
let active_state: UnitActiveStateType = t.3.into();
let sub_state: UnitSubStateType = t.4.into();
let follow_unit = match t.5.is_empty() {
true => None,
false => Some(t.5),
};
let follow_unit = if t.5.is_empty() { None } else { Some(t.5) };
let object_path = t.6;
let job_id = t.7;
let job_ty = t.8;
Expand Down Expand Up @@ -278,6 +276,7 @@ pub struct UnitProps {
}

impl UnitProps {
#[must_use]
pub fn builder() -> UnitPropsBuilder {
UnitPropsBuilder::default()
}
Expand All @@ -298,6 +297,7 @@ impl Default for UnitPropsBuilder {
}

impl UnitPropsBuilder {
#[must_use]
pub fn new() -> Self {
UnitPropsBuilder {
id: None,
Expand All @@ -308,31 +308,37 @@ impl UnitPropsBuilder {
}
}

#[must_use]
pub fn id(mut self, id: String) -> Self {
self.id = Some(id);
self
}

#[must_use]
pub fn description(mut self, description: String) -> Self {
self.description = Some(description);
self
}

#[must_use]
pub fn load_state(mut self, load_state: String) -> Self {
self.load_state = Some(load_state.into());
self
}

#[must_use]
pub fn active_state(mut self, active_state: String) -> Self {
self.active_state = Some(active_state.into());
self
}

#[must_use]
pub fn sub_state(mut self, sub_state: String) -> Self {
self.sub_state = Some(sub_state.into());
self
}

#[must_use]
pub fn build(self) -> UnitProps {
let id = self.id.expect("id undefined");
let description = self.description.expect("description undefined");
Expand Down
41 changes: 34 additions & 7 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl<'a> Display for UnitConfiguration<'a> {
}

impl<'a> UnitConfiguration<'a> {
#[must_use]
pub fn builder() -> UnitConfigurationBuilder<'a> {
UnitConfigurationBuilder::default()
}
Expand All @@ -27,16 +28,19 @@ pub struct UnitConfigurationBuilder<'a> {
}

impl<'a> UnitConfigurationBuilder<'a> {
#[must_use]
pub fn description(mut self, description: &'a str) -> Self {
self.description = description;
self
}

#[must_use]
pub fn after(mut self, after: &'a str) -> Self {
self.after.push(after);
self
}

#[must_use]
pub fn build(self) -> UnitConfiguration<'a> {
let description = self.description;
let after = self.after;
Expand All @@ -57,7 +61,7 @@ impl Display for ServiceType {
ServiceType::Exec => "exec",
ServiceType::Forking => "forking",
};
write!(f, "{}", ty)
write!(f, "{ty}")
}
}

Expand All @@ -76,7 +80,7 @@ impl Display for RestartPolicy {
RestartPolicy::OnFailure => "on-failure",
RestartPolicy::Always => "always",
};
write!(f, "{}", policy)
write!(f, "{policy}")
}
}

Expand All @@ -86,6 +90,7 @@ pub struct EnvironmentVariable<'a> {
}

impl<'a> EnvironmentVariable<'a> {
#[must_use]
pub fn builder() -> EnvironmentVariableBuilder<'a> {
EnvironmentVariableBuilder::default()
}
Expand All @@ -109,23 +114,27 @@ impl<'a> Default for EnvironmentVariableBuilder<'a> {
}

impl<'a> EnvironmentVariableBuilder<'a> {
#[must_use]
pub fn new() -> Self {
EnvironmentVariableBuilder {
key: None,
value: None,
}
}

#[must_use]
pub fn key(mut self, key: &'a str) -> Self {
self.key = Some(key);
self
}

#[must_use]
pub fn value(mut self, value: &'a str) -> Self {
self.value = Some(value);
self
}

#[must_use]
pub fn build(self) -> EnvironmentVariable<'a> {
let key = self.key.expect("key undefined");
let value = self.value.expect("value undefined");
Expand All @@ -150,16 +159,16 @@ impl<'a> Display for ServiceConfiguration<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "[Service]")?;
if let Some(working_directory) = self.working_directory {
writeln!(f, "WorkingDirectory={}", working_directory)?;
writeln!(f, "WorkingDirectory={working_directory}")?;
}
if let Some(user) = self.user {
writeln!(f, "User={}", user)?;
writeln!(f, "User={user}")?;
}
if let Some(group) = self.group {
writeln!(f, "Group={}", group)?;
writeln!(f, "Group={group}")?;
}
for env in self.envs.iter() {
writeln!(f, r#"Environment="{}""#, env)?;
for env in &self.envs {
writeln!(f, r#"Environment="{env}""#)?;
}
writeln!(f, "ExecStart={}", self.exec_start.join(" "))?;
writeln!(f, "Restart={}", self.restart_policy)?;
Expand All @@ -168,6 +177,7 @@ impl<'a> Display for ServiceConfiguration<'a> {
}

impl<'a> ServiceConfiguration<'a> {
#[must_use]
pub fn builder() -> ServiceConfigurationBuilder<'a> {
ServiceConfigurationBuilder::default()
}
Expand Down Expand Up @@ -200,47 +210,56 @@ impl<'a> Default for ServiceConfigurationBuilder<'a> {
}

impl<'a> ServiceConfigurationBuilder<'a> {
#[must_use]
pub fn ty(mut self, ty: ServiceType) -> Self {
self.ty = ty;
self
}

#[must_use]
pub fn exec_start(mut self, exec_start: Vec<&'a str>) -> Self {
self.exec_start = exec_start;
self
}

#[must_use]
pub fn restart_policy(mut self, restart_policy: RestartPolicy) -> Self {
self.restart_policy = restart_policy;
self
}

#[must_use]
pub fn restart_sec(mut self, restart_sec: &'a str) -> Self {
self.restart_sec = restart_sec;
self
}

#[must_use]
pub fn working_directory(mut self, working_directory: &'a str) -> Self {
self.working_directory = Some(working_directory);
self
}

#[must_use]
pub fn user(mut self, user: &'a str) -> Self {
self.user = Some(user);
self
}

#[must_use]
pub fn group(mut self, group: &'a str) -> Self {
self.group = Some(group);
self
}

#[must_use]
pub fn env(mut self, key: &'a str, value: &'a str) -> Self {
self.envs
.push(EnvironmentVariable::builder().key(key).value(value).build());
self
}

#[must_use]
pub fn build(self) -> ServiceConfiguration<'a> {
let ty = self.ty;
let exec_start = self.exec_start;
Expand Down Expand Up @@ -275,6 +294,7 @@ impl<'a> Display for InstallConfiguration<'a> {
}

impl<'a> InstallConfiguration<'a> {
#[must_use]
pub fn builder() -> InstallConfigurationBuilder<'a> {
InstallConfigurationBuilder::default()
}
Expand All @@ -294,11 +314,13 @@ impl<'a> Default for InstallConfigurationBuilder<'a> {
}

impl<'a> InstallConfigurationBuilder<'a> {
#[must_use]
pub fn wanted_by(mut self, wanted_by: &'a str) -> Self {
self.wanted_by.push(wanted_by);
self
}

#[must_use]
pub fn build(self) -> InstallConfiguration<'a> {
let wanted_by = self.wanted_by;
InstallConfiguration { wanted_by }
Expand All @@ -319,6 +341,7 @@ impl<'a> Display for ServiceUnitConfiguration<'a> {
}

impl<'a> ServiceUnitConfiguration<'a> {
#[must_use]
pub fn builder() -> ServiceUnitConfigurationBuilder<'a> {
ServiceUnitConfigurationBuilder::default()
}
Expand All @@ -332,21 +355,25 @@ pub struct ServiceUnitConfigurationBuilder<'a> {
}

impl<'a> ServiceUnitConfigurationBuilder<'a> {
#[must_use]
pub fn unit(mut self, unit: UnitConfigurationBuilder<'a>) -> Self {
self.unit = unit;
self
}

#[must_use]
pub fn service(mut self, service: ServiceConfigurationBuilder<'a>) -> Self {
self.service = service;
self
}

#[must_use]
pub fn install(mut self, install: InstallConfigurationBuilder<'a>) -> Self {
self.install = install;
self
}

#[must_use]
pub fn build(self) -> ServiceUnitConfiguration<'a> {
let unit = self.unit.build();
let service = self.service.build();
Expand Down
Loading