Skip to content

Commit 220f7ea

Browse files
rustypbzweihander
authored andcommitted
Implement Status for Result<T, crate::Error>
1 parent 014f301 commit 220f7ea

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/status.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,48 @@ where
7171
}
7272
}
7373

74+
impl<T> Status<T, Error> for Result<T, Error> {
75+
/// Wrap the error value with an additional status code.
76+
///
77+
/// # Panics
78+
///
79+
/// Panics if [`Status`][status] is not a valid [`StatusCode`][statuscode].
80+
///
81+
/// [status]: crate::Status
82+
/// [statuscode]: crate::StatusCode
83+
fn status<S>(self, status: S) -> Result<T, Error>
84+
where
85+
S: TryInto<StatusCode>,
86+
S::Error: Debug,
87+
{
88+
self.map_err(|mut error| {
89+
error.set_status(status);
90+
error
91+
})
92+
}
93+
94+
/// Wrap the error value with an additional status code that is evaluated
95+
/// lazily only once an error does occur.
96+
///
97+
/// # Panics
98+
///
99+
/// Panics if [`Status`][status] is not a valid [`StatusCode`][statuscode].
100+
///
101+
/// [status]: crate::Status
102+
/// [statuscode]: crate::StatusCode
103+
fn with_status<S, F>(self, f: F) -> Result<T, Error>
104+
where
105+
S: TryInto<StatusCode>,
106+
S::Error: Debug,
107+
F: FnOnce() -> S,
108+
{
109+
self.map_err(|mut error| {
110+
error.set_status(f());
111+
error
112+
})
113+
}
114+
}
115+
74116
impl<T> Status<T, Infallible> for Option<T> {
75117
/// Wrap the error value with an additional status code.
76118
///

0 commit comments

Comments
 (0)