systemd: Move EnableState to using strum

This commit is contained in:
Vicki Pfau 2024-07-22 19:43:56 -07:00
parent bc9cb4d414
commit a7355ab922

View file

@ -8,6 +8,8 @@
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use strum::{Display, EnumString};
use zbus::zvariant::OwnedObjectPath; use zbus::zvariant::OwnedObjectPath;
use zbus::{CacheProperties, Connection}; use zbus::{CacheProperties, Connection};
@ -62,9 +64,10 @@ trait SystemdManager {
async fn reload(&self) -> Result<()>; async fn reload(&self) -> Result<()>;
} }
#[derive(PartialEq, Debug, Copy, Clone)] #[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
#[strum(serialize_all = "lowercase")]
pub enum EnableState { pub enum EnableState {
Disbled, Disabled,
Enabled, Enabled,
Masked, Masked,
Static, Static,
@ -149,13 +152,7 @@ impl<'dbus> SystemdUnit<'dbus> {
} }
pub async fn enabled(&self) -> Result<EnableState> { pub async fn enabled(&self) -> Result<EnableState> {
Ok(match self.proxy.unit_file_state().await?.as_str() { Ok(EnableState::from_str(self.proxy.unit_file_state().await?.as_str())?)
"enabled" => EnableState::Enabled,
"disabled" => EnableState::Disbled,
"masked" => EnableState::Masked,
"static" => EnableState::Static,
state => bail!("Unknown state {state}"),
})
} }
} }
@ -175,6 +172,18 @@ pub fn escape(name: &str) -> String {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
use crate::enum_roundtrip;
#[test]
fn enable_state_roundtrip() {
enum_roundtrip!(EnableState {
"disabled": str = Disabled,
"enabled": str = Enabled,
"masked": str = Masked,
"static": str = Static,
});
assert!(EnableState::from_str("loaded").is_err());
}
#[test] #[test]
fn test_escape() { fn test_escape() {