use serde::{Deserialize, Serialize}; use crate::hash; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct UpstreamPin { pub index: usize, pub url: String, pub ref_: String, pub branch: Option, pub commit: String, } #[derive(Debug, Clone)] pub struct StageBCacheRow { pub epoch: String, pub pkgver: String, pub pkgrel: String, pub pins: Vec, } impl StageBCacheRow { pub fn version(&self) -> String { crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SourceDigest { pub index: usize, pub url: String, pub kind: String, pub digest: String, pub size: u64, pub skip_declared: bool, } impl SourceDigest { pub fn is_vcs(&self) -> bool { matches!(self.kind.as_str(), "vcs" | "git" | "svn" | "hg" | "bzr") } } #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct WorldMember { pub pkgbase: String, pub version: String, } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct Identity { pub aur_commit: String, pub epoch: String, pub pkgver: String, pub pkgrel: String, // Neither field is read by name anywhere: `hash` renders the whole struct with // `canonical_json`, so both participate in the identity digest by being here. pub upstream_pins: Vec, pub source_digests: Vec, } impl Identity { pub fn hash(&self) -> crate::error::Result { Ok(format!("sha256:{}", hash::hex(hash::canonical_json(self)?.as_bytes()))) } } #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SourceEntry { pub index: usize, pub url: String, pub filename: Option, pub commit: Option, pub tag: Option, pub branch: Option, pub vcs: Option, pub local: bool, } impl SourceEntry { pub fn is_vcs(&self) -> bool { self.vcs.is_some() } } #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct Recipe { pub pkgbase: String, pub pkgver: String, pub pkgrel: String, pub epoch: String, pub pkgdesc: String, pub url: String, pub arch: Vec, pub license: Vec, pub groups: Vec, pub depends: Vec, pub makedepends: Vec, pub checkdepends: Vec, pub optdepends: Vec, pub provides: Vec, pub conflicts: Vec, pub source: Vec, pub sha256sums: Vec, pub b2sums: Vec, pub validpgpkeys: Vec, pub options: Vec, pub backup: Vec, pub install: Option, pub pkgnames: Vec, pub pkg_depends: Vec<(String, Vec)>, pub pkgbuild: String, pub srcinfo: Option, pub skipgpcheck: bool, #[serde(skip, default)] pub source_entries: Vec, } impl Recipe { pub fn full_version(&self) -> String { crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ReleaseRow { pub id: i64, pub pkgbase_id: i64, pub candidate_id: Option, pub approval_id: Option, pub aur_commit: String, pub recipe_hash: String, pub upstream_pins_json: String, pub source_digests_json: String, pub epoch: String, pub pkgver: String, pub pkgrel: String, pub world_snapshot_id: Option, pub state: ReleaseState, pub published_at: Option, } impl ReleaseRow { pub fn version(&self) -> String { crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) } pub fn pins(&self) -> Vec { serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() } pub fn digests(&self) -> Vec { serde_json::from_str(&self.source_digests_json).unwrap_or_default() } } #[derive(Debug, Clone, Serialize)] pub struct CandidateRow { pub id: i64, pub pkgbase_id: i64, pub base_release_id: Option, pub kind: String, pub snapshot_id: i64, pub epoch: String, pub pkgver: String, pub pkgrel: String, pub upstream_pins_json: String, pub source_digests_json: String, pub identity_hash: String, pub diff_digest: String, pub revision: i64, pub viewed_at: Option, pub risk_level: String, pub risk_signals_json: String, pub missing_deps_json: String, pub state: CandidateState, pub detected_at: String, pub resolved_at: Option, pub pkgbase: String, } impl CandidateRow { pub fn version(&self) -> String { crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel) } pub fn pins(&self) -> Vec { serde_json::from_str(&self.upstream_pins_json).unwrap_or_default() } pub fn digests(&self) -> Vec { serde_json::from_str(&self.source_digests_json).unwrap_or_default() } pub fn risk_signals(&self) -> Vec { serde_json::from_str(&self.risk_signals_json).unwrap_or_default() } pub fn missing_deps(&self) -> Vec { serde_json::from_str(&self.missing_deps_json).unwrap_or_default() } } /// Test-tier only: the one reader that needed it is `db::world_members`, which /// is itself gated because nothing in production reads a world snapshot back. #[cfg(feature = "test-support")] pub(crate) fn json_or(s: &str, fallback: T) -> T { serde_json::from_str(s).unwrap_or(fallback) } // The state machines sit beside the row shapes rather than inside them: a row // says what is stored, a state machine says which moves are legal. mod build_state; mod state; pub(crate) use state::StateEnum; pub use build_state::{BuildState, StepState, BUILD_STEPS}; #[cfg(feature = "test-support")] pub use state::ReleaseState; #[cfg(not(feature = "test-support"))] pub(crate) use state::ReleaseState; pub use state::{candidate_kind, notice_kind, CandidateState, SubscriptionState}; #[cfg(test)] mod tests { use super::*; // Moved verbatim from tests/hash.rs, where it sat next to the canonical // JSON tests but asserted nothing about hashing: it pins the contract that // the identity digest is a pure function of the identity's own fields. // That is a property of `Identity`, so it lives with `Identity`. #[test] fn the_identity_digest_is_a_function_of_the_identity_alone() { let base = Identity { aur_commit: "cafe123".into(), epoch: "0".into(), pkgver: "1.2.3".into(), pkgrel: "1".into(), upstream_pins: vec![], source_digests: vec![], }; let digest = |i: &Identity| i.hash().unwrap(); assert!(digest(&base).starts_with("sha256:")); assert_eq!(digest(&base), digest(&base.clone()), "the same identity hashed twice, twice differently"); assert_ne!(digest(&base), digest(&Identity { pkgrel: "2".into(), ..base.clone() })); assert_ne!(digest(&base), digest(&Identity { epoch: "1".into(), ..base.clone() })); assert_ne!(digest(&base), digest(&Identity { aur_commit: "cafe124".into(), ..base.clone() })); } }