Skip to main content

hydr_core/capability/
declaration.rs

1//! Component capability declaration and its scope records.
2
3use super::{
4    ArithmeticRequirement, BoundaryKind, ComponentId, Endpoint, EndpointId, Precision,
5    RegimeSupport, RuntimeKind, RuntimeSupport, SourceKind,
6};
7
8/// Named option with its accepted values and default.
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct OptionSet {
11    /// Stable kebab-case option id.
12    pub id: &'static str,
13    /// Accepted values.
14    pub values: &'static [&'static str],
15    /// Value used when the caller selects none, or `None` when the caller must choose.
16    /// A `Some` value is one of `values`.
17    pub default: Option<&'static str>,
18}
19
20/// Reason a component does not support a capability.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum UnsupportedReason {
24    /// Admission refuses the input with an error.
25    RejectedByAdmission,
26    /// The component has no attachment point for it.
27    NotAttached,
28    /// The physics lies outside the component's model.
29    OutsideModel,
30    /// The code runs it, but no test qualifies the result.
31    Unqualified,
32}
33
34impl UnsupportedReason {
35    /// Returns the stable kebab-case id. Persisted decisions store it, so it never changes.
36    pub fn id(&self) -> &'static str {
37        match self {
38            Self::RejectedByAdmission => "rejected-by-admission",
39            Self::NotAttached => "not-attached",
40            Self::OutsideModel => "outside-model",
41            Self::Unqualified => "unqualified",
42        }
43    }
44}
45
46/// Capability that a component does not support.
47#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub struct Unsupported {
49    /// Stable kebab-case id.
50    pub id: &'static str,
51    /// Why the component does not support it.
52    pub reason: UnsupportedReason,
53    /// One-line description.
54    pub summary: &'static str,
55    /// Repository-relative path of the doc that explains it.
56    pub doc: &'static str,
57}
58
59/// Boundary of a supported capability's valid range.
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub struct Limit {
62    /// Stable kebab-case id.
63    pub id: &'static str,
64    /// One-line description.
65    pub summary: &'static str,
66    /// Repository-relative path of the doc that explains it.
67    pub doc: &'static str,
68}
69
70/// Plain-text description of a component's model.
71#[derive(Clone, Copy, Debug, PartialEq, Eq)]
72pub struct Summary {
73    /// Governing equations.
74    pub equations: &'static str,
75    /// Numerical method.
76    pub method: &'static str,
77    /// Scope of the evidence behind the declaration.
78    pub assessment: &'static str,
79}
80
81/// Everything one component admits, refuses and limits.
82#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub struct ComponentCapabilities {
84    /// Declaring component.
85    pub component: ComponentId,
86    /// Public numerical contract version, when the component has one.
87    pub contract: Option<&'static str>,
88    /// Snapshot schema identity, when the component writes snapshots.
89    pub snapshot_schema: Option<&'static str>,
90    /// Supported state precisions.
91    pub precision: &'static [Precision],
92    /// Arithmetic properties the runtime must provide.
93    pub arithmetic: &'static [ArithmeticRequirement],
94    /// Standing on each runtime.
95    pub runtimes: &'static [RuntimeSupport],
96    /// Endpoints and their admitted boundary kinds.
97    pub endpoints: &'static [Endpoint],
98    /// Supported regimes per scope.
99    pub regimes: &'static [RegimeSupport],
100    /// Admitted local sources and sinks.
101    pub sources: &'static [SourceKind],
102    /// Options and their accepted values.
103    pub options: &'static [OptionSet],
104    /// Components that this component contains.
105    pub nested: &'static [ComponentId],
106    /// Components that this component attaches to.
107    pub attachable_to: &'static [ComponentId],
108    /// Capabilities the component does not support.
109    pub unsupported: &'static [Unsupported],
110    /// Limits of the supported range.
111    pub limits: &'static [Limit],
112    /// Model description.
113    pub summary: Summary,
114}
115
116impl ComponentCapabilities {
117    /// Returns the declared endpoint with identity `id`.
118    pub fn endpoint(&self, id: EndpointId) -> Option<&'static Endpoint> {
119        self.endpoints.iter().find(|e| e.id == id)
120    }
121
122    /// Returns `true` when endpoint `id` is declared and admits `kind`.
123    pub fn admits(&self, id: EndpointId, kind: BoundaryKind) -> bool {
124        self.endpoint(id).is_some_and(|e| e.admits(kind))
125    }
126
127    /// Returns the declared standing on `runtime`.
128    pub fn runtime(&self, runtime: RuntimeKind) -> Option<&'static RuntimeSupport> {
129        self.runtimes.iter().find(|r| r.runtime == runtime)
130    }
131
132    /// Returns the option with id `id`.
133    pub fn option(&self, id: &str) -> Option<&'static OptionSet> {
134        self.options.iter().find(|o| o.id == id)
135    }
136
137    /// Returns the unsupported entry with id `id`.
138    pub fn unsupported_by_id(&self, id: &str) -> Option<&'static Unsupported> {
139        self.unsupported.iter().find(|u| u.id == id)
140    }
141
142    /// Returns the limit with id `id`.
143    pub fn limit_by_id(&self, id: &str) -> Option<&'static Limit> {
144        self.limits.iter().find(|l| l.id == id)
145    }
146}
147
148#[cfg(test)]
149mod tests {
150    use super::*;
151    use crate::capability::RuntimeStatus;
152    use crate::capability::id_check::assert_ids;
153
154    const SAMPLE: ComponentCapabilities = ComponentCapabilities {
155        component: ComponentId::LevelPool,
156        contract: None,
157        snapshot_schema: None,
158        precision: &[Precision::F64],
159        arithmetic: &[ArithmeticRequirement::F64Ieee754Basic],
160        runtimes: &[RuntimeSupport {
161            runtime: RuntimeKind::Cpu,
162            status: RuntimeStatus::Reference,
163            evidence: "cpu_reference",
164        }],
165        endpoints: &[Endpoint {
166            id: EndpointId::Outlet,
167            admitted: &[BoundaryKind::SuppressedRectangularWeir],
168        }],
169        regimes: &[],
170        sources: &[SourceKind::LocalPoolRain],
171        options: &[OptionSet {
172            id: "step",
173            values: &["fixed", "adaptive"],
174            default: Some("fixed"),
175        }],
176        nested: &[],
177        attachable_to: &[ComponentId::SurfaceSolver],
178        unsupported: &[Unsupported {
179            id: "seepage",
180            reason: UnsupportedReason::OutsideModel,
181            summary: "No seepage.",
182            doc: "The pool holds no seepage term.",
183        }],
184        limits: &[Limit {
185            id: "max-stage",
186            summary: "Stage stays within the stage-volume table.",
187            doc: "The stage-volume table ends at its top row.",
188        }],
189        summary: Summary {
190            equations: "storage balance",
191            method: "explicit",
192            assessment: "sample",
193        },
194    };
195
196    #[test]
197    fn reason_ids_are_unique_kebab_case() {
198        let all = [
199            UnsupportedReason::RejectedByAdmission,
200            UnsupportedReason::NotAttached,
201            UnsupportedReason::OutsideModel,
202            UnsupportedReason::Unqualified,
203        ];
204        assert_ids(&all.map(|r| r.id()));
205    }
206
207    #[test]
208    fn admits_needs_declared_endpoint_and_kind() {
209        assert!(SAMPLE.admits(EndpointId::Outlet, BoundaryKind::SuppressedRectangularWeir));
210        assert!(!SAMPLE.admits(EndpointId::Outlet, BoundaryKind::Wall));
211        assert!(!SAMPLE.admits(EndpointId::Lateral, BoundaryKind::SuppressedRectangularWeir));
212    }
213
214    #[test]
215    fn lookups_find_declared_entries_only() {
216        assert_eq!(
217            SAMPLE.endpoint(EndpointId::Outlet).map(|e| e.id),
218            Some(EndpointId::Outlet)
219        );
220        assert_eq!(SAMPLE.endpoint(EndpointId::ReachLeft), None);
221        assert_eq!(
222            SAMPLE.runtime(RuntimeKind::Cpu).map(|r| r.status),
223            Some(RuntimeStatus::Reference)
224        );
225        assert_eq!(SAMPLE.runtime(RuntimeKind::Wgpu), None);
226        assert_eq!(SAMPLE.option("step").and_then(|o| o.default), Some("fixed"));
227        assert_eq!(SAMPLE.option("missing"), None);
228        assert_eq!(
229            SAMPLE.unsupported_by_id("seepage").map(|u| u.reason),
230            Some(UnsupportedReason::OutsideModel)
231        );
232        assert_eq!(SAMPLE.unsupported_by_id("missing"), None);
233        assert_eq!(
234            SAMPLE.limit_by_id("max-stage").map(|l| l.id),
235            Some("max-stage")
236        );
237        assert_eq!(SAMPLE.limit_by_id("missing"), None);
238    }
239}