Skip to main content

egui_pop_model/
diagnostics.rs

1//! Diagnostics (ADR 0003): what was ignored or changed during
2//! registration, with what happens instead. Each one is logged through the
3//! `log` facade and kept in a list tests and the debug bridge read.
4
5use std::fmt;
6
7use serde::Serialize;
8
9use crate::ids::OwnerId;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
12#[serde(rename_all = "lowercase")]
13pub enum Level {
14    /// Something was adjusted; the result is still what was most likely
15    /// meant.
16    Warning,
17    /// Something was refused or not shown.
18    Error,
19}
20
21/// One diagnostic.
22#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
23pub struct Diagnostic {
24    pub level: Level,
25    /// A stable code tests match on: `duplicate-id`, `unknown-zone`...
26    pub code: &'static str,
27    /// Who registered the thing concerned.
28    pub owner: Option<OwnerId>,
29    /// What it concerns: an id, a path.
30    pub subject: String,
31    /// What happened and what happens instead.
32    pub message: String,
33}
34
35impl fmt::Display for Diagnostic {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        if let Some(o) = &self.owner {
38            write!(f, "[{o}] ")?;
39        }
40        write!(f, "{} ({}): {}", self.subject, self.code, self.message)
41    }
42}
43
44/// The diagnostics collected so far.
45#[derive(Clone, Debug, Default, Serialize)]
46pub struct Diagnostics {
47    items: Vec<Diagnostic>,
48}
49
50impl Diagnostics {
51    pub fn new() -> Self {
52        Self::default()
53    }
54
55    /// Records and logs a diagnostic.
56    pub fn push(&mut self, d: Diagnostic) {
57        match d.level {
58            Level::Warning => log::warn!(target: "egui_pop", "{d}"),
59            Level::Error => log::error!(target: "egui_pop", "{d}"),
60        }
61        self.items.push(d);
62    }
63
64    /// Records a diagnostic already logged (moved between lists).
65    pub fn push_quiet(&mut self, d: Diagnostic) {
66        self.items.push(d);
67    }
68
69    /// Records a warning.
70    pub fn warn(
71        &mut self,
72        code: &'static str,
73        owner: Option<&OwnerId>,
74        subject: impl Into<String>,
75        message: impl Into<String>,
76    ) {
77        self.push(Diagnostic {
78            level: Level::Warning,
79            code,
80            owner: owner.cloned(),
81            subject: subject.into(),
82            message: message.into(),
83        });
84    }
85
86    /// Records an error.
87    pub fn error(
88        &mut self,
89        code: &'static str,
90        owner: Option<&OwnerId>,
91        subject: impl Into<String>,
92        message: impl Into<String>,
93    ) {
94        self.push(Diagnostic {
95            level: Level::Error,
96            code,
97            owner: owner.cloned(),
98            subject: subject.into(),
99            message: message.into(),
100        });
101    }
102
103    pub fn all(&self) -> &[Diagnostic] {
104        &self.items
105    }
106
107    pub fn is_empty(&self) -> bool {
108        self.items.is_empty()
109    }
110
111    pub fn len(&self) -> usize {
112        self.items.len()
113    }
114
115    /// The codes, in order: what tests usually compare.
116    pub fn codes(&self) -> Vec<&'static str> {
117        self.items.iter().map(|d| d.code).collect()
118    }
119
120    /// Takes every diagnostic out.
121    pub fn take(&mut self) -> Vec<Diagnostic> {
122        std::mem::take(&mut self.items)
123    }
124
125    /// Forgets the diagnostics of one owner (it was removed).
126    pub fn remove_owner(&mut self, owner: &OwnerId) {
127        self.items.retain(|d| d.owner.as_ref() != Some(owner));
128    }
129
130    /// Records diagnostics already logged.
131    pub fn extend(&mut self, other: impl IntoIterator<Item = Diagnostic>) {
132        self.items.extend(other);
133    }
134}