Skip to content

feat: removals #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ where

/// Add state to the router, readying methods that require that state.
///
/// Note that the type parameter `S2` is NOT the state you are adding to the
/// ## Note
///
/// The type parameter `S2` is NOT the state you are adding to the
/// router. It is additional state that must be added AFTER the state `S`.
pub fn with_state<S2>(self, state: S) -> Router<S2> {
map_inner!(self, inner => inner.with_state(&state))
Expand Down Expand Up @@ -227,6 +229,15 @@ where
})
}

/// Remove a method from the router.
///
/// If the route does not exist, this method is a no-op.
pub fn remove_route(self, method: impl Into<Cow<'static, str>>) -> Self {
tap_inner!(self, mut this => {
this = this.remove_route(method.into());
})
}

/// Nest a router under a prefix. This is useful for grouping related
/// methods together, or for namespacing logical groups of methods.
///
Expand Down Expand Up @@ -346,7 +357,6 @@ where
///
/// This method allows users to specify a runtime handle for the router to
/// use. This runtime is accessible to all handlers invoked by the router.
/// Handlers.
///
/// Tasks spawned by the router will be spawned on the provided runtime,
/// and automatically cancelled when the returned `axum::Router` is dropped.
Expand Down Expand Up @@ -514,7 +524,11 @@ impl<S> RouterInner<S> {
Ok(id)
}

/// Enroll a method name, returning an ID assignment. Panics if the method
/// Enroll a method name, returning an ID assignment.
///
/// # Panics
///
/// Panics if the method
/// name already exists in the router.
fn enroll_method(
&mut self,
Expand Down Expand Up @@ -566,6 +580,19 @@ impl<S> RouterInner<S> {
self.route_erased(method, MakeErasedHandler::from_handler(handler))
}

/// Remove a method from the router.
///
/// If the route does not exist, this method is a no-op.
pub(crate) fn remove_route(mut self, method: Cow<'static, str>) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought deeply about how this should interact with aliasing. should removing a method also remove all aliases of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine as a user you would expect that most of the time, but I guess there is a case where you want to remove just the alias or just the method you aliased

if let Some(id) = self.name_to_id.remove(&method) {
self.id_to_name.remove(&id);
self.routes.remove(&id);
self
} else {
self
}
}

/// Call a method on the router, with the provided state.
#[track_caller]
pub(crate) fn call_with_state(&self, args: HandlerArgs, state: S) -> RouteFuture {
Expand Down
Loading