egui_pop_model/
diagnostics.rs1use 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 Warning,
17 Error,
19}
20
21#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
23pub struct Diagnostic {
24 pub level: Level,
25 pub code: &'static str,
27 pub owner: Option<OwnerId>,
29 pub subject: String,
31 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#[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 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 pub fn push_quiet(&mut self, d: Diagnostic) {
66 self.items.push(d);
67 }
68
69 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 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 pub fn codes(&self) -> Vec<&'static str> {
117 self.items.iter().map(|d| d.code).collect()
118 }
119
120 pub fn take(&mut self) -> Vec<Diagnostic> {
122 std::mem::take(&mut self.items)
123 }
124
125 pub fn remove_owner(&mut self, owner: &OwnerId) {
127 self.items.retain(|d| d.owner.as_ref() != Some(owner));
128 }
129
130 pub fn extend(&mut self, other: impl IntoIterator<Item = Diagnostic>) {
132 self.items.extend(other);
133 }
134}