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
use crate::{Error, StatusCode};
use core::convert::{Infallible, TryInto};
use std::error::Error as StdError;
pub trait Status<T, E>: private::Sealed {
fn status<S>(self, status: S) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug;
fn with_status<S, F>(self, f: F) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug,
F: FnOnce() -> S;
}
impl<T, E> Status<T, E> for Result<T, E>
where
E: StdError + Send + Sync + 'static,
{
fn status<S>(self, status: S) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug,
{
self.map_err(|error| {
let status = status.try_into().unwrap();
Error::new(status, error)
})
}
fn with_status<S, F>(self, f: F) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug,
F: FnOnce() -> S,
{
self.map_err(|error| {
let status = f().try_into().unwrap();
Error::new(status, error)
})
}
}
impl<T> Status<T, Infallible> for Option<T> {
fn status<S>(self, status: S) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug,
{
self.ok_or_else(|| {
let status = status.try_into().unwrap();
Error::from_str(status, "NoneError")
})
}
fn with_status<S, F>(self, f: F) -> Result<T, Error>
where
S: TryInto<StatusCode>,
S::Error: std::fmt::Debug,
F: FnOnce() -> S,
{
self.ok_or_else(|| {
let status = f().try_into().unwrap();
Error::from_str(status, "NoneError")
})
}
}
pub(crate) mod private {
pub trait Sealed {}
impl<T, E> Sealed for Result<T, E> {}
impl<T> Sealed for Option<T> {}
}