Skip to content

Commit e429a37

Browse files
josephlrGabrielMajeri
authored andcommitted
Add Result and Error helper methods
1 parent 8090630 commit e429a37

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/result/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ impl<Data: Debug> Error<Data> {
2121
pub fn data(&self) -> &Data {
2222
&self.data
2323
}
24+
25+
/// Split this error into its inner status and error data
26+
pub fn split(self) -> (Status, Data) {
27+
(self.status, self.data)
28+
}
2429
}
2530

2631
// Errors without payloads can be autogenerated from statuses

src/result/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ pub trait ResultExt<Output, ErrData: Debug> {
4343
/// Expect success without warnings, panic with provided message otherwise
4444
fn expect_success(self, msg: &str) -> Output;
4545

46+
/// Expect error, panic with provided message otherwise, discarding output
47+
fn expect_error(self, msg: &str) -> Error<ErrData>;
48+
4649
/// Transform the inner output, if any
4750
fn map_inner<Mapped>(self, f: impl FnOnce(Output) -> Mapped) -> Result<Mapped, ErrData>;
4851

@@ -75,6 +78,10 @@ impl<Output, ErrData: Debug> ResultExt<Output, ErrData> for Result<Output, ErrDa
7578
self.expect(msg).expect(msg)
7679
}
7780

81+
fn expect_error(self, msg: &str) -> Error<ErrData> {
82+
self.map(|completion| completion.status()).expect_err(msg)
83+
}
84+
7885
fn map_inner<Mapped>(self, f: impl FnOnce(Output) -> Mapped) -> Result<Mapped, ErrData> {
7986
self.map(|completion| completion.map(f))
8087
}

0 commit comments

Comments
 (0)