Skip to main content

ferro_rs/http/
mod.rs

1mod body;
2pub mod cookie;
3mod extract;
4mod form_request;
5mod multipart;
6mod request;
7pub mod request_context;
8/// API resource and pagination types.
9pub mod resources;
10mod response;
11
12pub use body::{collect_body, parse_form, parse_json};
13pub use cookie::{parse_cookies, Cookie, CookieOptions, SameSite};
14pub use extract::{FromParam, FromRequest};
15pub use form_request::FormRequest;
16pub use multipart::{validate_mime, validate_size, MultipartForm, UploadedFile};
17pub use request::{Request, RequestParts};
18pub use request_context::request_host;
19pub use resources::{PaginationLinks, PaginationMeta, Resource, ResourceCollection, ResourceMap};
20pub use response::{
21    HttpResponse, InertiaRedirect, Redirect, RedirectRouteBuilder, Response, ResponseExt,
22};
23
24/// Error type for missing route parameters
25///
26/// This type is kept for backward compatibility. New code should use
27/// `FrameworkError::param()` instead.
28#[derive(Debug)]
29pub struct ParamError {
30    /// Name of the missing route parameter.
31    pub param_name: String,
32}
33
34impl From<ParamError> for HttpResponse {
35    fn from(err: ParamError) -> HttpResponse {
36        HttpResponse::json(serde_json::json!({
37            "error": format!("Missing required parameter: {}", err.param_name)
38        }))
39        .status(400)
40    }
41}
42
43impl From<ParamError> for crate::error::FrameworkError {
44    fn from(err: ParamError) -> crate::error::FrameworkError {
45        crate::error::FrameworkError::ParamError {
46            param_name: err.param_name,
47        }
48    }
49}
50
51impl From<ParamError> for Response {
52    fn from(err: ParamError) -> Response {
53        Err(HttpResponse::from(crate::error::FrameworkError::from(err)))
54    }
55}
56
57/// Create a text response
58pub fn text(body: impl Into<String>) -> Response {
59    Ok(HttpResponse::text(body))
60}
61
62/// Create a JSON response from a serde_json::Value
63pub fn json(body: serde_json::Value) -> Response {
64    Ok(HttpResponse::json(body))
65}
66
67/// Create a binary response from raw bytes.
68///
69/// No default Content-Type is set; add one via `.header()` on the inner `HttpResponse`.
70pub fn bytes(body: impl Into<bytes::Bytes>) -> Response {
71    Ok(HttpResponse::bytes(body))
72}