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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#[macro_use]
pub mod termcolor;
use clap;
use failure::Error;
use std::cmp;
use std::fmt::Arguments;
use std::result::Result as StdResult;
#[repr(usize)]
#[derive(Clone, Copy, Eq, Debug)]
pub enum ChatterLevel {
Minimal = 0,
Normal,
}
impl PartialEq for ChatterLevel {
#[inline]
fn eq(&self, other: &ChatterLevel) -> bool {
*self as usize == *other as usize
}
}
impl PartialOrd for ChatterLevel {
#[inline]
fn partial_cmp(&self, other: &ChatterLevel) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ChatterLevel {
#[inline]
fn cmp(&self, other: &ChatterLevel) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum NotificationKind {
Note,
Warning,
Severe,
Fatal,
}
pub trait NotificationBackend {
fn notify(&mut self, kind: NotificationKind, args: Arguments, err: Option<Error>);
}
#[macro_export]
macro_rules! rn_note {
($dest:expr, $( $fmt_args:expr ),*) => {
$dest.notify($crate::notify::NotificationKind::Note, format_args!($( $fmt_args ),*), None)
};
($dest:expr, $( $fmt_args:expr ),* ; $err:expr) => {
$dest.notify($crate::notify::NotificationKind::Note, format_args!($( $fmt_args ),*), Some($err))
};
}
#[macro_export]
macro_rules! rn_warning {
($dest:expr, $( $fmt_args:expr ),*) => {
$dest.notify($crate::notify::NotificationKind::Warning, format_args!($( $fmt_args ),*), None)
};
($dest:expr, $( $fmt_args:expr ),* ; $err:expr) => {
$dest.notify($crate::notify::NotificationKind::Warning, format_args!($( $fmt_args ),*), Some($err))
};
}
#[macro_export]
macro_rules! rn_severe {
($dest:expr, $( $fmt_args:expr ),*) => {
$dest.notify($crate::notify::NotificationKind::Severe, format_args!($( $fmt_args ),*), None)
};
($dest:expr, $( $fmt_args:expr ),* ; $err:expr) => {
$dest.notify($crate::notify::NotificationKind::Severe, format_args!($( $fmt_args ),*), Some($err))
};
}
#[macro_export]
macro_rules! rn_fatal {
($dest:expr, $( $fmt_args:expr ),*) => {
$dest.notify($crate::notify::NotificationKind::Fatal, format_args!($( $fmt_args ),*), None)
};
($dest:expr, $( $fmt_args:expr ),* ; $err:expr) => {
$dest.notify($crate::notify::NotificationKind::Fatal, format_args!($( $fmt_args ),*), Some($err))
};
}
#[derive(Clone, Copy, Debug)]
pub struct NoopNotificationBackend {}
impl NoopNotificationBackend {
pub fn new() -> NoopNotificationBackend {
NoopNotificationBackend {}
}
}
impl NotificationBackend for NoopNotificationBackend {
fn notify(&mut self, _kind: NotificationKind, _args: Arguments, _err: Option<Error>) {}
}
#[derive(Debug)]
struct NotificationData {
kind: NotificationKind,
text: String,
err: Option<Error>,
}
#[derive(Debug)]
pub struct BufferingNotificationBackend {
buf: Vec<NotificationData>,
}
impl BufferingNotificationBackend {
pub fn new() -> Self {
Self { buf: Vec::new() }
}
pub fn drain<B: NotificationBackend>(mut self, other: &mut B) {
for info in self.buf.drain(..) {
other.notify(info.kind, format_args!("{}", info.text), info.err);
}
}
}
impl NotificationBackend for BufferingNotificationBackend {
fn notify(&mut self, kind: NotificationKind, args: Arguments, err: Option<Error>) {
self.buf.push(NotificationData {
kind: kind,
text: format!("{}", args),
err: err,
});
}
}
pub trait ClapNotificationArgsExt {
fn rubbl_notify_args(self) -> Self;
}
impl<'a, 'b> ClapNotificationArgsExt for clap::App<'a, 'b> {
fn rubbl_notify_args(self) -> Self {
self.arg(
clap::Arg::with_name("chatter_level")
.long("chatter")
.short("c")
.value_name("LEVEL")
.help("How much chatter to print when running")
.possible_values(&["default", "minimal"])
.default_value("default"),
)
}
}
pub fn run_with_notifications<'a, E, F>(matches: clap::ArgMatches<'a>, inner: F) -> i32
where
E: Into<Error>,
F: FnOnce(clap::ArgMatches<'a>, &mut dyn NotificationBackend) -> StdResult<i32, E>,
{
let chatter = match matches.value_of("chatter_level").unwrap() {
"default" => ChatterLevel::Normal,
"minimal" => ChatterLevel::Minimal,
_ => unreachable!(),
};
let mut tnb = termcolor::TermcolorNotificationBackend::new(chatter);
match inner(matches, &mut tnb) {
Ok(ret) => ret,
Err(e) => {
tnb.bare_error(e);
1
}
}
}