Skip to main content

ifc_model/diagnostic/
damage.rs

1//! A model's reader diagnostics, classified for a resolution that reads a
2//! known set of records (#615, #684).
3//!
4//! Such a resolution (units, say) reads a handful of records through
5//! [`Model::get`] and refuses a missing one. A reader diagnostic matters to it
6//! only when it names one of those records, or when the diagnostic is of a
7//! kind this module does not classify:
8//!
9//! - a REAL without its decimal point and a dangling reference do not change
10//!   what a kept record says, so they are ignored;
11//! - a skipped record or a duplicate id is remembered by the id the reader
12//!   named, so a resolution can refuse exactly the record it reads;
13//! - a skipped record or duplicate with no id, and every other kind
14//!   (including kinds added later), cannot be shown harmless, so
15//!   [`RecordDamage::of`] refuses the model.
16//!
17//! The rule reads only the model's diagnostics and ids. It carries no IFC
18//! meaning and names no codec, so every domain crate shares it.
19
20use std::collections::BTreeMap;
21use std::fmt;
22
23use crate::{DiagnosticKind, EntityId, Model};
24
25/// A model's reader diagnostics, sorted into what a resolution must refuse.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct RecordDamage {
28    /// The position of the first diagnostic naming each reported record id.
29    reported: BTreeMap<EntityId, usize>,
30    /// Whether a record was dropped, so an absent record may have existed.
31    records_lost: bool,
32    /// Whether a skipped record names an id a kept record holds.
33    held_id_skipped: bool,
34    /// How many diagnostics the model carries.
35    diagnostics: usize,
36}
37
38/// A diagnostic [`RecordDamage::of`] cannot classify.
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct UnclassifiedDiagnostic {
42    /// Position in [`Model::diagnostics`].
43    pub index: usize,
44    /// The kind of the diagnostic.
45    pub kind: DiagnosticKind,
46}
47
48impl fmt::Display for UnclassifiedDiagnostic {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        write!(
51            f,
52            "diagnostic {} ({:?}) names no record a resolution can check",
53            self.index, self.kind
54        )
55    }
56}
57
58impl std::error::Error for UnclassifiedDiagnostic {}
59
60impl RecordDamage {
61    /// Classify `model`'s diagnostics by kind.
62    ///
63    /// # Errors
64    /// [`UnclassifiedDiagnostic`] for the first diagnostic of a kind other
65    /// than skipped record, duplicate id, dangling reference or REAL without
66    /// a point, and for a skipped record or duplicate id that names no
67    /// record.
68    pub fn of(model: &Model) -> Result<Self, UnclassifiedDiagnostic> {
69        let mut damage = Self {
70            reported: BTreeMap::new(),
71            records_lost: false,
72            held_id_skipped: false,
73            diagnostics: model.diagnostics().len(),
74        };
75        for (index, diagnostic) in model.diagnostics().iter().enumerate() {
76            let kind = diagnostic.kind();
77            let unclassified = UnclassifiedDiagnostic { index, kind };
78            match kind {
79                DiagnosticKind::RealWithoutPoint | DiagnosticKind::DanglingReference => {}
80                DiagnosticKind::SkippedRecord => {
81                    damage.records_lost = true;
82                    // The reader names the id it read, also when a kept
83                    // record holds it; such a record is reported. A skip
84                    // whose `#id` was not read may have held a duplicate of a
85                    // record the resolution reads.
86                    // A record hidden inside the skip's span by its own
87                    // damage is not named (#668).
88                    let id = diagnostic.instance().ok_or(unclassified)?;
89                    damage.held_id_skipped |= model.contains(id);
90                    damage.reported.entry(id).or_insert(index);
91                }
92                DiagnosticKind::DuplicateId => {
93                    let id = diagnostic.instance().ok_or(unclassified)?;
94                    damage.reported.entry(id).or_insert(index);
95                }
96                _ => return Err(unclassified),
97            }
98        }
99        Ok(damage)
100    }
101
102    /// Position in [`Model::diagnostics`] of the first diagnostic naming `id`.
103    #[must_use]
104    pub fn reported(&self, id: EntityId) -> Option<usize> {
105        self.reported.get(&id).copied()
106    }
107
108    /// Whether a record was skipped, so an absent record may have existed.
109    #[must_use]
110    pub fn records_lost(&self) -> bool {
111        self.records_lost
112    }
113
114    /// Whether a skipped record names an id a kept record holds. Such a skip
115    /// may have swallowed later records (#668).
116    #[must_use]
117    pub fn held_id_skipped(&self) -> bool {
118        self.held_id_skipped
119    }
120
121    /// How many diagnostics the model carries.
122    #[must_use]
123    pub fn diagnostics(&self) -> usize {
124        self.diagnostics
125    }
126}