From 985e0152ff5717681f125a5c5613f9a3dd5108a0 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 17 May 2022 18:57:37 +0200 Subject: [PATCH 1/2] Add `axum_extra::extract::Query` --- axum-extra/Cargo.toml | 1 + axum-extra/src/extract/mod.rs | 6 ++ axum-extra/src/extract/query.rs | 117 ++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 axum-extra/src/extract/query.rs diff --git a/axum-extra/Cargo.toml b/axum-extra/Cargo.toml index 3e9807d14e..36df3126f3 100644 --- a/axum-extra/Cargo.toml +++ b/axum-extra/Cargo.toml @@ -18,6 +18,7 @@ cookie = ["cookie-lib"] cookie-signed = ["cookie", "cookie-lib/signed"] cookie-private = ["cookie", "cookie-lib/private"] form = ["serde", "serde_html_form"] +query = ["serde", "serde_html_form"] spa = ["tower-http/fs"] [dependencies] diff --git a/axum-extra/src/extract/mod.rs b/axum-extra/src/extract/mod.rs index 4a98c7163c..9abed09f88 100644 --- a/axum-extra/src/extract/mod.rs +++ b/axum-extra/src/extract/mod.rs @@ -8,6 +8,9 @@ mod form; #[cfg(feature = "cookie")] pub mod cookie; +#[cfg(feature = "query")] +mod query; + pub use self::cached::Cached; #[cfg(feature = "cookie")] @@ -21,3 +24,6 @@ pub use self::cookie::SignedCookieJar; #[cfg(feature = "form")] pub use self::form::Form; + +#[cfg(feature = "query")] +pub use self::query::Query; diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs new file mode 100644 index 0000000000..dcbcdb31fd --- /dev/null +++ b/axum-extra/src/extract/query.rs @@ -0,0 +1,117 @@ +use axum::{ + async_trait, + extract::{ + rejection::{FailedToDeserializeQueryString, QueryRejection}, + FromRequest, RequestParts, + }, +}; +use serde::de::DeserializeOwned; +use std::ops::Deref; + +/// Extractor that deserializes query strings into some type. +/// +/// `T` is expected to implement [`serde::Deserialize`]. +/// +/// # Differences from `axum::extract::Form` +/// +/// This extractor uses [`serde_html_form`] under-the-hood which supports multi-value items. These +/// are sent by multiple `` attributes of the same name (e.g. checkboxes) and `