use serde::{Deserialize, Serialize}; use crate::db::model::build_state::{BuildState, StepState}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum CandidateState { PendingReview, Approved, Rejected, Superseded, Blocked, } impl CandidateState { const ALL: &'static [CandidateState] = &[ CandidateState::PendingReview, CandidateState::Approved, CandidateState::Rejected, CandidateState::Superseded, CandidateState::Blocked, ]; pub fn as_str(&self) -> &'static str { match self { CandidateState::PendingReview => "pending_review", CandidateState::Approved => "approved", CandidateState::Rejected => "rejected", CandidateState::Superseded => "superseded", CandidateState::Blocked => "blocked", } } pub fn parse(s: &str) -> Option { CandidateState::ALL.iter().copied().find(|c| c.as_str() == s) } pub fn is_open(&self) -> bool { *self == CandidateState::PendingReview } fn can_transition(self, to: CandidateState) -> bool { use CandidateState::{Approved, Blocked, PendingReview, Rejected, Superseded}; if self == to { return true; } match self { PendingReview => matches!(to, Approved | Rejected | Superseded | Blocked), Approved | Rejected => to == Superseded, Blocked => matches!(to, PendingReview | Approved | Rejected), Superseded => false, } } pub(crate) fn predecessors(to: CandidateState) -> Vec<&'static str> { CandidateState::ALL.iter().filter(|from| from.can_transition(to)).map(CandidateState::as_str).collect() } } impl std::fmt::Display for CandidateState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } pub mod candidate_kind { pub const NEW_VERSION: &str = "new_version"; pub const FIRST_INTRODUCTION: &str = "first_introduction"; } /// The real definition; the alias below carries the visibility. mod release_state { use super::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum ReleaseState { Published, Withdrawn, FailedUnbuilt, } } /// The state column of a release row. #[cfg(feature = "test-support")] pub use release_state::ReleaseState; #[cfg(not(feature = "test-support"))] pub(crate) use release_state::ReleaseState; impl ReleaseState { const ALL: &'static [ReleaseState] = &[ReleaseState::Published, ReleaseState::Withdrawn, ReleaseState::FailedUnbuilt]; pub fn as_str(self) -> &'static str { match self { ReleaseState::Published => "published", ReleaseState::Withdrawn => "withdrawn", ReleaseState::FailedUnbuilt => "failed_unbuilt", } } pub fn parse(s: &str) -> Option { ReleaseState::ALL.iter().copied().find(|r| r.as_str() == s) } } impl std::fmt::Display for ReleaseState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } pub(crate) trait StateEnum: Copy + Sized { const KIND: &'static str; fn parse(text: &str) -> Option; } macro_rules! impl_state_enum { ($t:ty, $kind:literal) => { impl StateEnum for $t { const KIND: &'static str = $kind; fn parse(text: &str) -> Option { <$t>::parse(text) } } }; } impl_state_enum!(BuildState, "build"); impl_state_enum!(CandidateState, "candidate"); impl_state_enum!(StepState, "build step"); impl_state_enum!(ReleaseState, "release"); impl_state_enum!(SubscriptionState, "subscription"); pub mod notice_kind { pub const PIN_DRIFT: &str = "pin_drift"; pub const RECIPE_DRIFT: &str = "recipe_drift"; pub const INTEGRITY_ALERT: &str = "integrity_alert"; pub const DELETED: &str = "deleted"; pub const DOWNGRADE: &str = "downgrade"; } #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum SubscriptionState { Active, Paused, Orphaned, Removed, } impl SubscriptionState { const ALL: &'static [SubscriptionState] = &[ SubscriptionState::Active, SubscriptionState::Paused, SubscriptionState::Orphaned, SubscriptionState::Removed, ]; pub fn as_str(&self) -> &'static str { match self { SubscriptionState::Active => "active", SubscriptionState::Paused => "paused", SubscriptionState::Orphaned => "orphaned", SubscriptionState::Removed => "removed", } } pub fn parse(text: &str) -> Option { SubscriptionState::ALL.iter().copied().find(|st| st.as_str() == text) } } impl std::fmt::Display for SubscriptionState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.write_str(self.as_str()) } } #[cfg(test)] mod tests { use super::*; // `ALL` is private, so the round trip through column text is checked here. #[test] fn every_state_declared_here_round_trips_through_its_column_text() { for s in CandidateState::ALL { assert_eq!(CandidateState::parse(s.as_str()), Some(*s), "candidate {s:?}"); } for s in ReleaseState::ALL { assert_eq!(ReleaseState::parse(s.as_str()), Some(*s), "release {s:?}"); } for s in SubscriptionState::ALL { assert_eq!(SubscriptionState::parse(s.as_str()), Some(*s), "subscription {s:?}"); } assert_eq!(CandidateState::parse("exploded"), None); assert_eq!(ReleaseState::parse("exploded"), None); assert_eq!(SubscriptionState::parse("exploded"), None); } }