axioval-ir 0.3.0

Source-neutral semantic IR and validation result contracts for Axioval
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Reviewers' decisions about findings, kept across re-checks.
//!
//! A reviewer accepts or rejects a finding, with a comment. The decision is
//! keyed by the finding's [`FindingId`], so checking a revised model carries
//! it over to the finding with the same identity. A decision whose finding is
//! gone is *stale* and listed apart; a decision whose finding is still there
//! but whose evidence changed is flagged, where the change can be told.
//!
//! Decisions only mark findings. They never remove a finding, never change
//! its severity, and never touch a not-evaluated outcome: an outcome that
//! could not be evaluated is not a finding anybody decided.
//!
//! # What counts as changed
//!
//! A decision may record its [`DecisionBasis`]: the rule, message, severity
//! and the number of evidence items, exact and inexact, of the finding when
//! it was decided. A re-check compares those with the finding now:
//!
//! - rule and message are part of the identity, so they differ only for a
//!   decision recorded against something else;
//! - the severity changes when a revised model is graded into another band
//!   or another override holds;
//! - the evidence count changes when the finding is decided by more or fewer
//!   facts, and the inexact count when facts become approximate or exact.
//!
//! Evidence *locators* are not compared: a source numbers its entities anew
//! on every export, so a locator changes with no change to the model, and
//! comparing them would flag every decision after a re-export. A changed
//! measured value is not missed either: it is written into the message, so
//! the finding gets a new identity and the old decision becomes stale.
//! Without a basis nothing can be compared, and the decision says so
//! ([`EvidenceCheck::Unknown`]) instead of claiming the evidence unchanged.

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{DateTime, Finding, FindingId, Report, RuleId, Severity};

/// What the reviewer decided.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DecisionStatus {
    /// Seen, not decided yet.
    Open,
    /// The finding stands and the reviewer accepts it.
    Accepted,
    /// The reviewer rejects the finding.
    Rejected,
}

impl DecisionStatus {
    /// `open`, `accepted` or `rejected`, as written on the wire.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Open => "open",
            Self::Accepted => "accepted",
            Self::Rejected => "rejected",
        }
    }
}

/// What a finding looked like when it was decided.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DecisionBasis {
    pub rule_id: RuleId,
    pub message: String,
    pub severity: Severity,
    /// How many evidence items the finding cited.
    pub evidence: usize,
    /// How many of them were inexact.
    pub inexact_evidence: usize,
}

impl DecisionBasis {
    /// The basis of a decision about `finding` as it is now.
    #[must_use]
    pub fn of(finding: &Finding) -> Self {
        Self {
            rule_id: finding.rule_id.clone(),
            message: finding.message.clone(),
            severity: finding.severity.clone(),
            evidence: finding.evidence.len(),
            inexact_evidence: finding.evidence.iter().filter(|e| !e.exact).count(),
        }
    }

    /// What differs between this basis and `finding` now, in facet order.
    #[must_use]
    pub fn changes(&self, finding: &Finding) -> Vec<DecisionChange> {
        let now = Self::of(finding);
        let mut changes = Vec::new();
        let mut differ = |facet, decided: String, now: String| {
            if decided != now {
                changes.push(DecisionChange {
                    facet,
                    decided,
                    now,
                });
            }
        };
        differ(
            ChangedFacet::Rule,
            self.rule_id.to_string(),
            now.rule_id.to_string(),
        );
        differ(ChangedFacet::Message, self.message.clone(), now.message);
        differ(
            ChangedFacet::Severity,
            severity(&self.severity).to_owned(),
            severity(&now.severity).to_owned(),
        );
        differ(
            ChangedFacet::Evidence,
            self.evidence.to_string(),
            now.evidence.to_string(),
        );
        differ(
            ChangedFacet::InexactEvidence,
            self.inexact_evidence.to_string(),
            now.inexact_evidence.to_string(),
        );
        changes
    }
}

fn severity(severity: &Severity) -> &'static str {
    match severity {
        Severity::Error => "error",
        Severity::Warning => "warning",
        Severity::Info => "info",
    }
}

