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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
use std::collections::HashMap;
/// Represents a map of the route parameters using the name of the parameter specified in the path as their respective keys.
///
/// Please refer to the [Route Parameters](./index.html#route-parameters) section for more info.
///
/// **Note:** This type shouldn't be created directly. It will be populated into the `req` object of the route handler and
/// can be accessed as `req.params()`.
#[derive(Debug, Clone, Default)]
pub struct RouteParams(HashMap<String, String>);
impl RouteParams {
/// Creates an empty route parameters map.
pub fn new() -> RouteParams {
RouteParams(HashMap::new())
}
/// Creates an empty route parameters map with the specified capacity.
pub fn with_capacity(capacity: usize) -> RouteParams {
RouteParams(HashMap::with_capacity(capacity))
}
/// Sets a new parameter entry with the specified key and the value.
pub fn set<N: Into<String>, V: Into<String>>(&mut self, param_name: N, param_val: V) {
self.0.insert(param_name.into(), param_val.into());
}
/// Returns the route parameter value mapped with the specified key.
///
/// # Examples
///
/// ```
/// use routerify::{Router, RouteParams};
/// use routerify::ext::RequestExt;
/// use hyper::{Response, Body};
/// # use std::convert::Infallible;
///
/// # fn run() -> Router<Body, Infallible> {
/// let router = Router::builder()
/// .get("/users/:userName/books/:bookName", |req| async move {
/// let params: &RouteParams = req.params();
///
/// let user_name = params.get("userName").unwrap();
/// let book_name = params.get("bookName").unwrap();
///
/// Ok(Response::new(Body::from(format!("Username: {}, Book Name: {}", user_name, book_name))))
/// })
/// .build()
/// .unwrap();
/// # router
/// # }
/// # run();
/// ```
pub fn get<N: Into<String>>(&self, param_name: N) -> Option<&String> {
self.0.get(¶m_name.into())
}
/// Checks if a route parameter exists.
///
/// # Examples
///
/// ```
/// use routerify::{Router, RouteParams};
/// use routerify::ext::RequestExt;
/// use hyper::{Response, Body};
/// # use std::convert::Infallible;
///
/// # fn run() -> Router<Body, Infallible> {
/// let router = Router::builder()
/// .get("/users/:userName", |req| async move {
/// let params: &RouteParams = req.params();
///
/// if params.has("userName") {
/// Ok(Response::new(Body::from(params.get("userName").unwrap().to_string())))
/// } else {
/// Ok(Response::new(Body::from("username is not provided")))
/// }
/// })
/// .build()
/// .unwrap();
/// # router
/// # }
/// # run();
/// ```
pub fn has<N: Into<String>>(&self, param_name: N) -> bool {
self.0.contains_key(¶m_name.into())
}
/// Returns the length of the route parameters.
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns an [`Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html) over the parameter names.
pub fn params_names(&self) -> impl Iterator<Item = &String> {
self.0.keys()
}
/// Returns an [`Iterator`](https://doc.rust-lang.org/std/iter/trait.Iterator.html) over the parameter entries
/// as `(parameter_name: &String, parameter_value: &String)`.
pub fn iter(&self) -> impl Iterator<Item = (&String, &String)> {
self.0.iter()
}
/// Extends the current parameters map with other one.
pub fn extend(&mut self, other_route_params: RouteParams) {
other_route_params.0.into_iter().for_each(|(key, val)| {
self.set(key, val);
})
}
}