pub struct RouteParams(/* private fields */);
Expand description
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 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()
.
Implementations§
Source§impl RouteParams
impl RouteParams
Sourcepub fn new() -> RouteParams
pub fn new() -> RouteParams
Creates an empty route parameters map.
Sourcepub fn with_capacity(capacity: usize) -> RouteParams
pub fn with_capacity(capacity: usize) -> RouteParams
Creates an empty route parameters map with the specified capacity.
Sourcepub fn set<N: Into<String>, V: Into<String>>(
&mut self,
param_name: N,
param_val: V,
)
pub fn set<N: Into<String>, V: Into<String>>( &mut self, param_name: N, param_val: V, )
Sets a new parameter entry with the specified key and the value.
Sourcepub fn get<N: Into<String>>(&self, param_name: N) -> Option<&String>
pub fn get<N: Into<String>>(&self, param_name: N) -> Option<&String>
Returns the route parameter value mapped with the specified key.
§Examples
use routerify::{Router, RouteParams};
use routerify::ext::RequestExt;
use hyper::{Response, Body};
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();
Sourcepub fn has<N: Into<String>>(&self, param_name: N) -> bool
pub fn has<N: Into<String>>(&self, param_name: N) -> bool
Checks if a route parameter exists.
§Examples
use routerify::{Router, RouteParams};
use routerify::ext::RequestExt;
use hyper::{Response, Body};
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();
pub fn is_empty(&self) -> bool
Sourcepub fn params_names(&self) -> impl Iterator<Item = &String>
pub fn params_names(&self) -> impl Iterator<Item = &String>
Returns an Iterator
over the parameter names.
Sourcepub fn iter(&self) -> impl Iterator<Item = (&String, &String)>
pub fn iter(&self) -> impl Iterator<Item = (&String, &String)>
Returns an Iterator
over the parameter entries
as (parameter_name: &String, parameter_value: &String)
.
Sourcepub fn extend(&mut self, other_route_params: RouteParams)
pub fn extend(&mut self, other_route_params: RouteParams)
Extends the current parameters map with other one.
Trait Implementations§
Source§impl Clone for RouteParams
impl Clone for RouteParams
Source§fn clone(&self) -> RouteParams
fn clone(&self) -> RouteParams
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more