diff --git a/Cargo.lock b/Cargo.lock index 5128cdf..bdd1a43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,6 +156,7 @@ dependencies = [ "rustix 1.1.5", "ser_nix", "serde", + "serde_plain", "signal-hook 0.4.5", "thiserror 2.0.21", "toml", @@ -1534,6 +1535,15 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "serde_plain" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1fc6db65a611022b23a0dec6975d63fb80a302cb3388835ff02c097258d50" +dependencies = [ + "serde", +] + [[package]] name = "serde_spanned" version = "1.1.2" diff --git a/Cargo.toml b/Cargo.toml index 31bf0b1..933d70a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ ratatui = { version = "0.30.2", features = ["serde", "unstable-rendered-line-inf rustix = { version = "1.1.5", features = ["event", "fs", "process", "stdio", "system", "termios", "time"] } ser_nix = "0.2.4" serde = { version = "1.0.229", features = ["derive"] } +serde_plain = "1.0.2" signal-hook = "0.4.5" thiserror = "2.0.12" toml = { version = "1.1.8", features = ["preserve_order"] } diff --git a/src/boot_model/unit_facts.rs b/src/boot_model/unit_facts.rs index c0bc2c0..0a08d0a 100644 --- a/src/boot_model/unit_facts.rs +++ b/src/boot_model/unit_facts.rs @@ -1,23 +1,29 @@ use std::time::Duration; -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +use serde::Deserialize; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "lowercase")] pub enum ActiveState { - Inactive, Activating, Active, Deactivating, Reloading, Failed, + #[serde(other)] + Inactive, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "lowercase")] pub enum JobResult { Done, Skipped, Canceled, - Failed, Timeout, Dependency, + #[serde(other)] + Failed, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -47,14 +53,18 @@ impl Activity { } } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] pub enum JobKind { - BringsUp, + #[serde(rename = "stop")] TakesDown, + #[serde(skip)] Unknown, + #[serde(other)] + BringsUp, } -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)] +#[serde(rename_all = "lowercase")] pub enum UnitKind { Service, Target, @@ -67,26 +77,16 @@ pub enum UnitKind { Timer, Slice, Scope, + #[serde(other)] Other, } impl UnitKind { #[must_use] pub fn of(name: &str) -> Self { - match name.rsplit_once('.').map(|(_, suffix)| suffix) { - Some("service") => Self::Service, - Some("target") => Self::Target, - Some("socket") => Self::Socket, - Some("mount") => Self::Mount, - Some("automount") => Self::Automount, - Some("device") => Self::Device, - Some("swap") => Self::Swap, - Some("path") => Self::Path, - Some("timer") => Self::Timer, - Some("slice") => Self::Slice, - Some("scope") => Self::Scope, - _ => Self::Other, - } + name.rsplit_once('.') + .and_then(|(_, suffix)| serde_plain::from_str(suffix).ok()) + .unwrap_or(Self::Other) } #[must_use] @@ -143,9 +143,27 @@ pub(super) fn summary(facts: &UnitFacts) -> String { #[cfg(test)] mod tests { + use serde_plain::from_str; + use super::*; use crate::demo_script::boot_test_fixtures::{facts, waiting}; + #[test] + fn unknown_systemd_strings_fall_back_instead_of_failing() { + assert_eq!( + from_str("deactivating").ok(), + Some(ActiveState::Deactivating) + ); + assert_eq!(from_str("maintenance").ok(), Some(ActiveState::Inactive)); + assert_eq!(from_str("dependency").ok(), Some(JobResult::Dependency)); + assert_eq!(from_str("frozen").ok(), Some(JobResult::Failed)); + assert_eq!(from_str("stop").ok(), Some(JobKind::TakesDown)); + assert_eq!(from_str("restart").ok(), Some(JobKind::BringsUp)); + assert_eq!(UnitKind::of("dev-sda.swap"), UnitKind::Swap); + assert_eq!(UnitKind::of("init.scope.d"), UnitKind::Other); + assert_eq!(UnitKind::of("no-suffix"), UnitKind::Other); + } + #[test] fn kinds_with_a_tag_drop_their_suffix() { let socket = facts("docker.socket", &[]); diff --git a/src/systemd_link/systemd_bus.rs b/src/systemd_link/systemd_bus.rs index 6c602b4..26c5d3d 100644 --- a/src/systemd_link/systemd_bus.rs +++ b/src/systemd_link/systemd_bus.rs @@ -11,7 +11,7 @@ use dbus::{ }; use crate::{ - boot_model::{ActiveState, Activity, JobKind, JobResult, UnitFacts}, + boot_model::{Activity, JobKind, JobResult, UnitFacts}, systemd_link::link_error::Error, }; @@ -26,7 +26,6 @@ const BUS_DESTINATION: &str = "org.freedesktop.DBus"; const BUS_PATH: &str = "/org/freedesktop/DBus"; const PRIVATE_SOCKET: &str = "unix:path=/run/systemd/private"; const SIGNAL_RULE: &str = "type='signal',path_namespace='/org/freedesktop/systemd1'"; -const STOP_JOB: &str = "stop"; const CALL_TIMEOUT: Duration = Duration::from_secs(1); #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -70,19 +69,11 @@ impl From for JobListing { Self { id, unit, - kind: job_kind(&kind), + kind: serde_plain::from_str(&kind).unwrap_or(JobKind::Unknown), } } } -fn job_kind(raw: &str) -> JobKind { - if raw == STOP_JOB { - JobKind::TakesDown - } else { - JobKind::BringsUp - } -} - pub struct Session { channel: Channel, pub peer_to_peer: bool, @@ -194,7 +185,7 @@ impl Session { (JOB_INTERFACE, "JobType"), )?; let kind: Variant = reply.read1()?; - Ok(job_kind(&kind.0)) + Ok(serde_plain::from_str(&kind.0).unwrap_or(JobKind::Unknown)) } pub fn fetch_unit(&self, name: &str) -> Result { @@ -298,7 +289,7 @@ fn parse_job_signal(message: &Message, member: &str) -> Option { message.read4().ok()?; Some(Signal::JobRemoved { id, - result: job_result_from_bus(&result), + result: serde_plain::from_str(&result).unwrap_or(JobResult::Failed), }) } _ => None, @@ -329,31 +320,9 @@ fn activity_from(properties: &PropMap) -> Activity { state: properties .get("ActiveState") .and_then(|value| value.0.as_str()) - .map(active_state_from_bus), + .and_then(|raw| serde_plain::from_str(raw).ok()), inactive_exit: monotonic("InactiveExitTimestampMonotonic"), active_enter: monotonic("ActiveEnterTimestampMonotonic"), active_exit: monotonic("ActiveExitTimestampMonotonic"), } } - -fn active_state_from_bus(raw: &str) -> ActiveState { - match raw { - "activating" => ActiveState::Activating, - "active" => ActiveState::Active, - "deactivating" => ActiveState::Deactivating, - "reloading" => ActiveState::Reloading, - "failed" => ActiveState::Failed, - _ => ActiveState::Inactive, - } -} - -fn job_result_from_bus(raw: &str) -> JobResult { - match raw { - "done" => JobResult::Done, - "skipped" => JobResult::Skipped, - "canceled" => JobResult::Canceled, - "timeout" => JobResult::Timeout, - "dependency" => JobResult::Dependency, - _ => JobResult::Failed, - } -}