use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum BuildState { Queued, Leased, Running, BlockedMissingDep, Succeeded, Failed, Cancelled, } impl BuildState { /// Every state, in column-text order. `parse` and `predecessors` /// below are its only production readers, so it is not exported. const ALL: &'static [BuildState] = &[ BuildState::Queued, BuildState::Leased, BuildState::Running, BuildState::BlockedMissingDep, BuildState::Succeeded, BuildState::Failed, BuildState::Cancelled, ]; pub fn as_str(&self) -> &'static str { match self { BuildState::Queued => "queued", BuildState::Leased => "leased", BuildState::Running => "running", BuildState::BlockedMissingDep => "blocked_missing_dep", BuildState::Succeeded => "succeeded", BuildState::Failed => "failed", BuildState::Cancelled => "cancelled", } } pub fn parse(s: &str) -> Option { BuildState::ALL.iter().copied().find(|b| b.as_str() == s) } pub fn is_terminal(&self) -> bool { matches!(self, BuildState::Succeeded | BuildState::Failed | BuildState::Cancelled) } /// The transition table itself. `predecessors` is how production /// enforces it; nothing outside this module asks the question directly. fn can_transition(self, to: BuildState) -> bool { use BuildState::{BlockedMissingDep, Cancelled, Failed, Leased, Queued, Running, Succeeded}; if self == to { return true; } match self { Queued => matches!(to, Leased | Running | BlockedMissingDep | Cancelled), Leased => matches!(to, Running | Queued | BlockedMissingDep | Succeeded | Failed | Cancelled), Running => matches!(to, Queued | BlockedMissingDep | Succeeded | Failed | Cancelled), BlockedMissingDep => matches!(to, Queued | Cancelled), Failed => matches!(to, Queued | Running), Cancelled => to == Queued, Succeeded => false, } } pub(crate) fn predecessors(to: BuildState) -> Vec<&'static str> { BuildState::ALL.iter().filter(|from| from.can_transition(to)).map(BuildState::as_str).collect() } pub fn active() -> &'static [BuildState] { &[BuildState::Queued, BuildState::Running, BuildState::Leased, BuildState::BlockedMissingDep] } } impl std::fmt::Display for BuildState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum StepState { Pending, Running, Succeeded, Failed, Interrupted, Skipped, } impl StepState { const ALL: &'static [StepState] = &[ StepState::Pending, StepState::Running, StepState::Succeeded, StepState::Failed, StepState::Interrupted, StepState::Skipped, ]; pub fn as_str(&self) -> &'static str { match self { StepState::Pending => "pending", StepState::Running => "running", StepState::Succeeded => "succeeded", StepState::Failed => "failed", StepState::Interrupted => "interrupted", StepState::Skipped => "skipped", } } pub fn parse(text: &str) -> Option { StepState::ALL.iter().copied().find(|st| st.as_str() == text) } pub fn is_done(&self) -> bool { matches!(self, StepState::Succeeded | StepState::Skipped) } fn can_transition(self, to: StepState) -> bool { use StepState::{Failed, Interrupted, Pending, Running, Skipped, Succeeded}; if self == to { return true; } match self { Pending => matches!(to, Running | Succeeded | Failed | Interrupted | Skipped), Running => matches!(to, Pending | Succeeded | Failed | Interrupted | Skipped), Failed | Interrupted => matches!(to, Pending | Running), Succeeded | Skipped => false, } } pub(crate) fn predecessors(to: StepState) -> Vec<&'static str> { StepState::ALL.iter().filter(|from| from.can_transition(to)).map(StepState::as_str).collect() } } impl std::fmt::Display for StepState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } pub const BUILD_STEPS: &[&str] = &["materialize", "plan", "chroot", "sources", "pkgver", "build", "collect", "verify", "ingest", "publish"]; #[cfg(test)] mod tests { use super::*; // Moved verbatim from tests/state_machine.rs. Both are properties of the // state enum itself — a column-text round trip and the shape of the // transition table — and neither needed the database the rest of that file // was built around. #[test] fn every_state_round_trips_through_its_column_text() { for s in BuildState::ALL { assert_eq!(BuildState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); } assert_eq!(BuildState::parse("exploded"), None); assert_eq!(BuildState::parse(""), None); } #[test] fn the_transition_table_is_the_machine_the_code_actually_uses() { let reachable = |from: BuildState| -> Vec<&'static str> { BuildState::ALL.iter().filter(|to| from.can_transition(**to)).map(super::BuildState::as_str).collect() }; assert_eq!(reachable(BuildState::Queued), ["queued", "leased", "running", "blocked_missing_dep", "cancelled"]); assert_eq!( reachable(BuildState::Leased), ["queued", "leased", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] ); assert_eq!( reachable(BuildState::Running), ["queued", "running", "blocked_missing_dep", "succeeded", "failed", "cancelled"] ); assert_eq!(reachable(BuildState::BlockedMissingDep), ["queued", "blocked_missing_dep", "cancelled"]); assert_eq!(reachable(BuildState::Failed), ["queued", "running", "failed"]); assert_eq!(reachable(BuildState::Cancelled), ["queued", "cancelled"]); assert_eq!(reachable(BuildState::Succeeded), ["succeeded"]); // The claim that `a_terminal_build_is_history_and_does_not_move` used // to make here against the table, while its database-level twin stayed // in tests/state_machine.rs. for to in [BuildState::Running, BuildState::Queued, BuildState::Failed] { assert!(!BuildState::Succeeded.can_transition(to), "succeeded must not transition to {to:?}"); } } #[test] fn step_states_round_trip_through_their_column_text() { for s in StepState::ALL { assert_eq!(StepState::parse(s.as_str()), Some(*s), "{s:?} does not round-trip"); } assert_eq!(StepState::parse("exploded"), None); } }