Skip to content

Commit

Permalink
feat(web): add comprehensive documentation for extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
zavakid committed Dec 13, 2024
1 parent f0819f5 commit 4978248
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
38 changes: 38 additions & 0 deletions crates/web/src/extract/extract_body.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
//! Body data extraction implementations
//!
//! This module provides implementations for extracting typed data from request bodies.
//! It supports extracting raw bytes, strings, JSON data and form data.
//!
//! # Examples
//!
//! ```no_run
//! # use micro_web::extract::{Json, Form};
//! # use serde::Deserialize;
//!
//! #[derive(Deserialize)]
//! struct User {
//! name: String,
//! age: u32
//! }
//!
//! // Extract JSON data
//! async fn handle_json(Json(user): Json<User>) {
//! println!("Got user: {}", user.name);
//! }
//!
//! // Extract form data
//! async fn handle_form(Form(user): Form<User>) {
//! println!("Got user: {}", user.name);
//! }
//! ```
use crate::body::OptionReqBody;
use crate::extract::{Form, FromRequest, Json};
use crate::RequestContext;
Expand All @@ -7,6 +35,7 @@ use http_body_util::BodyExt;
use micro_http::protocol::ParseError;
use serde::Deserialize;

/// Extracts raw bytes from request body
#[async_trait]
impl FromRequest for Bytes {
type Output<'any> = Bytes;
Expand All @@ -17,6 +46,7 @@ impl FromRequest for Bytes {
}
}

/// Extracts UTF-8 string from request body
#[async_trait]
impl FromRequest for String {
type Output<'any> = String;
Expand All @@ -32,6 +62,10 @@ impl FromRequest for String {
}
}

/// Extracts form data from request body
///
/// This implementation expects the request body to be URL-encoded form data
/// and deserializes it into the target type using `serde_urlencoded`.
#[async_trait]
impl<T> FromRequest for Form<T>
where
Expand All @@ -48,6 +82,10 @@ where
}
}

/// Extracts JSON data from request body
///
/// This implementation expects the request body to be valid JSON
/// and deserializes it into the target type using `serde_json`.
#[async_trait]
impl<T> FromRequest for Json<T>
where
Expand Down
47 changes: 47 additions & 0 deletions crates/web/src/extract/extract_header.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
//! HTTP header extraction implementations
//!
//! This module provides extractors for HTTP header-related information from requests,
//! including HTTP methods, header maps and raw request headers. These extractors allow
//! handlers to directly access header information in a type-safe way.
//!
//! The extractors support both owned and borrowed access to the header data:
//! - Owned extractors like `Method` and `HeaderMap` take ownership of the data
//! - Borrowed extractors like `&Method` and `&HeaderMap` provide reference access
//!
//! # Examples
//!
//! ```no_run
//! use http::{HeaderMap, Method};
//!
//! // Access HTTP method
//! async fn handle_method(method: Method) {
//! match method {
//! Method::GET => println!("Handling GET request"),
//! Method::POST => println!("Handling POST request"),
//! _ => println!("Handling other request method")
//! }
//! }
//!
//! // Access headers
//! async fn handle_headers(headers: &HeaderMap) {
//! if let Some(content_type) = headers.get("content-type") {
//! println!("Content-Type: {:?}", content_type);
//! }
//! }
//! ```
use crate::body::OptionReqBody;
use crate::extract::from_request::FromRequest;
use crate::RequestContext;
use async_trait::async_trait;
use http::{HeaderMap, Method};
use micro_http::protocol::{ParseError, RequestHeader};

/// Extracts the HTTP method by value
///
/// This extractor takes ownership of the request method.
#[async_trait]
impl FromRequest for Method {
type Output<'any> = Method;
Expand All @@ -15,6 +50,9 @@ impl FromRequest for Method {
}
}

/// Extracts a reference to the HTTP method
///
/// This extractor borrows the request method, avoiding cloning.
#[async_trait]
impl FromRequest for &Method {
type Output<'r> = &'r Method;
Expand All @@ -25,6 +63,9 @@ impl FromRequest for &Method {
}
}

/// Extracts a reference to the raw request header
///
/// Provides access to the underlying HTTP request header structure.
#[async_trait]
impl FromRequest for &RequestHeader {
type Output<'r> = &'r RequestHeader;
Expand All @@ -35,6 +76,9 @@ impl FromRequest for &RequestHeader {
}
}

/// Extracts a reference to the header map
///
/// This extractor provides borrowed access to all HTTP headers.
#[async_trait]
impl FromRequest for &HeaderMap {
type Output<'r> = &'r HeaderMap;
Expand All @@ -45,6 +89,9 @@ impl FromRequest for &HeaderMap {
}
}

/// Extracts the header map by value
///
/// This extractor clones and takes ownership of all HTTP headers.
#[async_trait]
impl FromRequest for HeaderMap {
type Output<'any> = HeaderMap;
Expand Down
26 changes: 26 additions & 0 deletions crates/web/src/extract/extract_url.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
//! URL query string extraction functionality
//!
//! This module provides implementation for extracting typed data from URL query strings.
//! It allows handlers to receive strongly-typed query parameters by implementing the
//! `FromRequest` trait for the `Query<T>` type.
//!
//! # Example
//! ```no_run
//! # use serde::Deserialize;
//! # use micro_web::extract::Query;
//!
//! #[derive(Deserialize)]
//! struct Params {
//! name: String,
//! age: u32,
//! }
//!
//! async fn handler(Query(params): Query<Params>) {
//! println!("Name: {}, Age: {}", params.name, params.age);
//! }
//! ```
use crate::extract::{FromRequest, Query};
use crate::{OptionReqBody, RequestContext};
use async_trait::async_trait;
use micro_http::protocol::ParseError;
use serde::Deserialize;

/// Implements query string extraction for any type that implements Deserialize
///
/// This implementation allows automatic deserialization of query string parameters
/// into a strongly-typed struct using serde_qs.
#[async_trait]
impl<T> FromRequest for Query<T>
where
Expand Down

0 comments on commit 4978248

Please sign in to comment.