From ef60f855ce0d473211deba35ebab735b0ce3be03 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 24 Apr 2025 08:47:17 +0200 Subject: [PATCH] Add Router::reset_fallback --- axum/CHANGELOG.md | 4 ++++ axum/src/routing/mod.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 2fc2d20c..b32883ec 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +# Unreleased + +- **added:** `Router::reset_fallback` + # 0.8.3 - **added:** Implement `From` for `Message` ([#3273]) diff --git a/axum/src/routing/mod.rs b/axum/src/routing/mod.rs index 9c30288e..7da58a16 100644 --- a/axum/src/routing/mod.rs +++ b/axum/src/routing/mod.rs @@ -381,6 +381,21 @@ where }) } + /// Reset the fallback to its default. + /// + /// Useful to merge two routers with fallbacks, as [`merge`] doesn't allow + /// both routers to have an explicit fallback. Use this method to remove the + /// one you want to discard before merging. + /// + /// [`merge`]: Self::merge + pub fn reset_fallback(self) -> Self { + tap_inner!(self, mut this => { + this.fallback_router = PathRouter::new_fallback(); + this.default_fallback = true; + this.catch_all_fallback = Fallback::Default(Route::new(NotFound)); + }) + } + fn fallback_endpoint(self, endpoint: Endpoint) -> Self { tap_inner!(self, mut this => { this.fallback_router.set_fallback(endpoint);