diff options
Diffstat (limited to 'src/db/model/build_state.rs')
| -rw-r--r-- | src/db/model/build_state.rs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/src/db/model/build_state.rs b/src/db/model/build_state.rs new file mode 100644 index 0000000..a5998de --- /dev/null +++ b/src/db/model/build_state.rs @@ -0,0 +1,169 @@ +use serde::{Deserialize, Serialize}; + + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +pub enum BuildState { + Queued, + Leased, + Running, + BlockedMissingDep, + Succeeded, + Failed, + Cancelled, +} + +impl BuildState { + pub 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> { + BuildState::ALL.iter().copied().find(|b| b.as_str() == s) + } + + pub fn is_terminal(&self) -> bool { + matches!( + self, + BuildState::Succeeded | BuildState::Failed | BuildState::Cancelled + ) + } + + pub fn can_transition(&self, to: BuildState) -> bool { + use BuildState::*; + 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 fn predecessors(to: BuildState) -> Vec<&'static str> { + BuildState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.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 { + pub 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> { + StepState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + pub fn is_done(&self) -> bool { + matches!(self, StepState::Succeeded | StepState::Skipped) + } + + pub fn can_transition(&self, to: StepState) -> bool { + use StepState::*; + 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 fn predecessors(to: StepState) -> Vec<&'static str> { + StepState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.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", +]; + |
