fix(macros): support prefix/suffix in TypedPath segments#3725
Open
dihannahdi wants to merge 1 commit into
Open
fix(macros): support prefix/suffix in TypedPath segments#3725dihannahdi wants to merge 1 commit into
dihannahdi wants to merge 1 commit into
Conversation
f5093b4 to
a7cfc20
Compare
The TypedPath derive macro previously required path segments to be
entirely a capture (e.g., /{id}) or entirely static (e.g., /users).
This made it impossible to use routes with mixed segments like
/@{username} or /avatars/{id}.png, even though the underlying matchit
router fully supports them.
Add a CaptureWithAffix variant to the Segment enum and update
parse_path() to detect captures embedded within a segment,
extracting the prefix and suffix around the {capture}. The
format_str, captures, and field expansion functions are all
updated to handle the new variant.
Closes tokio-rs#3681
2f757ed to
fbb5a22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
The
TypedPathderive macro currently requires each path segment to be entirely a capture (/{id}) or entirely static (/users). This means routes with mixed segments—where a capture is embedded within static text—are rejected by the macro even though the underlyingmatchitrouter fully supports them.Common real-world patterns that fail today:
These all produce a compile error because
parse_path()only recognises segments that start with{and end with}.Closes #3681
Solution
Add a
CaptureWithAffixvariant to the internalSegmentenum:parse_path()now scans each segment for{…}anywhere within it. When a capture is found with surrounding static text, the new variant is emitted. All downstream functions (format_str_from_path,captures_from_path, field expansion, unit-struct validation) are updated to handle it.Escaped braces (
{{,}}) are left alone as static segments—matching the existing behaviour.Testing
prefix_suffix_capture.rs) covering named structs, tuple structs, and paths with prefix-only, suffix-only, and prefix+suffix captures.cargo test -p axum-macros— 28/28 passing).