Skip to main content

appstruct_contracts/
lib.rs

1//! Current format versions for persisted and generated `AppStruct` contracts.
2
3/// Source files embedded by the generated backend runtime.
4#[doc(hidden)]
5pub mod __source {
6    pub const LIB: &str = include_str!("lib.rs");
7}
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct ContractVersion {
11    pub current: u32,
12}
13
14impl ContractVersion {
15    #[must_use]
16    pub const fn exact(current: u32) -> Self {
17        Self { current }
18    }
19
20    #[must_use]
21    pub const fn supports(self, version: u32) -> bool {
22        version == self.current
23    }
24}
25
26pub const IR: ContractVersion = ContractVersion::exact(18);
27pub const RUNTIME_API: ContractVersion = ContractVersion::exact(5);
28pub const MODULE_API: ContractVersion = ContractVersion::exact(1);
29pub const PROJECT_LAYOUT: ContractVersion = ContractVersion::exact(2);
30pub const DATABASE_SCHEMA: ContractVersion = ContractVersion::exact(3);
31pub const OWNERSHIP_MANIFEST: ContractVersion = ContractVersion::exact(1);
32pub const CACHE_SCHEMA: ContractVersion = ContractVersion::exact(2);
33pub const TRANSACTION_JOURNAL: ContractVersion = ContractVersion::exact(1);
34
35pub const CONTRACT_MATRIX: &[(&str, ContractVersion)] = &[
36    ("ir", IR),
37    ("runtime_api", RUNTIME_API),
38    ("module_api", MODULE_API),
39    ("project_layout", PROJECT_LAYOUT),
40    ("database_schema", DATABASE_SCHEMA),
41    ("ownership_manifest", OWNERSHIP_MANIFEST),
42    ("cache_schema", CACHE_SCHEMA),
43    ("transaction_journal", TRANSACTION_JOURNAL),
44];
45
46#[cfg(test)]
47mod tests {
48    #[test]
49    fn contracts_accept_only_the_current_format() {
50        for (name, version) in super::CONTRACT_MATRIX {
51            assert!(version.supports(version.current), "{name}");
52            assert!(!version.supports(version.current + 1), "{name}");
53            assert!(!version.supports(version.current - 1), "{name}");
54        }
55        assert_eq!(super::__source::LIB.len(), include_str!("lib.rs").len());
56    }
57}