Skip to main content

appstruct_contracts/
lib.rs

1//! Central compatibility policy 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 VersionRange {
11    pub minimum: u32,
12    pub current: u32,
13}
14
15impl VersionRange {
16    #[must_use]
17    pub const fn exact(current: u32) -> Self {
18        Self {
19            minimum: current,
20            current,
21        }
22    }
23
24    #[must_use]
25    pub const fn supports(self, version: u32) -> bool {
26        version >= self.minimum && version <= self.current
27    }
28
29    #[must_use]
30    pub const fn classify(self, version: u32) -> Compatibility {
31        if version == self.current {
32            Compatibility::Current
33        } else if self.supports(version) {
34            Compatibility::Compatible
35        } else {
36            Compatibility::Unsupported
37        }
38    }
39}
40
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum Compatibility {
43    Current,
44    Compatible,
45    Unsupported,
46}
47
48pub const IR: VersionRange = VersionRange {
49    minimum: 7,
50    current: 11,
51};
52pub const RUNTIME_API: VersionRange = VersionRange::exact(3);
53pub const MODULE_API: VersionRange = VersionRange::exact(1);
54pub const PROJECT_LAYOUT: VersionRange = VersionRange {
55    minimum: 1,
56    current: 2,
57};
58pub const DATABASE_SCHEMA: VersionRange = VersionRange {
59    minimum: 1,
60    current: 2,
61};
62pub const OWNERSHIP_MANIFEST: VersionRange = VersionRange::exact(1);
63pub const CACHE_SCHEMA: VersionRange = VersionRange::exact(2);
64pub const TRANSACTION_JOURNAL: VersionRange = VersionRange::exact(1);
65
66pub const CONTRACT_MATRIX: &[(&str, VersionRange)] = &[
67    ("ir", IR),
68    ("runtime_api", RUNTIME_API),
69    ("module_api", MODULE_API),
70    ("project_layout", PROJECT_LAYOUT),
71    ("database_schema", DATABASE_SCHEMA),
72    ("ownership_manifest", OWNERSHIP_MANIFEST),
73    ("cache_schema", CACHE_SCHEMA),
74    ("transaction_journal", TRANSACTION_JOURNAL),
75];
76
77#[cfg(test)]
78mod tests {
79    use super::{CONTRACT_MATRIX, Compatibility};
80
81    #[test]
82    fn every_contract_classifies_current_legacy_and_future_versions() {
83        for (name, range) in CONTRACT_MATRIX {
84            assert_eq!(
85                range.classify(range.current),
86                Compatibility::Current,
87                "{name} current"
88            );
89            assert_eq!(
90                range.classify(range.current.saturating_add(1)),
91                Compatibility::Unsupported,
92                "{name} future"
93            );
94            if range.minimum < range.current {
95                assert_eq!(
96                    range.classify(range.minimum),
97                    Compatibility::Compatible,
98                    "{name} legacy"
99                );
100            }
101            if range.minimum > 0 {
102                assert_eq!(
103                    range.classify(range.minimum - 1),
104                    Compatibility::Unsupported,
105                    "{name} too old"
106                );
107            }
108        }
109    }
110}