Skip to main content

ifc_geometry/rules/
violation.rs

1//! A where-rule violation, named the way the schema names it.
2
3use ifc_model::EntityId;
4
5/// What kind of schema condition was broken.
6///
7/// Kept separate from the message so a consumer can filter (
8/// "show me only degeneracies") without string matching.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum ViolationKind {
12    /// Coordinate-space mismatch, e.g. a 2D direction on a 3D placement.
13    Dimensionality,
14    /// The values describe something with no extent or no unique solution:
15    /// a zero-length direction, an axis parallel to its reference direction.
16    Degenerate,
17    /// Two things that must agree do not, e.g. boolean operands of
18    /// different dimensionality.
19    Disagreement,
20    /// A value outside the range the schema permits.
21    OutOfRange,
22    /// An attribute holds an entity type the select does not admit.
23    WrongType,
24}
25
26/// One broken `WHERE` rule.
27///
28/// Carries the schema's own rule name (`AxisToRefDirPosition`), not a
29/// paraphrase, so a user can look it up in the IFC documentation. That is the
30/// difference between a diagnostic someone can act on and one they can only
31/// report.
32#[derive(Debug, Clone, PartialEq, Eq)]
33#[non_exhaustive]
34pub struct RuleViolation {
35    /// The entity that violates the rule.
36    pub entity: EntityId,
37    /// Its IFC type, e.g. `IFCAXIS2PLACEMENT3D`.
38    pub type_name: String,
39    /// The rule's name in the EXPRESS schema.
40    pub rule: &'static str,
41    /// The class of problem.
42    pub kind: ViolationKind,
43    /// What specifically went wrong.
44    pub detail: String,
45}
46
47impl RuleViolation {
48    /// Build a violation.
49    pub fn new(
50        entity: EntityId,
51        type_name: impl Into<String>,
52        rule: &'static str,
53        kind: ViolationKind,
54        detail: impl Into<String>,
55    ) -> Self {
56        Self {
57            entity,
58            type_name: type_name.into(),
59            rule,
60            kind,
61            detail: detail.into(),
62        }
63    }
64}
65
66impl std::fmt::Display for RuleViolation {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        write!(
69            f,
70            "{} {} violates {}: {}",
71            self.entity, self.type_name, self.rule, self.detail
72        )
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    /// The message must name the schema rule so it can be looked up.
81    #[test]
82    fn display_names_the_schema_rule_and_the_entity() {
83        let v = RuleViolation::new(
84            EntityId(4711),
85            "IFCAXIS2PLACEMENT3D",
86            "AxisToRefDirPosition",
87            ViolationKind::Degenerate,
88            "Axis is parallel to RefDirection",
89        );
90        let text = v.to_string();
91        assert!(text.contains("#4711"), "{text}");
92        assert!(text.contains("AxisToRefDirPosition"), "{text}");
93        assert!(text.contains("parallel"), "{text}");
94    }
95}