egui_pop_model/commands/
error.rs1use std::fmt;
5
6use serde::{Deserialize, Serialize};
7
8use crate::notice::{Notice, Origin as NoticeOrigin};
9
10#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
44#[non_exhaustive]
45pub struct CommandError {
46 pub headline: String,
49 #[serde(default, skip_serializing_if = "Option::is_none")]
51 pub reason: Option<String>,
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 pub hint: Option<String>,
55 #[serde(default, skip_serializing_if = "Option::is_none")]
57 pub details: Option<String>,
58 #[serde(default)]
61 pub user: bool,
62}
63
64fn some(text: impl Into<String>) -> Option<String> {
66 Some(text.into()).filter(|t| !t.trim().is_empty())
67}
68
69impl CommandError {
70 pub fn new(headline: impl Into<String>) -> Self {
72 Self {
73 headline: headline.into(),
74 user: true,
75 ..Self::default()
76 }
77 }
78
79 pub fn technical(text: impl Into<String>) -> Self {
81 Self {
82 headline: text.into(),
83 user: false,
84 ..Self::default()
85 }
86 }
87
88 pub fn from_error(err: &(dyn std::error::Error + 'static)) -> Self {
91 let mut chain = err.to_string();
92 let mut source = err.source();
93 while let Some(s) = source {
94 chain.push('\n');
95 chain.push_str(&s.to_string());
96 source = s.source();
97 }
98 let mut e = Self::technical(err.to_string());
99 if chain != e.headline {
100 e.details = Some(chain);
101 }
102 e
103 }
104
105 #[must_use]
107 pub fn reason(mut self, reason: impl Into<String>) -> Self {
108 self.reason = some(reason);
109 self
110 }
111
112 #[must_use]
114 pub fn hint(mut self, hint: impl Into<String>) -> Self {
115 self.hint = some(hint);
116 self
117 }
118
119 #[must_use]
121 pub fn details(mut self, details: impl Into<String>) -> Self {
122 self.details = some(details);
123 self
124 }
125
126 pub fn to_notice(&self, generic: impl Into<String>) -> Notice {
131 let n = if self.user {
132 let mut n = Notice::error(self.headline.clone());
133 if let Some(r) = &self.reason {
134 n = n.reason(r.clone());
135 }
136 if let Some(h) = &self.hint {
137 n = n.hint(h.clone());
138 }
139 if let Some(d) = &self.details {
140 n = n.details(d.clone());
141 }
142 n
143 } else {
144 let mut details = self.headline.clone();
145 if let Some(d) = self.details.as_ref().filter(|d| **d != self.headline) {
146 details.push('\n');
147 details.push_str(d);
148 }
149 let mut n = Notice::error(generic).details(details);
150 if let Some(r) = &self.reason {
151 n = n.reason(r.clone());
152 }
153 if let Some(h) = &self.hint {
154 n = n.hint(h.clone());
155 }
156 n
157 };
158 n.origin(NoticeOrigin::User)
159 }
160}
161
162impl fmt::Display for CommandError {
163 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
164 f.write_str(&self.headline)?;
165 if let Some(r) = &self.reason {
166 write!(f, ": {r}")?;
167 }
168 Ok(())
169 }
170}
171
172impl From<String> for CommandError {
173 fn from(text: String) -> Self {
174 Self::technical(text)
175 }
176}
177
178impl From<&str> for CommandError {
179 fn from(text: &str) -> Self {
180 Self::technical(text)
181 }
182}
183
184impl From<&String> for CommandError {
185 fn from(text: &String) -> Self {
186 Self::technical(text.clone())
187 }
188}
189
190impl From<std::io::Error> for CommandError {
191 fn from(err: std::io::Error) -> Self {
192 Self::from_error(&err)
193 }
194}
195
196impl From<serde_json::Error> for CommandError {
197 fn from(err: serde_json::Error) -> Self {
198 Self::from_error(&err)
199 }
200}
201
202impl From<Box<dyn std::error::Error + Send + Sync>> for CommandError {
203 fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
205 Self::from_error(&*err)
206 }
207}
208
209impl From<Box<dyn std::error::Error>> for CommandError {
210 fn from(err: Box<dyn std::error::Error>) -> Self {
211 Self::from_error(&*err)
212 }
213}
214
215impl From<CommandError> for String {
216 fn from(err: CommandError) -> Self {
218 err.to_string()
219 }
220}