1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-12-03 20:02:12 +01:00
actix-web/src/types/path.rs

217 lines
5.5 KiB
Rust
Raw Normal View History

//! Path extractor
use std::{fmt, ops};
use actix_http::error::{Error, ErrorNotFound};
use actix_router::PathDeserializer;
use serde::de;
2019-04-07 23:43:07 +02:00
use crate::dev::Payload;
use crate::request::HttpRequest;
2019-03-17 05:43:48 +01:00
use crate::FromRequest;
#[derive(PartialEq, Eq, PartialOrd, Ord)]
/// Extract typed information from the request's path.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
///
/// /// extract path info from "/{username}/{count}/index.html" url
/// /// {username} - deserializes to a String
/// /// {count} - - deserializes to a u32
/// fn index(info: web::Path<(String, u32)>) -> String {
/// format!("Welcome {}! {}", info.0, info.1)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/{count}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- register handler with `Path` extractor
/// );
/// }
/// ```
///
/// It is possible to extract path information to a specific type that
/// implements `Deserialize` trait from *serde*.
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, Error};
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// extract `Info` from a path using serde
/// fn index(info: web::Path<Info>) -> Result<String, Error> {
/// Ok(format!("Welcome {}!", info.username))
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- use handler with Path` extractor
/// );
/// }
/// ```
pub struct Path<T> {
inner: T,
}
impl<T> Path<T> {
/// Deconstruct to an inner value
pub fn into_inner(self) -> T {
self.inner
}
}
impl<T> AsRef<T> for Path<T> {
fn as_ref(&self) -> &T {
&self.inner
}
}
impl<T> ops::Deref for Path<T> {
type Target = T;
fn deref(&self) -> &T {
&self.inner
}
}
impl<T> ops::DerefMut for Path<T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T> From<T> for Path<T> {
fn from(inner: T) -> Path<T> {
Path { inner }
}
}
impl<T: fmt::Debug> fmt::Debug for Path<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
impl<T: fmt::Display> fmt::Display for Path<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.inner.fmt(f)
}
}
/// Extract typed information from the request's path.
///
/// ## Example
///
/// ```rust
/// use actix_web::{web, App};
///
/// /// extract path info from "/{username}/{count}/index.html" url
/// /// {username} - deserializes to a String
/// /// {count} - - deserializes to a u32
/// fn index(info: web::Path<(String, u32)>) -> String {
/// format!("Welcome {}! {}", info.0, info.1)
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/{count}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- register handler with `Path` extractor
/// );
/// }
/// ```
///
/// It is possible to extract path information to a specific type that
/// implements `Deserialize` trait from *serde*.
///
/// ```rust
/// #[macro_use] extern crate serde_derive;
/// use actix_web::{web, App, Error};
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// extract `Info` from a path using serde
/// fn index(info: web::Path<Info>) -> Result<String, Error> {
/// Ok(format!("Welcome {}!", info.username))
/// }
///
/// fn main() {
/// let app = App::new().service(
/// web::resource("/{username}/index.html") // <- define path parameters
/// .route(web::get().to(index)) // <- use handler with Path` extractor
/// );
/// }
/// ```
impl<T> FromRequest for Path<T>
where
T: de::DeserializeOwned,
{
2019-04-14 01:35:25 +02:00
type Config = ();
type Error = Error;
type Future = Result<Self, Error>;
#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
2019-04-07 23:43:07 +02:00
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
.map(|inner| Path { inner })
.map_err(ErrorNotFound)
}
}
2019-03-10 19:04:50 +01:00
#[cfg(test)]
mod tests {
use actix_router::ResourceDef;
use super::*;
use crate::test::{block_on, TestRequest};
#[test]
fn test_extract_path_single() {
let resource = ResourceDef::new("/{value}/");
2019-04-07 23:43:07 +02:00
let mut req = TestRequest::with_uri("/32/").to_srv_request();
2019-03-10 19:04:50 +01:00
resource.match_path(req.match_info_mut());
2019-04-07 23:43:07 +02:00
let (req, mut pl) = req.into_parts();
assert_eq!(*Path::<i8>::from_request(&req, &mut pl).unwrap(), 32);
2019-03-10 19:04:50 +01:00
}
#[test]
fn test_tuple_extract() {
let resource = ResourceDef::new("/{key}/{value}/");
2019-04-07 23:43:07 +02:00
let mut req = TestRequest::with_uri("/name/user1/?id=test").to_srv_request();
2019-03-10 19:04:50 +01:00
resource.match_path(req.match_info_mut());
2019-04-07 23:43:07 +02:00
let (req, mut pl) = req.into_parts();
let res =
block_on(<(Path<(String, String)>,)>::from_request(&req, &mut pl)).unwrap();
2019-03-10 19:04:50 +01:00
assert_eq!((res.0).0, "name");
assert_eq!((res.0).1, "user1");
let res = block_on(
2019-04-07 23:43:07 +02:00
<(Path<(String, String)>, Path<(String, String)>)>::from_request(
&req, &mut pl,
),
2019-03-10 19:04:50 +01:00
)
.unwrap();
assert_eq!((res.0).0, "name");
assert_eq!((res.0).1, "user1");
assert_eq!((res.1).0, "name");
assert_eq!((res.1).1, "user1");
2019-04-07 23:43:07 +02:00
let () = <()>::from_request(&req, &mut pl).unwrap();
2019-03-10 19:04:50 +01:00
}
}