Skip to main content

treeboot_spec/
case.rs

1use serde::Serialize;
2
3/// The compatibility requirement exercised by a conformance case.
4#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum CaseRequirement {
7    /// Observable behavior that can remain compatible across specification versions.
8    #[default]
9    Functional,
10    /// Exact identity with this crate's specification version or canonical assets.
11    Exact,
12}
13
14/// Public metadata for one stable conformance case.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
16pub struct CaseMetadata {
17    id: &'static str,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    source_test: Option<&'static str>,
20    spec_references: &'static [&'static str],
21    #[serde(skip)]
22    requirement: CaseRequirement,
23}
24
25impl CaseMetadata {
26    pub(crate) const fn new(
27        id: &'static str,
28        source_test: &'static str,
29        spec_references: &'static [&'static str],
30    ) -> Self {
31        Self {
32            id,
33            source_test: Some(source_test),
34            spec_references,
35            requirement: CaseRequirement::Functional,
36        }
37    }
38
39    /// Returns the stable, dot-separated case identifier.
40    pub const fn id(self) -> &'static str {
41        self.id
42    }
43
44    /// Returns the Phase 1 source test key, or `None` for a closure case added
45    /// after the inventory audit.
46    pub const fn source_test(self) -> Option<&'static str> {
47        self.source_test
48    }
49
50    pub(crate) const fn closure(
51        id: &'static str,
52        spec_references: &'static [&'static str],
53    ) -> Self {
54        Self {
55            id,
56            source_test: None,
57            spec_references,
58            requirement: CaseRequirement::Functional,
59        }
60    }
61
62    pub(crate) const fn exact(id: &'static str, spec_references: &'static [&'static str]) -> Self {
63        Self {
64            id,
65            source_test: None,
66            spec_references,
67            requirement: CaseRequirement::Exact,
68        }
69    }
70
71    /// Returns specification section anchors or requirement identifiers.
72    pub const fn spec_references(self) -> &'static [&'static str] {
73        self.spec_references
74    }
75
76    /// Returns whether the case checks functional behavior or exact identity.
77    pub const fn requirement(self) -> CaseRequirement {
78        self.requirement
79    }
80}
81
82pub(crate) type CaseFn = fn();
83
84#[derive(Clone, Copy)]
85pub(crate) struct CaseDefinition {
86    pub metadata: CaseMetadata,
87    pub run: Option<CaseFn>,
88    pub skip_reason: Option<&'static str>,
89}
90
91impl CaseDefinition {
92    pub const fn new(metadata: CaseMetadata, run: CaseFn) -> Self {
93        Self {
94            metadata,
95            run: Some(run),
96            skip_reason: None,
97        }
98    }
99
100    pub const fn skipped(metadata: CaseMetadata, reason: &'static str) -> Self {
101        Self {
102            metadata,
103            run: None,
104            skip_reason: Some(reason),
105        }
106    }
107}