Skip to main content

ferro_rs/http/
mod.rs

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