From cdcf6e8aa573aeb3165a15f4c6aede8c3a82fe43 Mon Sep 17 00:00:00 2001 From: Daniel Segovia Date: Fri, 18 Oct 2024 11:28:26 -0700 Subject: [PATCH 1/2] add axum::Form struct to prelude and its corresponding error --- src/controller/mod.rs | 14 ++++++++++++-- src/errors.rs | 5 ++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/controller/mod.rs b/src/controller/mod.rs index 66ead17a3..5b7eba80c 100644 --- a/src/controller/mod.rs +++ b/src/controller/mod.rs @@ -44,11 +44,11 @@ //! AppRoutes::with_default_routes() //! // .add_route(controllers::notes::routes()) //! } -//! +//! //! async fn boot(mode: StartMode, environment: &Environment) -> Result{ //! create_app::(mode, environment).await //! } -//! +//! //! #[cfg(feature = "channels")] //! /// Only when `channels` feature is enabled //! fn register_channels(_ctx: &AppContext) -> AppChannels { @@ -180,6 +180,16 @@ impl IntoResponse for Json { } } +#[derive(Debug, FromRequest)] +#[from_request(via(axum::Form), rejection(Error))] +pub struct Form(pub T); + +impl IntoResponse for Form { + fn into_response(self) -> axum::response::Response { + axum::Form(self.0).into_response() + } +} + impl IntoResponse for Error { /// Convert an `Error` into an HTTP response. fn into_response(self) -> Response { diff --git a/src/errors.rs b/src/errors.rs index 334e3e0f8..e10283c69 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,7 @@ //! # Application Error Handling use axum::{ - extract::rejection::JsonRejection, + extract::rejection::{FormRejection, JsonRejection}, http::{ header::{InvalidHeaderName, InvalidHeaderValue}, method::InvalidMethod, @@ -62,6 +62,9 @@ pub enum Error { #[error(transparent)] JsonRejection(#[from] JsonRejection), + #[error(transparent)] + FormRejection(#[from] FormRejection), + #[error("cannot parse `{1}`: {0}")] YAMLFile(#[source] serde_yaml::Error, String), From f0d4755250402c5934c5ced51435186982d538ba Mon Sep 17 00:00:00 2001 From: Daniel Segovia Date: Mon, 21 Oct 2024 09:38:11 -0700 Subject: [PATCH 2/2] use newly formed loco Form extractor instead of direct axum's --- src/prelude.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/prelude.rs b/src/prelude.rs index 3f247cd0d..4b0d5b06c 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,6 +1,6 @@ pub use async_trait::async_trait; pub use axum::{ - extract::{Form, Path, State}, + extract::{Path, State}, response::{IntoResponse, Response}, routing::{delete, get, head, options, patch, post, put, trace}, }; @@ -34,7 +34,7 @@ pub use crate::{ }, not_found, unauthorized, views::{engines::TeraView, ViewEngine, ViewRenderer}, - Json, Routes, + Form, Json, Routes, }, errors::Error, mailer,