comfy_core/errors.rs
1use crate::*;
2
3pub static ERRORS: Lazy<AtomicRefCell<Errors>> =
4 Lazy::new(|| AtomicRefCell::new(Errors::new()));
5
6pub struct Errors {
7 pub data: HashMap<Cow<'static, str>, Cow<'static, str>>,
8}
9
10impl Errors {
11 pub fn new() -> Self {
12 Self { data: HashMap::new() }
13 }
14}
15
16/// Stores an error message with the given ID. This is useful for immediate mode error reporting
17/// when you don't want to pollute the log on every frame.
18///
19/// When `--features dev` is active, errors will show in an egui window in game.
20///
21/// The `id` parameter can be any string. We're using `Cow<'static, str>` to save on allocations
22/// for ids/errors that can be represented as `&'static str`.
23pub fn report_error(
24 id: impl Into<Cow<'static, str>>,
25 error: impl Into<Cow<'static, str>>,
26) {
27 ERRORS.borrow_mut().data.insert(id.into(), error.into());
28}
29
30/// Clears a previously set error. Use the same ID as when calling `report_error`.
31pub fn clear_error(id: impl Into<Cow<'static, str>>) {
32 ERRORS.borrow_mut().data.remove(&id.into());
33}