From d66728889105f6f1ef5113d9ceb223e362df0008 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Mon, 27 Oct 2025 18:15:25 -0400 Subject: Convert the `Setup` component into a freestanding struct. The changes to the setup-requiring middleware are probably more general than was strictly needed, but they will make it work with anything that can provide a `Setup` component rather than being bolted to `App` specifically, which feels tidier. --- src/setup/required.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/setup/required.rs') diff --git a/src/setup/required.rs b/src/setup/required.rs index a2aed18..e475381 100644 --- a/src/setup/required.rs +++ b/src/setup/required.rs @@ -4,26 +4,29 @@ use std::{ }; use axum::{ - extract::Request, + extract::{FromRef, Request}, http::StatusCode, response::{IntoResponse, Response}, }; use tower::{Layer, Service}; -use crate::{app::App, error::Internal}; +use crate::{error::Internal, setup::app::Setup}; #[derive(Clone)] -pub struct Required(pub App); +pub struct Required(pub App); -impl Required { - pub fn with_fallback(self, fallback: F) -> WithFallback { +impl Required { + pub fn with_fallback(self, fallback: F) -> WithFallback { let Self(app) = self; WithFallback { app, fallback } } } -impl Layer for Required { - type Service = Middleware; +impl Layer for Required +where + Self: Clone, +{ + type Service = Middleware; fn layer(&self, inner: S) -> Self::Service { let Self(app) = self.clone(); @@ -36,16 +39,16 @@ impl Layer for Required { } #[derive(Clone)] -pub struct WithFallback { +pub struct WithFallback { app: App, fallback: F, } -impl Layer for WithFallback +impl Layer for WithFallback where Self: Clone, { - type Service = Middleware; + type Service = Middleware; fn layer(&self, inner: S) -> Self::Service { let Self { app, fallback } = self.clone(); @@ -58,17 +61,19 @@ where } #[derive(Clone)] -pub struct Middleware { +pub struct Middleware { inner: S, app: App, fallback: F, } -impl Service for Middleware +impl Service for Middleware where + Setup: FromRef, Self: Clone, S: Service + Send + 'static, S::Future: Send, + App: Send + 'static, F: IntoResponse + Clone + Send + 'static, { type Response = S::Response; @@ -87,7 +92,7 @@ where } = self.clone(); Box::pin(async move { - match app.setup().completed().await { + match Setup::from_ref(&app).completed().await { Ok(true) => inner.call(req).await, Ok(false) => Ok(fallback.into_response()), Err(error) => Ok(Internal::from(error).into_response()), -- cgit v1.2.3