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
use core::fmt::Debug;
mod completion;
pub use self::completion::Completion;
mod error;
pub use self::error::Error;
mod status;
pub use self::status::Status;
pub type Result<Output = (), ErrData = ()> =
core::result::Result<Completion<Output>, Error<ErrData>>;
pub trait ResultExt<Output, ErrData: Debug> {
fn status(&self) -> Status;
fn log_warning(self) -> core::result::Result<Output, Error<ErrData>>;
fn unwrap_success(self) -> Output;
fn expect_success(self, msg: &str) -> Output;
fn expect_error(self, msg: &str) -> Error<ErrData>;
fn map_inner<Mapped>(self, f: impl FnOnce(Output) -> Mapped) -> Result<Mapped, ErrData>;
fn discard_errdata(self) -> Result<Output>;
fn warning_as_error(self) -> core::result::Result<Output, Error<ErrData>>
where
ErrData: Default;
}
impl<Output, ErrData: Debug> ResultExt<Output, ErrData> for Result<Output, ErrData> {
fn status(&self) -> Status {
match self {
Ok(c) => c.status(),
Err(e) => e.status(),
}
}
fn log_warning(self) -> core::result::Result<Output, Error<ErrData>> {
self.map(Completion::log)
}
fn unwrap_success(self) -> Output {
self.unwrap().unwrap()
}
fn expect_success(self, msg: &str) -> Output {
self.expect(msg).expect(msg)
}
fn expect_error(self, msg: &str) -> Error<ErrData> {
self.map(|completion| completion.status()).expect_err(msg)
}
fn map_inner<Mapped>(self, f: impl FnOnce(Output) -> Mapped) -> Result<Mapped, ErrData> {
self.map(|completion| completion.map(f))
}
fn discard_errdata(self) -> Result<Output> {
match self {
Ok(o) => Ok(o),
Err(e) => Err(e.status().into()),
}
}
fn warning_as_error(self) -> core::result::Result<Output, Error<ErrData>>
where
ErrData: Default,
{
match self.map(Completion::split) {
Ok((Status::SUCCESS, res)) => Ok(res),
Ok((s, _)) => Err(Error::new(s, Default::default())),
Err(e) => Err(e),
}
}
}