/// One reviewer's decision about one finding.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Decision {
    /// The identity of the finding decided about.
    pub finding: FindingId,
    pub status: DecisionStatus,
    /// Who decided; never blank.
    pub author: String,
    /// When.
    pub date: DateTime,
    /// Why; may be empty, and is then absent on the wire.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub comment: String,
    /// The finding as it was decided, when recorded.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub basis: Option<DecisionBasis>,
}

impl Decision {
    /// A decision without a comment or basis.
    ///
    /// # Errors
    ///
    /// [`DecisionError::BlankAuthor`] when `author` is blank.
    pub fn new(
        finding: FindingId,
        status: DecisionStatus,
        author: impl Into<String>,
        date: DateTime,
    ) -> Result<Self, DecisionError> {
        let author = author.into();
        if author.trim().is_empty() {
            return Err(DecisionError::BlankAuthor(finding));
        }
        Ok(Self {
            finding,
            status,
            author,
            date,
            comment: String::new(),
            basis: None,
        })
    }

    /// The same decision with a comment.
    #[must_use]
    pub fn with_comment(mut self, comment: impl Into<String>) -> Self {
        self.comment = comment.into();
        self
    }

    /// The same decision recording `finding` as its basis.
    #[must_use]
    pub fn with_basis(mut self, finding: &Finding) -> Self {
        self.basis = Some(DecisionBasis::of(finding));
        self
    }
}

/// Why decisions were refused.
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum DecisionError {
    /// Two decisions name one finding, so which holds is ambiguous.
    #[error("two decisions name finding {0}")]
    Duplicate(FindingId),
    /// A decision names nobody who made it.
    #[error("the decision about finding {0} has a blank author")]
    BlankAuthor(FindingId),
    /// A finding of the report has no identity, so no decision can be
    /// matched to it; see [`Report::identify_findings`].
    #[error("finding of rule {0} has no identity; identify the report's findings first")]
    Unidentified(RuleId),
}

/// A set of decisions, at most one per finding, ordered by finding identity.
///
/// On the wire an object with one field, `decisions`, so a file can gain
/// fields later without breaking readers of this one.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "DecisionsWire", into = "DecisionsWire")]
pub struct Decisions {
    decisions: Vec<Decision>,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct DecisionsWire {
    decisions: Vec<Decision>,
}

impl TryFrom<DecisionsWire> for Decisions {
    type Error = DecisionError;
    fn try_from(wire: DecisionsWire) -> Result<Self, DecisionError> {
        Self::new(wire.decisions)
    }
}

impl From<Decisions> for DecisionsWire {
    fn from(decisions: Decisions) -> Self {
        Self {
            decisions: decisions.decisions,
        }
    }
}

impl Decisions {
    /// The decisions, ordered by finding.
    ///
    /// # Errors
    ///
    /// [`DecisionError::Duplicate`] when two name one finding, and
    /// [`DecisionError::BlankAuthor`] when one has a blank author.
    pub fn new(decisions: impl IntoIterator<Item = Decision>) -> Result<Self, DecisionError> {
        let mut decisions: Vec<Decision> = decisions.into_iter().collect();
        decisions.sort_by_key(|decision| decision.finding);
        for pair in decisions.windows(2) {
            if pair[0].finding == pair[1].finding {
                return Err(DecisionError::Duplicate(pair[0].finding));
            }
        }
        if let Some(blank) = decisions.iter().find(|d| d.author.trim().is_empty()) {
            return Err(DecisionError::BlankAuthor(blank.finding));
        }
        Ok(Self { decisions })
    }

    /// Every decision, ordered by finding.
    #[must_use]
    pub fn decisions(&self) -> &[Decision] {
        &self.decisions
    }

    /// Whether there are none.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.decisions.is_empty()
    }

    /// The decision about `finding`, if any.
    #[must_use]
    pub fn get(&self, finding: FindingId) -> Option<&Decision> {
        self.decisions
            .binary_search_by_key(&finding, |decision| decision.finding)
            .ok()
            .map(|at| &self.decisions[at])
    }

