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
/*!
[`FailureMode`]: FailureMode
[`LogLevelOverride`]: LogLevelOverride
[`Failure`]: Failure
[`quick_sysfail`]: quick_sysfail
[`sysfail`]: sysfail
*/
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#![warn(clippy::pedantic)]

use bevy_time::Time;
use bevy_utils::Duration;
use std::hash::Hash;

pub use bevy_mod_sysfail_macros::*;
pub use logged_errors::LoggedErrors;

mod logged_errors;

#[doc(hidden)]
pub mod __macro {
    pub type LoggedErrorsParam<'s, T> = bevy_ecs::prelude::Local<'s, LoggedErrors<T>>;
    pub type TimeParam<'s> = bevy_ecs::prelude::Res<'s, bevy_time::Time>;
    pub use crate::{Failure, LoggedErrors};
    pub use bevy_log::{debug, error, info, trace, warn};
}
/// The [`quick_sysfail`] and [`sysfail`] macros.
#[deprecated(
    since = "4.0.0",
    note = "Directly import the macros with bevy_mod_sysfail::{sysfail, quick_sysfail}"
)]
pub mod macros {
    pub use crate::{quick_sysfail, sysfail};
}
/// This crate's traits.
///
/// Useful if you need to use extension methods, for example on
/// [`LogLevelOverride`].
pub mod traits {
    #[allow(deprecated)]
    pub use crate::{Failure, FailureMode, LogLevelOverride};
}

/// Deprecated, `bevy_mod_sysfail` doesn't respect the runtime-set log level.
#[deprecated(since = "4.0.0", note = "runtime log levels are not respected anymore")]
pub struct OverrideLevel<T> {
    inner: T,
}
#[allow(deprecated)]
impl<T> OverrideLevel<T> {
    /// Log `inner`, but always with provided `level`.
    pub fn new(_: LogLevel, inner: T) -> Self {
        Self { inner }
    }
}
#[allow(deprecated)]
impl<T: FailureMode> FailureMode for OverrideLevel<T> {
    fn log_level(&self) -> LogLevel {
        LogLevel::Silent
    }
    type ID = T::ID;

    fn identify(&self) -> Self::ID {
        self.inner.identify()
    }
    fn display(&self) -> Option<String> {
        None
    }
    fn cooldown(&self) -> Duration {
        self.inner.cooldown()
    }
}

/// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
#[deprecated(since = "4.0.0", note = "runtime log levels are not respected anymore")]
#[allow(deprecated)]
pub trait LogLevelOverride: Sized {
    /// The type resulting from applying the override.
    type Output;

    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn set_level(self, level: LogLevel) -> Self::Output;

    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn silent(self) -> Self::Output {
        self.set_level(LogLevel::Silent)
    }
    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn trace(self) -> Self::Output {
        self.set_level(LogLevel::Trace)
    }
    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn debug(self) -> Self::Output {
        self.set_level(LogLevel::Debug)
    }
    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn info(self) -> Self::Output {
        self.set_level(LogLevel::Info)
    }
    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn warn(self) -> Self::Output {
        self.set_level(LogLevel::Warn)
    }
    /// Deprecated: This is completely ignored by `bevy_mod_sysfail`.
    fn error(self) -> Self::Output {
        self.set_level(LogLevel::Error)
    }
}
#[allow(deprecated)]
impl<T: FailureMode> LogLevelOverride for T {
    type Output = OverrideLevel<Self>;
    fn set_level(self, level: LogLevel) -> OverrideLevel<Self> {
        OverrideLevel::new(level, self)
    }
}
#[allow(deprecated)]
impl<T: FailureMode> LogLevelOverride for Result<(), T> {
    type Output = Result<(), OverrideLevel<T>>;
    fn set_level(self, level: LogLevel) -> Self::Output {
        self.map_err(|err| err.set_level(level))
    }
}

