use super::{
ArithmeticRequirement, BoundaryKind, ComponentId, Endpoint, EndpointId, Precision,
RegimeSupport, RuntimeKind, RuntimeSupport, SourceKind,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OptionSet {
pub id: &'static str,
pub values: &'static [&'static str],
pub default: Option<&'static str>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum UnsupportedReason {
RejectedByAdmission,
NotAttached,
OutsideModel,
Unqualified,
}
impl UnsupportedReason {
pub fn id(&self) -> &'static str {
match self {
Self::RejectedByAdmission => "rejected-by-admission",
Self::NotAttached => "not-attached",
Self::OutsideModel => "outside-model",
Self::Unqualified => "unqualified",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Unsupported {
pub id: &'static str,
pub reason: UnsupportedReason,
pub summary: &'static str,
pub doc: &'static str,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Limit {
pub id: &'static str,
pub summary: &'static str,
pub doc: &'static str,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Summary {
pub equations: &'static str,
pub method: &'static str,
pub assessment: &'static str,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ComponentCapabilities {
pub component: ComponentId,
pub contract: Option<&'static str>,
pub snapshot_schema: Option<&'static str>,
pub precision: &'static [Precision],
pub arithmetic: &'static [ArithmeticRequirement],
pub runtimes: &'static [RuntimeSupport],
pub endpoints: &'static [Endpoint],
pub regimes: &'static [RegimeSupport],
pub sources: &'static [SourceKind],
pub options: &'static [OptionSet],
pub nested: &'static [ComponentId],
pub attachable_to: &'static [ComponentId],
pub unsupported: &'static [Unsupported],
pub limits: &'static [Limit],
pub summary: Summary,
}
impl ComponentCapabilities {
pub fn endpoint(&self, id: EndpointId) -> Option<&'static Endpoint> {
self.endpoints.iter().find(|e| e.id == id)
}
pub fn admits(&self, id: EndpointId, kind: BoundaryKind) -> bool {
self.endpoint(id).is_some_and(|e| e.admits(kind))
}
pub fn runtime(&self, runtime: RuntimeKind) -> Option<&'static RuntimeSupport> {
self.runtimes.iter().find(|r| r.runtime == runtime)
}
pub fn option(&self, id: &str) -> Option<&'static OptionSet> {
self.options.iter().find(|o| o.id == id)
}
pub fn unsupported_by_id(&self, id: &str) -> Option<&'static Unsupported> {
self.unsupported.iter().find(|u| u.id == id)
}
pub fn limit_by_id(&self, id: &str) -> Option<&'static Limit> {
self.limits.iter().find(|l| l.id == id)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::capability::RuntimeStatus;
use crate::capability::id_check::assert_ids;
const SAMPLE: ComponentCapabilities = ComponentCapabilities {
component: ComponentId::LevelPool,
contract: None,
snapshot_schema: None,
precision: &[Precision::F64],
arithmetic: &[ArithmeticRequirement::F64Ieee754Basic],
runtimes: &[RuntimeSupport {
runtime: RuntimeKind::Cpu,
status: RuntimeStatus::Reference,
evidence: "cpu_reference",
}],
endpoints: &[Endpoint {
id: EndpointId::Outlet,
admitted: &[BoundaryKind::SuppressedRectangularWeir],
}],
regimes: &[],
sources: &[SourceKind::LocalPoolRain],
options: &[OptionSet {
id: "step",
values: &["fixed", "adaptive"],
default: Some("fixed"),
}],
nested: &[],
attachable_to: &[ComponentId::SurfaceSolver],
unsupported: &[Unsupported {
id: "seepage",
reason: UnsupportedReason::OutsideModel,
summary: "No seepage.",
doc: "The pool holds no seepage term.",
}],
limits: &[Limit {
id: "max-stage",
summary: "Stage stays within the stage-volume table.",
doc: "The stage-volume table ends at its top row.",
}],
summary: Summary {
equations: "storage balance",
method: "explicit",
assessment: "sample",
},
};
#[test]
fn reason_ids_are_unique_kebab_case() {
let all = [
UnsupportedReason::RejectedByAdmission,
UnsupportedReason::NotAttached,
UnsupportedReason::OutsideModel,
UnsupportedReason::Unqualified,
];
assert_ids(&all.map(|r| r.id()));
}
#[test]
fn admits_needs_declared_endpoint_and_kind() {
assert!(SAMPLE.admits(EndpointId::Outlet, BoundaryKind::SuppressedRectangularWeir));
assert!(!SAMPLE.admits(EndpointId::Outlet, BoundaryKind::Wall));
assert!(!SAMPLE.admits(EndpointId::Lateral, BoundaryKind::SuppressedRectangularWeir));
}
#[test]
fn lookups_find_declared_entries_only() {
assert_eq!(
SAMPLE.endpoint(EndpointId::Outlet).map(|e| e.id),
Some(EndpointId::Outlet)
);
assert_eq!(SAMPLE.endpoint(EndpointId::ReachLeft), None);
assert_eq!(
SAMPLE.runtime(RuntimeKind::Cpu).map(|r| r.status),
Some(RuntimeStatus::Reference)
);
assert_eq!(SAMPLE.runtime(RuntimeKind::Wgpu), None);
assert_eq!(SAMPLE.option("step").and_then(|o| o.default), Some("fixed"));
assert_eq!(SAMPLE.option("missing"), None);
assert_eq!(
SAMPLE.unsupported_by_id("seepage").map(|u| u.reason),
Some(UnsupportedReason::OutsideModel)
);
assert_eq!(SAMPLE.unsupported_by_id("missing"), None);
assert_eq!(
SAMPLE.limit_by_id("max-stage").map(|l| l.id),
Some("max-stage")
);
assert_eq!(SAMPLE.limit_by_id("missing"), None);
}
}