From 57e3e067b1c9c4084af8da6f4e2c5e19271fd72c Mon Sep 17 00:00:00 2001 From: imbolc Date: Sat, 15 Jun 2024 15:18:49 +0600 Subject: [PATCH] Feature --- Cargo.toml | 9 ++++++--- src/{vary_middleware.rs => auto_vary.rs} | 6 +++--- src/extractors.rs | 20 ++++++++++++-------- src/lib.rs | 4 ++-- 4 files changed, 23 insertions(+), 16 deletions(-) rename src/{vary_middleware.rs => auto_vary.rs} (96%) diff --git a/Cargo.toml b/Cargo.toml index b39863e..c2c3c59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,14 +15,17 @@ default = [] unstable = [] guards = ["tower", "futures-core", "pin-project-lite"] serde = ["dep:serde", "dep:serde_json"] +auto-vary = ["axum", "futures", "tokio"] [dependencies] axum-core = "0.4" http = { version = "1.0", default-features = false } async-trait = "0.1" -axum = "0.7" # TODO: remove -tokio = { version = "1", features = ["sync"] } # TODO: hide behind a feature? -futures = "0.3" # TODO + +# Optional dependencies required for the `auto-vary` feature. +axum = { version = "0.7", default-features = false, optional = true } +tokio = { version = "1", features = ["sync"], optional = true } +futures = { version = "0.3", default-features = false, optional = true } # Optional dependencies required for the `guards` feature. tower = { version = "0.4", default-features = false, optional = true } diff --git a/src/vary_middleware.rs b/src/auto_vary.rs similarity index 96% rename from src/vary_middleware.rs rename to src/auto_vary.rs index 84c614b..fd3b609 100644 --- a/src/vary_middleware.rs +++ b/src/auto_vary.rs @@ -17,7 +17,7 @@ use crate::{ const MIDDLEWARE_DOUBLE_USE: &str = "Configuration error: `axum_httpx::vary_middleware` is used twice"; -pub trait Notifier { +pub(crate) trait Notifier { fn sender(&mut self) -> Option>; fn notify(&mut self) { @@ -59,7 +59,7 @@ define_notifiers!( HxTriggerNameExtracted ); -pub async fn vary_middleware(mut request: Request, next: Next) -> Response { +pub async fn middleware(mut request: Request, next: Next) -> Response { let exts = request.extensions_mut(); let rx_header = [ (HxRequestExtracted::insert(exts), HX_REQUEST_STR), @@ -122,7 +122,7 @@ mod tests { "/multiple-extractors", get(|_: HxRequest, _: HxTarget, _: HxTrigger, _: HxTriggerName| async { () }), ) - .layer(axum::middleware::from_fn(vary_middleware)); + .layer(axum::middleware::from_fn(middleware)); axum_test::TestServer::new(app).unwrap() } diff --git a/src/extractors.rs b/src/extractors.rs index 7a90f11..5f25683 100644 --- a/src/extractors.rs +++ b/src/extractors.rs @@ -137,10 +137,11 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + #[cfg(feature = "auto-vary")] parts .extensions - .get_mut::() - .map(crate::vary_middleware::Notifier::notify); + .get_mut::() + .map(crate::auto_vary::Notifier::notify); if parts.headers.contains_key(HX_REQUEST) { return Ok(HxRequest(true)); @@ -169,10 +170,11 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + #[cfg(feature = "auto-vary")] parts .extensions - .get_mut::() - .map(crate::vary_middleware::Notifier::notify); + .get_mut::() + .map(crate::auto_vary::Notifier::notify); if let Some(target) = parts.headers.get(HX_TARGET) { if let Ok(target) = target.to_str() { @@ -203,10 +205,11 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + #[cfg(feature = "auto-vary")] parts .extensions - .get_mut::() - .map(crate::vary_middleware::Notifier::notify); + .get_mut::() + .map(crate::auto_vary::Notifier::notify); if let Some(trigger_name) = parts.headers.get(HX_TRIGGER_NAME) { if let Ok(trigger_name) = trigger_name.to_str() { @@ -237,10 +240,11 @@ where type Rejection = std::convert::Infallible; async fn from_request_parts(parts: &mut Parts, _: &S) -> Result { + #[cfg(feature = "auto-vary")] parts .extensions - .get_mut::() - .map(crate::vary_middleware::Notifier::notify); + .get_mut::() + .map(crate::auto_vary::Notifier::notify); if let Some(trigger) = parts.headers.get(HX_TRIGGER) { if let Ok(trigger) = trigger.to_str() { diff --git a/src/lib.rs b/src/lib.rs index 5d4dae1..c0dc625 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,5 +23,5 @@ pub use headers::*; #[doc(inline)] pub use responders::*; -pub(crate) mod vary_middleware; -pub use vary_middleware::vary_middleware; +#[cfg(feature = "auto-vary")] +pub mod auto_vary;