use cadmpeg_ir::report::{LossKind, LossNote, LossTaxonomy, Severity};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SatLossCode {
GeometryFramedWithoutCarriers,
GeometryProceduralSurfaceUntyped,
ContainerAcisSaveFormatUnsupported,
}
impl SatLossCode {
pub const ALL: &'static [SatLossCode] = &[
Self::GeometryFramedWithoutCarriers,
Self::GeometryProceduralSurfaceUntyped,
Self::ContainerAcisSaveFormatUnsupported,
];
#[must_use]
pub const fn code(self) -> &'static str {
match self {
Self::GeometryFramedWithoutCarriers => "geometry.framed-without-carriers",
Self::GeometryProceduralSurfaceUntyped => "geometry.procedural-surface-untyped",
Self::ContainerAcisSaveFormatUnsupported => "container.acis-save-format-unsupported",
}
}
#[must_use]
pub const fn severity(self) -> Severity {
match self {
Self::GeometryFramedWithoutCarriers | Self::ContainerAcisSaveFormatUnsupported => {
Severity::Blocking
}
Self::GeometryProceduralSurfaceUntyped => Severity::Warning,
}
}
const fn shared_taxonomy(self) -> LossTaxonomy {
match self {
Self::GeometryFramedWithoutCarriers
| Self::GeometryProceduralSurfaceUntyped
| Self::ContainerAcisSaveFormatUnsupported => LossTaxonomy::GeometryNotTransferred,
}
}
const fn strict_floor(self) -> Option<Severity> {
self.shared_taxonomy().strict_floor()
}
#[must_use]
pub fn kind(self) -> LossKind {
LossKind::namespaced("sat", self.code(), self.shared_taxonomy())
.with_strict_floor(self.strict_floor())
}
#[must_use]
pub fn note(self, message: impl Into<String>) -> LossNote {
LossNote::new(self.kind(), message).with_severity(self.severity())
}
}
#[cfg(test)]
mod tests {
use super::SatLossCode;
use std::collections::BTreeSet;
#[test]
fn code_strings_are_pinned() {
let codes: Vec<&str> = SatLossCode::ALL.iter().map(|c| c.code()).collect();
assert_eq!(
codes,
[
"geometry.framed-without-carriers",
"geometry.procedural-surface-untyped",
"container.acis-save-format-unsupported",
]
);
}
#[test]
fn codes_are_unique_and_well_formed() {
let mut seen = BTreeSet::new();
for code in SatLossCode::ALL {
let text = code.code();
assert!(seen.insert(text), "duplicate code {text}");
let (family, detail) = text.split_once('.').expect("family.detail shape");
assert!(!family.is_empty() && !detail.is_empty());
assert!(
text.bytes()
.all(|b| b.is_ascii_lowercase() || b == b'.' || b == b'-'),
"code {text} is not lowercase kebab"
);
}
}
#[test]
fn note_takes_severity_from_the_code() {
for code in SatLossCode::ALL {
let note = code.note("x");
assert_eq!(note.severity, code.severity());
assert_eq!(note.message, "x");
assert!(note.provenance.is_none());
}
}
}