Skip to main content

tuwunel_core/utils/result/
inspect_log.rs

1use std::fmt;
2
3use tracing::Level;
4
5use super::Result;
6use crate::error;
7
8/// Logs display-formatted errors while preserving their result.
9///
10/// Successful values pass through without producing a log record. Error
11/// values are inspected at the requested tracing level and remain unchanged.
12pub trait ErrLog<T, E>
13where
14	E: fmt::Display,
15{
16	/// Logs an error at `level` and returns the original result.
17	///
18	/// The error is formatted with [`fmt::Display`]. An `Ok` value produces no
19	/// record and is returned unchanged.
20	#[must_use]
21	fn log_err(self, level: Level) -> Self;
22
23	/// Logs an error at the error tracing level.
24	///
25	/// [`ErrLog::log_err`] supplies this convenience form with
26	/// [`Level::ERROR`]. The original result is returned after inspection.
27	#[inline]
28	#[must_use]
29	fn err_log(self) -> Self
30	where
31		Self: Sized,
32	{
33		self.log_err(Level::ERROR)
34	}
35}
36
37/// Logs debug-formatted errors while preserving their result.
38///
39/// Successful values pass through without producing a log record. Error
40/// values are inspected at the requested tracing level and remain unchanged.
41pub trait ErrDebugLog<T, E>
42where
43	E: fmt::Debug,
44{
45	/// Logs an error at `level` and returns the original result.
46	///
47	/// The error is formatted with [`fmt::Debug`]. An `Ok` value produces no
48	/// record and is returned unchanged.
49	#[must_use]
50	fn log_err_debug(self, level: Level) -> Self;
51
52	/// Logs a debug-formatted error at the error tracing level.
53	///
54	/// [`ErrDebugLog::log_err_debug`] supplies this convenience form with
55	/// [`Level::ERROR`]. The original result is returned after inspection.
56	#[inline]
57	#[must_use]
58	fn err_debug_log(self) -> Self
59	where
60		Self: Sized,
61	{
62		self.log_err_debug(Level::ERROR)
63	}
64}
65
66impl<T, E> ErrLog<T, E> for Result<T, E>
67where
68	E: fmt::Display,
69{
70	#[inline]
71	fn log_err(self, level: Level) -> Self
72	where
73		Self: Sized,
74	{
75		self.inspect_err(|error| error::inspect_log_level(&error, level))
76	}
77}
78
79impl<T, E> ErrDebugLog<T, E> for Result<T, E>
80where
81	E: fmt::Debug,
82{
83	#[inline]
84	fn log_err_debug(self, level: Level) -> Self
85	where
86		Self: Sized,
87	{
88		self.inspect_err(|error| error::inspect_debug_log_level(&error, level))
89	}
90}