Skip to main content

ferro_rs/http/
mod.rs

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