Skip to content

Commit 4745f44

Browse files
committed
Parameterize ResultExt by Into<StatusCode>
1 parent 5f85f54 commit 4745f44

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/result_ext.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,48 @@ use std::error::Error as StdError;
77
/// This trait is sealed and cannot be implemented outside of `http-types`.
88
pub trait ResultExt<T, E>: private::Sealed {
99
/// Wrap the error value with an additional status code.
10-
fn status(self, status: StatusCode) -> Result<T, Error>;
10+
fn status<S>(self, status: S) -> Result<T, Error>
11+
where
12+
S: Into<StatusCode>;
1113

1214
/// Wrap the error value with an additional status code that is evaluated
1315
/// lazily only once an error does occur.
14-
fn with_status<F>(self, f: F) -> Result<T, Error>
16+
fn with_status<S, F>(self, f: F) -> Result<T, Error>
1517
where
16-
F: FnOnce() -> StatusCode;
18+
S: Into<StatusCode>,
19+
F: FnOnce() -> S;
1720
}
1821

1922
impl<T, E> ResultExt<T, E> for Result<T, E>
2023
where
2124
E: StdError + Send + Sync + 'static,
2225
{
23-
fn status(self, status: StatusCode) -> Result<T, Error> {
24-
self.map_err(|error| Error::new(status, error))
26+
fn status<S>(self, status: S) -> Result<T, Error> where
27+
S: Into<StatusCode> {
28+
self.map_err(|error| Error::new(status.into(), error))
2529
}
2630

27-
fn with_status<F>(self, f: F) -> Result<T, Error>
31+
fn with_status<S, F>(self, f: F) -> Result<T, Error>
2832
where
29-
F: FnOnce() -> StatusCode,
33+
S: Into<StatusCode>,
34+
F: FnOnce() -> S,
3035
{
31-
self.map_err(|error| Error::new(f(), error))
36+
self.map_err(|error| Error::new(f().into(), error))
3237
}
3338
}
3439

3540
impl<T> ResultExt<T, Infallible> for Option<T> {
36-
fn status(self, status: StatusCode) -> Result<T, Error> {
37-
self.ok_or_else(|| Error::from_str(status, "NoneError"))
41+
fn status<S>(self, status: S) -> Result<T, Error>
42+
where S: Into<StatusCode> {
43+
self.ok_or_else(|| Error::from_str(status.into(), "NoneError"))
3844
}
3945

40-
fn with_status<F>(self, f: F) -> Result<T, Error>
46+
fn with_status<S, F>(self, f: F) -> Result<T, Error>
4147
where
42-
F: FnOnce() -> StatusCode,
48+
S: Into<StatusCode>,
49+
F: FnOnce() -> S,
4350
{
44-
self.ok_or_else(|| Error::from_str(f(), "NoneError"))
51+
self.ok_or_else(|| Error::from_str(f().into(), "NoneError"))
4552
}
4653
}
4754

0 commit comments

Comments
 (0)