tuwunel_core/utils/result/log_err.rs
1use std::fmt::Display;
2
3use tracing::Level;
4
5use super::Result;
6use crate::error;
7
8/// Logs display-formatted errors without changing their result.
9///
10/// Successful values pass through without producing a record. Errors remain
11/// available to the caller after being logged.
12pub trait LogErr<T, E: Display> {
13 /// Logs an error at `level` and returns the original result.
14 ///
15 /// The error is formatted with [`Display`]. Successful values produce no
16 /// log record.
17 #[must_use]
18 fn err_log(self, level: Level) -> Self;
19
20 /// Logs an error at the error tracing level.
21 ///
22 /// [`LogErr::err_log`] supplies this convenience form with
23 /// [`Level::ERROR`]. The original result is returned after inspection.
24 #[must_use]
25 fn log_err(self) -> Self
26 where
27 Self: Sized,
28 {
29 self.err_log(Level::ERROR)
30 }
31}
32
33impl<T, E: Display> LogErr<T, E> for Result<T, E> {
34 #[inline]
35 fn err_log(self, level: Level) -> Self {
36 self.inspect_err(|error| error::inspect_log_level(&error, level))
37 }
38}