Skip to main content

ferro_rs/http/
mod.rs

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