/// Deprecated: It is now completely unused.
#[deprecated(since = "4.0.0", note = "This is unusued and serves no purpose")]
#[derive(Debug, Clone, Copy)]
pub enum LogLevel {
    /// Never log anything.
    Silent,
    /// `trace!`
    Trace,
    /// `debug!`
    Debug,
    /// `info!`
    Info,
    /// `warn!`
    Warn,
    /// `error!`
    Error,
}

/// Something that can be logged in a [`sysfail`] handler
pub trait FailureMode {
    /// Deprecated: this is ignored.
    #[deprecated(since = "4.0.0", note = "This is ignored")]
    #[allow(deprecated)]
    fn log_level(&self) -> LogLevel {
        LogLevel::Error
    }

    /// How long an error must not be produced in order to be displayed again.
    ///
    /// This controls when [`Failure::get_error`] returns an error.
    ///
    /// Return `Duration::ZERO` to trigger [`Failure::get_error`] each time there
    /// is an error.
    fn cooldown(&self) -> Duration {
        Duration::from_secs(1)
    }

    /// Used to de-duplicate identical messages to avoid spamming the log.
    type ID: Hash + Eq;

    /// What constitutes "distinct" error types.
    fn identify(&self) -> Self::ID;

    /// Deprecated, this does nothing.
    #[deprecated(
        since = "4.0.0",
        note = "This crate now directly uses the fmt::Display impl on the error."
    )]
    fn display(&self) -> Option<String> {
        None
    }

    /// What happens, by default this logs based on the return value of
    /// [`FailureMode::log_level`].
    ///
    /// # Examples
    ///
    /// <https://crates.io/crates/bevy_debug_text_overlay> provides a wrapper
    /// struct to replace log-printing with displaying errors on screen.
    fn log(&self) {}
}

impl FailureMode for () {
    #[allow(deprecated)]
    fn log_level(&self) -> LogLevel {
        LogLevel::Silent
    }
    type ID = Self;
    fn identify(&self) {}
    fn display(&self) -> Option<String> {
        None
    }
}
impl FailureMode for &'static str {
    #[allow(deprecated)]
    fn log_level(&self) -> LogLevel {
        LogLevel::Warn
    }
    type ID = Self;
    fn identify(&self) -> Self {
        self
    }
    fn display(&self) -> Option<String> {
        Some((*self).to_string())
    }
}
impl FailureMode for Box<dyn std::error::Error> {
    #[allow(deprecated)]
    fn log_level(&self) -> LogLevel {
        LogLevel::Warn
    }
    type ID = ();
    /// By default, only print a single error per system.
    fn identify(&self) {}
    fn display(&self) -> Option<String> {
        Some(self.to_string())
    }
}
impl FailureMode for anyhow::Error {
    #[allow(deprecated)]
    fn log_level(&self) -> LogLevel {
        LogLevel::Warn
    }
    type ID = ();
    /// By default, only print a single error per system.
    fn identify(&self) {}
    fn display(&self) -> Option<String> {
        Some(self.to_string())
    }
}

/// Something that can be returned by a function marked with `#[sysfail(log)]`.
pub trait Failure {
    /// The actual error's type in this failure.
    type Error: FailureMode;

    /// The actual error in this failure, None if the failure isn't a failure.
    fn failure(self) -> Option<Self::Error>;

    /// Get the error if it is ready to be logged.
    fn get_error(self, time: &Time, logged_errors: &mut LoggedErrors<Self>) -> Option<Self::Error>
    where
        Self: Sized,
    {
        let error = self.failure()?;
        let cooldown = error.cooldown();
        let now = time.elapsed();
        let last_shown = logged_errors.0.insert(error.identify(), now);
        let should_log = last_shown.map_or(true, |d| now < d + cooldown);
        should_log.then_some(error)
    }
}
impl<T: FailureMode> Failure for Result<(), T> {
    type Error = T;

    fn failure(self) -> Option<Self::Error> {
        self.err()
    }
}
impl Failure for Option<()> {
    type Error = &'static str;

    fn failure(self) -> Option<Self::Error> {
        Some("A none value")
    }
}