Skip to content

Commit

Permalink
Add Router::has_routes (#2790)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeck88 committed Jun 22, 2024
1 parent 670bf69 commit 4f3999c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions axum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- **change:** Avoid cloning `Arc` during deserialization of `Path`
- **added:** `axum::serve::Serve::tcp_nodelay` and `axum::serve::WithGracefulShutdown::tcp_nodelay` ([#2653])
- **added:** `Router::has_routes` function ([#2790])

[#2653]: https://github.com/tokio-rs/axum/pull/2653
[#2790]: https://github.com/tokio-rs/axum/pull/2790

# 0.7.5 (24. March, 2024)

Expand Down
4 changes: 4 additions & 0 deletions axum/src/docs/routing/route_layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ the request matches a route. This is useful for middleware that return early
(such as authorization) which might otherwise convert a `404 Not Found` into a
`401 Unauthorized`.

This function will panic if no routes have been declared yet on the router,
since the new layer will have no effect, and this is typically a bug.
In generic code, you can test if that is the case first, by calling [`Router::has_routes`].

# Example

```rust
Expand Down
5 changes: 5 additions & 0 deletions axum/src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ where
})
}

/// True if the router currently has at least one route added.
pub fn has_routes(&self) -> bool {
self.inner.path_router.has_routes()
}

#[track_caller]
#[doc = include_str!("../docs/routing/fallback.md")]
pub fn fallback<H, T>(self, handler: H) -> Self
Expand Down
4 changes: 4 additions & 0 deletions axum/src/routing/path_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ where
}
}

pub(super) fn has_routes(&self) -> bool {
!self.routes.is_empty()
}

pub(super) fn with_state<S2>(self, state: S) -> PathRouter<S2, IS_FALLBACK> {
let routes = self
.routes
Expand Down

0 comments on commit 4f3999c

Please sign in to comment.