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
//! Utilities used across the whole crate.
// TODO add documentation for traits
// -------------------------------------------------------------------------------------------------
// Logging.
use clap_verbosity_flag::LevelFilter;
pub fn activate_logging(log_level: LevelFilter) {
env_logger::Builder::new().filter_level(log_level).init();
}
// -------------------------------------------------------------------------------------------------
// Traits for Option & Result.
use log::error;
use std::fmt::{Debug, Display};
pub trait LogOnErr {
fn log_on_err(self) -> Self;
}
impl<T, E: Debug + Display> LogOnErr for Result<T, E> {
/// Produce an error [log] if self is an Err.
fn log_on_err(self) -> Self {
match &self {
Err(err) => error!("{:?} {}", err, err),
Ok(_) => {}
}
self
}
}
pub trait LogOnErrUnwrap<T> {
fn log_on_err_unwrap(self) -> T;
}
impl<T, E: Debug + Display> LogOnErrUnwrap<T> for Result<T, E> {
/// Produce an error [log] if self is an Err, then unwrap.
fn log_on_err_unwrap(self) -> T {
match &self {
Err(err) => error!("{:?} {}", err, err),
Ok(_) => {}
}
self.unwrap()
}
}
pub trait Consume<T> {
fn consume<F>(self, f: F)
where
F: FnOnce(T);
}
impl<T> Consume<T> for Option<T> {
/// If `None` then do nothing and return nothing. If `Some` then call the
/// given function `f` with the value `T` but do not return anything.
fn consume<F>(self, f: F)
where
F: FnOnce(T),
{
match self {
None => {}
Some(x) => f(x),
}
}
}
pub trait IfSomeThen<T> {
fn if_some_then<F>(self, f: F) -> Option<T>
where
F: FnOnce(&T);
}
impl<T> IfSomeThen<T> for Option<T> {
/// If Some then execute the function on the underlying value. Always return
/// Option as it was.
fn if_some_then<F>(self, f: F) -> Option<T>
where
F: FnOnce(&T),
{
match &self {
None => {}
Some(x) => f(x),
}
self
}
}
pub trait IfNoneThen<T> {
fn if_none_then<F>(self, f: F) -> Option<T>
where
F: FnOnce();
}
impl<T> IfNoneThen<T> for Option<T> {
/// If None then execute the function on the underlying value. Always return
/// Option as it was.
fn if_none_then<F>(self, f: F) -> Option<T>
where
F: FnOnce(),
{
match &self {
None => f(),
Some(_) => {}
}
self
}
}
pub trait ErrOnSome {
fn err_on_some<E>(&self, err: E) -> Result<(), E>;
}
impl<T> ErrOnSome for Option<T> {
/// Return an error if `Some(_)`, otherwise do nothing.
fn err_on_some<E>(&self, err: E) -> Result<(), E> {
match self {
None => Ok(()),
Some(_) => Err(err),
}
}
}
pub trait ErrUnlessTrue {
fn err_unless_true<E>(&self, err: E) -> Result<(), E>;
}
impl ErrUnlessTrue for Option<bool> {
/// Return an error if `None` or `Some(false)`, otherwise do nothing.
fn err_unless_true<E>(&self, err: E) -> Result<(), E> {
match self {
None => Err(err),
Some(false) => Err(err),
Some(true) => Ok(()),
}
}
}
// -------------------------------------------------------------------------------------------------
// Testing utils.
#[cfg(any(test, feature = "fuzzing"))]
pub mod test_utils {
/// Check 2 errors are the same.
/// https://stackoverflow.com/a/65618681
macro_rules! assert_err {
($expression:expr, $($pattern:tt)+) => {
match $expression {
$($pattern)+ => (),
ref e => panic!("expected `{}` but got `{:?}`", stringify!($($pattern)+), e),
}
}
}
pub(crate) use assert_err;
/// Same as [assert_err] but without needing debug
/// https://stackoverflow.com/a/65618681
macro_rules! assert_err_simple {
($expression:expr, $($pattern:tt)+) => {
match $expression {
$($pattern)+ => (),
_ => panic!("expected a specific error but did not get it"),
}
}
}
pub(crate) use assert_err_simple;
pub fn init_logger() {
let _ =
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace"))
.is_test(true)
.try_init();
}
}