tako-rs 1.1.2

Multi-transport Rust framework for modern network services.
Documentation
//! Path extraction from HTTP requests.
//!
//! This module provides the [`Path`](crate::extractors::path::Path) extractor for accessing the URI path from
//! incoming HTTP requests. It wraps a reference to the path string, allowing
//! efficient access to the request path without copying the underlying data.
//!
//! # Examples
//!
//! ```rust
//! use tako::extractors::path::Path;
//! use tako::types::Request;
//!
//! async fn handle_path(Path(path): Path<'_>) {
//!     println!("Request path: {}", path);
//!
//!     // Check specific path patterns
//!     if path.starts_with("/api/") {
//!         println!("API endpoint");
//!     }
//!
//!     // Extract path segments
//!     let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
//!     println!("Path segments: {:?}", segments);
//! }
//! ```

use std::convert::Infallible;

use http::request::Parts;

use crate::extractors::FromRequest;
use crate::extractors::FromRequestParts;
use crate::types::Request;

/// Path extractor that provides access to the HTTP request path.
///
/// This extractor wraps a reference to the URI path of a request, providing
/// efficient access to the path string without copying the underlying data.
/// It can be used to inspect, validate, or extract information from the request path.
///
/// # Examples
///
/// ```rust
/// use tako::extractors::path::Path;
/// use tako::types::Request;
///
/// async fn handler(Path(path): Path<'_>) {
///     match path {
///         "/health" => println!("Health check endpoint"),
///         "/api/users" => println!("Users API endpoint"),
///         _ if path.starts_with("/static/") => println!("Static file request"),
///         _ => println!("Other path: {}", path),
///     }
/// }
/// ```
#[doc(alias = "path")]
pub struct Path(pub String);

impl<'a> FromRequest<'a> for Path {
  type Error = Infallible;

  fn from_request(
    req: &'a mut Request,
  ) -> impl core::future::Future<Output = core::result::Result<Self, Self::Error>> + Send + 'a {
    futures_util::future::ready(Ok(Path(req.uri().path().to_string())))
  }
}

impl<'a> FromRequestParts<'a> for Path {
  type Error = Infallible;

  fn from_request_parts(
    parts: &'a mut Parts,
  ) -> impl core::future::Future<Output = core::result::Result<Self, Self::Error>> + Send + 'a {
    futures_util::future::ready(Ok(Path(parts.uri.path().to_string())))
  }
}