1use super::{
4 ArithmeticRequirement, BoundaryKind, ComponentId, Endpoint, EndpointId, Precision,
5 RegimeSupport, RuntimeKind, RuntimeSupport, SourceKind,
6};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub struct OptionSet {
11 pub id: &'static str,
13 pub values: &'static [&'static str],
15 pub default: Option<&'static str>,
18}
19
20#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum UnsupportedReason {
24 RejectedByAdmission,
26 NotAttached,
28 OutsideModel,
30 Unqualified,
32}
33
34impl UnsupportedReason {
35 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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub struct Unsupported {
49 pub id: &'static str,
51 pub reason: UnsupportedReason,
53 pub summary: &'static str,
55 pub doc: &'static str,
57}
58
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub struct Limit {
62 pub id: &'static str,
64 pub summary: &'static str,
66 pub doc: &'static str,
68}
69
70#[derive(Clone, Copy, Debug, PartialEq, Eq)]
72pub struct Summary {
73 pub equations: &'static str,
75 pub method: &'static str,
77 pub assessment: &'static str,
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub struct ComponentCapabilities {
84 pub component: ComponentId,
86 pub contract: Option<&'static str>,
88 pub snapshot_schema: Option<&'static str>,
90 pub precision: &'static [Precision],
92 pub arithmetic: &'static [ArithmeticRequirement],
94 pub runtimes: &'static [RuntimeSupport],
96 pub endpoints: &'static [Endpoint],
98 pub regimes: &'static [RegimeSupport],
100 pub sources: &'static [SourceKind],
102 pub options: &'static [OptionSet],
104 pub nested: &'static [ComponentId],
106 pub attachable_to: &'static [ComponentId],
108 pub unsupported: &'static [Unsupported],
110 pub limits: &'static [Limit],
112 pub summary: Summary,
114}
115
116impl ComponentCapabilities {
117 pub fn endpoint(&self, id: EndpointId) -> Option<&'static Endpoint> {
119 self.endpoints.iter().find(|e| e.id == id)
120 }
121
122 pub fn admits(&self, id: EndpointId, kind: BoundaryKind) -> bool {
124 self.endpoint(id).is_some_and(|e| e.admits(kind))
125 }
126
127 pub fn runtime(&self, runtime: RuntimeKind) -> Option<&'static RuntimeSupport> {
129 self.runtimes.iter().find(|r| r.runtime == runtime)
130 }
131
132 pub fn option(&self, id: &str) -> Option<&'static OptionSet> {
134 self.options.iter().find(|o| o.id == id)
135 }
136
137 pub fn unsupported_by_id(&self, id: &str) -> Option<&'static Unsupported> {
139 self.unsupported.iter().find(|u| u.id == id)
140 }
141
142 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}