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
use http::{header, StatusCode};
use reply::{self, Reply};
use self::sealed::AsLocation;
pub fn redirect(uri: impl AsLocation) -> impl Reply {
reply::with_header(
StatusCode::MOVED_PERMANENTLY,
header::LOCATION,
uri.header_value(),
)
}
mod sealed {
use bytes::Bytes;
use http::{header::HeaderValue, Uri};
pub trait AsLocation: Sealed {}
pub trait Sealed {
fn header_value(self) -> HeaderValue;
}
impl AsLocation for Uri {}
impl Sealed for Uri {
fn header_value(self) -> HeaderValue {
let bytes = Bytes::from(self.to_string());
HeaderValue::from_shared(bytes)
.expect("Uri is a valid HeaderValue")
}
}
}