mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-03 20:02:12 +01:00
177 lines
4.4 KiB
Rust
177 lines
4.4 KiB
Rust
|
//! Path extractor
|
||
|
|
||
|
use std::{fmt, ops};
|
||
|
|
||
|
use actix_http::error::{Error, ErrorNotFound};
|
||
|
use actix_router::PathDeserializer;
|
||
|
use serde::de;
|
||
|
|
||
|
use crate::request::HttpRequest;
|
||
|
use crate::service::ServiceFromRequest;
|
||
|
|
||
|
use super::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
|
||
|
}
|
||
|
|
||
|
/// Extract path information from a request
|
||
|
pub fn extract(req: &HttpRequest) -> Result<Path<T>, de::value::Error>
|
||
|
where
|
||
|
T: de::DeserializeOwned,
|
||
|
{
|
||
|
de::Deserialize::deserialize(PathDeserializer::new(req.match_info()))
|
||
|
.map(|inner| Path { 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, P> FromRequest<P> for Path<T>
|
||
|
where
|
||
|
T: de::DeserializeOwned,
|
||
|
{
|
||
|
type Error = Error;
|
||
|
type Future = Result<Self, Error>;
|
||
|
|
||
|
#[inline]
|
||
|
fn from_request(req: &mut ServiceFromRequest<P>) -> Self::Future {
|
||
|
Self::extract(req).map_err(ErrorNotFound)
|
||
|
}
|
||
|
}
|