error_log/
traits.rs

1use crate::{if_std, Entry, ErrorLog};
2use alloc::vec::IntoIter;
3#[cfg(feature = "helper-traits")]
4use core::{
5    fmt::{Debug, Display},
6    ops::{AddAssign, Deref, DerefMut, MulAssign},
7};
8if_std! {
9    use std::process::Termination;
10}
11
12impl<T, E> IntoIterator for ErrorLog<T, E> {
13    type Item = Entry<E>;
14    type IntoIter = IntoIter<Self::Item>;
15    /// Iterates over Error stored.
16    fn into_iter(self) -> Self::IntoIter {
17        self.entries.into_iter()
18    }
19}
20
21#[cfg(feature = "helper-traits")]
22impl<T, E: Debug + Display> AddAssign<E> for ErrorLog<T, E> {
23    /// Make `err_log += ERROR` store error if [`Result`] if an [`Err`].
24    ///
25    /// Shorthand for [`push_err()`][crate::ErrorLog::push_err]
26    fn add_assign(&mut self, rhs: E) {
27        self.push_err(rhs);
28    }
29}
30
31#[cfg(feature = "helper-traits")]
32impl<T, U, E: Debug + Display> AddAssign<Result<U, E>> for ErrorLog<T, E> {
33    /// Make `err_log += RESULT` store error of [`Result`] if any.
34    ///
35    /// Shorthand for [`push_result()`][crate::ErrorLog::push_result]
36    fn add_assign(&mut self, rhs: Result<U, E>) {
37        self.push_result(rhs);
38    }
39}
40
41#[cfg(feature = "std")]
42impl<T, E> Termination for ErrorLog<T, E> {
43    fn report(self) -> std::process::ExitCode {
44        use std::process::ExitCode;
45        match self.ok.is_some() {
46            true => ExitCode::SUCCESS,
47            false => ExitCode::FAILURE,
48        }
49    }
50}
51
52#[cfg(feature = "helper-traits")]
53impl<T, U: Into<T>, E: Debug + Display, F: Into<E>> MulAssign<Result<U, F>> for ErrorLog<T, E> {
54    fn mul_assign(&mut self, rhs: Result<U, F>) {
55        self.merge_result(rhs);
56    }
57}
58
59#[cfg(feature = "helper-traits")]
60impl<T, U: Into<T>, E> MulAssign<Option<U>> for ErrorLog<T, E> {
61    fn mul_assign(&mut self, rhs: Option<U>) {
62        if let Some(val) = rhs {
63            self.set_ok(val.into());
64        };
65    }
66}
67
68// impl<T: Debug, U: Into<T>, E, F> DivAssign<Result<U, F>> for ErrorLog<T, E> {
69//     fn div_assign(&mut self, rhs: Result<U, F>) {
70//         self.set_ok()
71//     }
72// }
73
74#[cfg(feature = "helper-traits")]
75/// Get immutable '`ok`' value as [`Option`] by dereferencing
76impl<T, E> Deref for ErrorLog<T, E> {
77    type Target = Option<T>;
78    fn deref(&self) -> &Self::Target {
79        self.ok()
80    }
81}
82
83#[cfg(feature = "helper-traits")]
84/// Get mutable '`ok`' value as [`Option`] by dereferencing
85impl<T, E> DerefMut for ErrorLog<T, E> {
86    fn deref_mut(&mut self) -> &mut Self::Target {
87        self.ok_mut()
88    }
89}