Skip to content
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

Add Support for Parameter Suffixes #61

Merged
merged 9 commits into from
Jan 2, 2025
Merged

Add Support for Parameter Suffixes #61

merged 9 commits into from
Jan 2, 2025

Conversation

ibraheemdev
Copy link
Owner

@ibraheemdev ibraheemdev commented Oct 11, 2024

Adds support for parameters with static suffixes. For example, /{foo}.png.

The implementation allows parameters with and without suffixes to co-exist. For example, /foo, /{foo}.jpg, and /{foo}.png are not considered overlapping. This works by adding another layer to the tree after parameter nodes, representing suffixes before the next route segment. The appropriate suffix is determined using a linear search.

Additionally, the restriction of a single parameter per route segment still remains. This means /{foo}.{bar} is not supported. Arbitrary parameters without a / anchor can become quite complicated in terms of backtracking (similar to #41 (comment)), so multiple parameters are unlikely to be supported.

Resolves #17.

@ibraheemdev ibraheemdev added the feature New feature or request label Oct 11, 2024
Copy link

@scottlamb scottlamb left a comment

Choose a reason for hiding this comment

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

Thank you! I've been looking forward to this.

I have a case in which I'd like to use this for two file suffixes, one of which is a prefix of the other. It appears to fail as follows:

#[test]
fn wildcard_suffix2() {
    MatchTest {
        routes: vec![
            "/{foo}.ts",
            "/{foo}.tst",
        ],
        matches: vec![
            ("/bar.ts", "/{foo}.ts", p! {}),
            ("/bar.tst", "/{foo}.tst", p! {}),
        ],
    }
    .run();
}
---- wildcard_suffix2 stdout ----
thread 'wildcard_suffix2' panicked at tests/match.rs:78:13:
assertion `left == right` failed: /{foo}.tst
  left: Err(Conflict { with: "/{foo}.ts" })
 right: Ok(())
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@ibraheemdev
Copy link
Owner Author

Yeah that seems reasonable, I think we can support it.

@ibraheemdev ibraheemdev force-pushed the wildcard-suffix branch 3 times, most recently from 79b0067 to 709a95f Compare December 31, 2024 05:56
@ibraheemdev
Copy link
Owner Author

ibraheemdev commented Jan 1, 2025

I updated the implementation to support overlapping suffixes, such as /foo, /{foo}.jpg, and /{foo}.png. This does impose a very minor performance regression by introducing an extra node after any route parameter, even without a suffix. Routes without suffixes are special cased so it is possible to avoid this, but it's incredibly minor so it's not very relevant.

@ibraheemdev ibraheemdev force-pushed the wildcard-suffix branch 2 times, most recently from dee1bd3 to 00fcc28 Compare January 1, 2025 08:20
@ibraheemdev ibraheemdev merged commit 9736ae9 into master Jan 2, 2025
10 checks passed
@ibraheemdev ibraheemdev deleted the wildcard-suffix branch January 2, 2025 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implementing Support for suffixes eg. file extensions
2 participants