diff options
| author | dev <dev@localhost> | 2026-11-20 17:13:44 +0000 |
|---|---|---|
| committer | dev <dev@localhost> | 2026-11-20 17:13:44 +0000 |
| commit | 8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4 (patch) | |
| tree | 2ce6c30a688ef3fb15af5639e22cfc230f8f3627 /src/db/model/state.rs | |
| parent | b73bb60403c3f2e7fc55d5038831912e8371e3a6 (diff) | |
| download | aur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.tar.gz aur-build-8cd32e5309b14f5264813e5f2ce6ecdb5aa41fb4.zip | |
db: one module per aggregate, and an interface you can read
queries.rs was 1,501 lines holding 83 queries across seventeen aggregates,
separated by comment banners that said "settings", "release", "notices" and
then kept them in one file. The banners were the design; they just never got
files. Each one has one now, verbatim — the split is provably content-only,
the moved text being identical to the original as a sorted set of lines.
model.rs and builds.rs were each two concerns deep, so they became
directories. model.rs held what a row looks like and, separately, the state
machines that say which moves are legal; builds.rs held the build machine and,
below its own `step rows` banner, the step machine. The impl_state_enum!
macro implements StateEnum for all five enums from one place, so it moved
with its invocations rather than being exported across a boundary to chase
them.
They are directories rather than new sibling modules on purpose:
`db::model::CandidateState` and `db::builds::steps` are used across the crate
and by the integration tests, and renaming them would be a bigger change than
the one being made. No call site and no test changed.
db/mod.rs now lists its interface instead of spraying one. `pub use model::*`
and `pub use queries::*` are gone, replaced by seventeen explicit exports, and
the query modules are private — so a query that was not chosen for export is
not reachable, and the width of the module is visible from its own text
rather than only from a grep.
The ratchet caught the win immediately: db.glob went 2 -> 0, which failed
a_shrunk_interface_is_written_back until the budget was written back at the
new value. db is off the glob allowlist now rather than holding a stale zero.
277 passed / 0 failed, clippy -D warnings clean, largest file in src/db 363
lines (candidates.rs, unchanged from before this work).
Diffstat (limited to 'src/db/model/state.rs')
| -rw-r--r-- | src/db/model/state.rs | 208 |
1 files changed, 208 insertions, 0 deletions
diff --git a/src/db/model/state.rs b/src/db/model/state.rs new file mode 100644 index 0000000..4882428 --- /dev/null +++ b/src/db/model/state.rs @@ -0,0 +1,208 @@ +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 { + pub 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> { + CandidateState::ALL.iter().copied().find(|c| c.as_str() == s) + } + + pub fn is_open(&self) -> bool { + *self == CandidateState::PendingReview + } + + pub fn can_transition(&self, to: CandidateState) -> bool { + use CandidateState::*; + 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 fn predecessors(to: CandidateState) -> Vec<&'static str> { + CandidateState::ALL + .iter() + .filter(|from| from.can_transition(to)) + .map(|from| from.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"; +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum ReleaseState { + Published, + Withdrawn, + FailedUnbuilt, +} + +impl ReleaseState { + pub 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> { + ReleaseState::ALL.iter().copied().find(|r| r.as_str() == s) + } + + pub fn can_transition(&self, to: ReleaseState) -> bool { + use ReleaseState::*; + if *self == to { + return true; + } + match self { + Published => matches!(to, Withdrawn | FailedUnbuilt), + Withdrawn | FailedUnbuilt => false, + } + } +} + +impl std::fmt::Display for ReleaseState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + +pub trait StateEnum: Copy + Sized { + const KIND: &'static str; + fn parse(text: &str) -> Option<Self>; + fn as_str(&self) -> &'static str; +} + +macro_rules! impl_state_enum { + ($t:ty, $kind:literal) => { + impl StateEnum for $t { + const KIND: &'static str = $kind; + fn parse(text: &str) -> Option<Self> { + <$t>::parse(text) + } + fn as_str(&self) -> &'static str { + <$t>::as_str(self) + } + } + }; +} + +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 DIGEST_DRIFT: &str = "digest_drift"; + pub const INTEGRITY_ALERT: &str = "integrity_alert"; + pub const UNPINABLE_PKGVER: &str = "unpinable_pkgver"; + pub const ORPHANED: &str = "orphaned"; + pub const DELETED: &str = "deleted"; + pub const RENAMED: &str = "renamed"; + 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 { + pub 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> { + SubscriptionState::ALL.iter().copied().find(|st| st.as_str() == text) + } + + pub fn can_transition(&self, to: SubscriptionState) -> bool { + use SubscriptionState::*; + if *self == to { + return true; + } + match self { + Active => matches!(to, Paused | Orphaned | Removed), + Paused => matches!(to, Active | Orphaned | Removed), + Orphaned => matches!(to, Active | Removed), + Removed => false, + } + } +} + +impl std::fmt::Display for SubscriptionState { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str(self.as_str()) + } +} + |
