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
//! This crate provides `inspect_or_log`, `unwrap_or_log` and `unwrap_or_default_log` methods
//! on all types implemented [`Try`].
//! They are useful if you want to log the error value.
//!
//! Different log levels are chosen by calling different methods.
//!
//! | method                   | level |
//! | ------------------------ | ----- |
//! | `inspect_or_log*`        | info  |
//! | `unwrap_or_default_log*` | warn  |
//! | `unwrap_or_log*`         | error |

#![feature(decl_macro)]
#![feature(try_trait_v2)]
#![warn(missing_docs)]

pub mod macros;

#[doc(hidden)]
pub use log as __log;

use log::{error, info, warn};
use std::{
    fmt::{Debug, Display},
    ops::{ControlFlow, Try},
};

/// This trait provides the or-log methods.
pub trait TryLog<T, R: Debug> {
    /// Log with [`log::Level::Info`] if the value is [`Err`] or [`None`].
    fn inspect_or_log(self, msg: &str) -> Self;

    /// Log with [`log::Level::Info`] if the value is [`Err`] or [`None`].
    ///
    /// The log message is returned by `f`.
    fn inspect_or_log_with<M: Display>(self, f: impl FnOnce() -> M) -> Self;

    /// Log with [`log::Level::Warn`] if the value is [`Err`] or [`None`],
    /// and return a default value of `T`.
    fn unwrap_or_default_log(self, msg: &str) -> T
    where
        T: Default;

    /// Log with [`log::Level::Warn`] if the value is [`Err`] or [`None`],
    /// and return a default value of `T`.
    ///
    /// The log message is returned by `f`.
    fn unwrap_or_default_log_with<M: Display>(self, f: impl FnOnce() -> M) -> T
    where
        T: Default;

    /// Log with [`log::Level::Error`] if the value is [`Err`] or [`None`],
    /// and panic with the same error message.
    fn unwrap_or_log(self, msg: &str) -> T;

    /// Log with [`log::Level::Error`] if the value is [`Err`] or [`None`],
    /// and panic with the same error message.
    ///
    /// The log message is returned by `f`.
    fn unwrap_or_log_with<M: Display>(self, f: impl FnOnce() -> M) -> T;
}

#[inline(always)]
fn inspect_or_and<T: Try>(t: T, f: impl FnOnce(&T::Residual)) -> T {
    match t.branch() {
        ControlFlow::Continue(v) => T::from_output(v),
        ControlFlow::Break(e) => {
            f(&e);
            T::from_residual(e)
        }
    }
}

#[inline(always)]
fn unwrap_or_default_and<T: Try>(t: T, f: impl FnOnce(&T::Residual)) -> T::Output
where
    T::Output: Default,
{
    match t.branch() {
        ControlFlow::Continue(v) => v,
        ControlFlow::Break(e) => {
            f(&e);
            Default::default()
        }
    }
}

#[inline(always)]
fn unwrap_or_and<T: Try>(t: T, f: impl FnOnce(&T::Residual) -> String) -> T::Output {
    match t.branch() {
        ControlFlow::Continue(v) => v,
        ControlFlow::Break(e) => {
            let msg = f(&e);
            error!("{}", msg);
            panic!("{}", msg)
        }
    }
}

impl<T, R: Debug, Tr: Try<Output = T, Residual = R>> TryLog<T, R> for Tr {
    #[inline(always)]
    fn inspect_or_log(self, msg: &str) -> Self {
        inspect_or_and(self, |e| info!("{msg}: {e:?}"))
    }

    #[inline(always)]
    fn inspect_or_log_with<M: Display>(self, f: impl FnOnce() -> M) -> Self {
        inspect_or_and(self, |e| info!("{}: {e:?}", f()))
    }

    #[inline(always)]
    fn unwrap_or_default_log(self, msg: &str) -> T
    where
        T: Default,
    {
        unwrap_or_default_and(self, |e| warn!("{msg}: {e:?}"))
    }

    #[inline(always)]
    fn unwrap_or_default_log_with<M: Display>(self, f: impl FnOnce() -> M) -> T
    where
        T: Default,
    {
        unwrap_or_default_and(self, |e| warn!("{}: {e:?}", f()))
    }

    #[inline(always)]
    fn unwrap_or_log(self, msg: &str) -> T {
        unwrap_or_and(self, |e| format!("{msg}: {e:?}"))
    }

    #[inline(always)]
    fn unwrap_or_log_with<M: Display>(self, f: impl FnOnce() -> M) -> T {
        unwrap_or_and(self, |e| format!("{}: {e:?}", f()))
    }
}