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
use super::{rejection::*, FromRequest, RequestParts};
use crate::util::ByteStr;
use async_trait::async_trait;
use std::{collections::HashMap, str::FromStr};
#[derive(Debug)]
#[deprecated(since = "0.1.3", note = "Use `axum::extract::Path` instead.")]
pub struct UrlParamsMap(HashMap<ByteStr, ByteStr>);
#[allow(deprecated)]
impl UrlParamsMap {
pub fn get(&self, key: &str) -> Option<&str> {
self.0.get(&ByteStr::new(key)).map(|s| s.as_str())
}
pub fn get_typed<T>(&self, key: &str) -> Option<Result<T, T::Err>>
where
T: FromStr,
{
self.get(key).map(str::parse)
}
}
#[async_trait]
#[allow(deprecated)]
impl<B> FromRequest<B> for UrlParamsMap
where
B: Send,
{
type Rejection = MissingRouteParams;
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
if let Some(params) = req
.extensions_mut()
.and_then(|ext| ext.get_mut::<Option<crate::routing::UrlParams>>())
{
if let Some(params) = params {
Ok(Self(params.0.iter().cloned().collect()))
} else {
Ok(Self(Default::default()))
}
} else {
Err(MissingRouteParams)
}
}
}