    /// Records `decision`, replacing any earlier one about its finding, and
    /// returns the replaced one.
    ///
    /// # Errors
    ///
    /// [`DecisionError::BlankAuthor`] when its author is blank.
    pub fn record(&mut self, decision: Decision) -> Result<Option<Decision>, DecisionError> {
        if decision.author.trim().is_empty() {
            return Err(DecisionError::BlankAuthor(decision.finding));
        }
        match self
            .decisions
            .binary_search_by_key(&decision.finding, |existing| existing.finding)
        {
            Ok(at) => Ok(Some(std::mem::replace(&mut self.decisions[at], decision))),
            Err(at) => {
                self.decisions.insert(at, decision);
                Ok(None)
            }
        }
    }
}

/// Whether a finding's evidence changed since it was decided.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceCheck {
    /// The decision's basis matches the finding.
    Unchanged,
    /// Something the basis records differs; see the decision's changes.
    Changed,
    /// The decision records no basis, so nothing could be compared.
    Unknown,
}

/// What differs between a decision's basis and its finding now.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ChangedFacet {
    Rule,
    Message,
    Severity,
    /// The number of evidence items.
    Evidence,
    /// The number of inexact evidence items.
    InexactEvidence,
}

impl ChangedFacet {
    /// `rule`, `message`, `severity`, `evidence` or `inexact_evidence`.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Rule => "rule",
            Self::Message => "message",
            Self::Severity => "severity",
            Self::Evidence => "evidence",
            Self::InexactEvidence => "inexact_evidence",
        }
    }
}

/// One facet that differs, as decided and as now.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DecisionChange {
    pub facet: ChangedFacet,
    pub decided: String,
    pub now: String,
}

/// The decision carried over to a finding of a report.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FindingDecision {
    pub status: DecisionStatus,
    pub author: String,
    pub date: DateTime,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub comment: String,
    /// Whether the finding changed since it was decided.
    pub evidence: EvidenceCheck,
    /// What changed, when [`EvidenceCheck::Changed`]; empty otherwise.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub changes: Vec<DecisionChange>,
}

impl FindingDecision {
    /// `decision` as it applies to `finding` now.
    #[must_use]
    pub fn carried(decision: &Decision, finding: &Finding) -> Self {
        let (evidence, changes) = match &decision.basis {
            None => (EvidenceCheck::Unknown, Vec::new()),
            Some(basis) => {
                let changes = basis.changes(finding);
                if changes.is_empty() {
                    (EvidenceCheck::Unchanged, changes)
                } else {
                    (EvidenceCheck::Changed, changes)
                }
            }
        };
        Self {
            status: decision.status,
            author: decision.author.clone(),
            date: decision.date,
            comment: decision.comment.clone(),
            evidence,
            changes,
        }
    }
}

impl Report {
    /// Carries `decisions` over to this report's findings.
    ///
    /// Each finding whose identity a decision names is marked with it
    /// ([`Finding::decision`]); every other finding is left undecided.
    /// Decisions naming no finding are listed in
    /// [`Report::stale_decisions`], by finding identity. Decisions applied
    /// before are replaced. Not-evaluated outcomes are never touched.
    ///
    /// # Errors
    ///
    /// [`DecisionError::Unidentified`] when there are decisions and a finding
    /// has no identity: a decision about it could not be told apart from a
    /// stale one. Nothing is changed then.
    pub fn apply_decisions(&mut self, decisions: &Decisions) -> Result<(), DecisionError> {
        if !decisions.is_empty()
            && let Some(unidentified) = self.findings.iter().find(|f| f.id.is_none())
        {
            return Err(DecisionError::Unidentified(unidentified.rule_id.clone()));
        }
        let mut matched = vec![false; decisions.decisions().len()];
        for finding in &mut self.findings {
            let at = finding.id.and_then(|id| {
                decisions
                    .decisions()
                    .binary_search_by_key(&id, |decision| decision.finding)
                    .ok()
            });
            finding.decision = at.map(|at| {
                matched[at] = true;
                FindingDecision::carried(&decisions.decisions()[at], finding)
            });
        }
        self.stale_decisions = decisions
            .decisions()
            .iter()
            .zip(matched)
            .filter(|(_, matched)| !matched)
            .map(|(decision, _)| decision.clone())
            .collect();
        Ok(())
    }
}