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

Implementing Support for suffixes eg. file extensions #17

Open
apps4uco opened this issue Apr 25, 2022 · 2 comments
Open

Implementing Support for suffixes eg. file extensions #17

apps4uco opened this issue Apr 25, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@apps4uco
Copy link

Hi it would be really useful for my use case to support suffixes.
e.g. routes like "/users/:id.png"

or even "/users/:id.:ext"

... or even "/users/*.png"

Although it is possible to do at the application level it quickly gets very complicated when multiple extensions are present, and it looks like it might be easier to implement it in Matchit.

According to what I understand from the docs the parameter extends up to the next slash, and so currently is not possible.

How easy would it be to change parameter names to not allow '.' and add something like ParamWithSuffix(String) to the enum NodeType.

I optimistically forked the repo and was planning to implement it but then I realized that I dont understand the code well enough... My fork just has the test case (that currently fails) See below:

use matchit::{InsertError, MatchError, Router};

fn router()->Result<Router<&'static str>,InsertError> {
    let mut router = Router::new();
    router.insert("/home", "Welcome!")?;
    router.insert("/users/:id", "A User")?;
    router.insert("/users/:id.png", "A User Photo Currently not working")?;
    router.insert("/users/:id/x.png", "Another User Photo that works")?;
    Ok(router)
}

#[test]
fn match_img()->Result<(),MatchError>{

    let router=router().unwrap();
    let matched = router.at("/users/978")?;
    assert_eq!(matched.params.get("id"), Some("978"));
    assert_eq!(*matched.value, "A User");
    Ok(())
}

To consider: how would multiple '.' be handled e.g. schema.foo.yaml

@ibraheemdev
Copy link
Owner

I would be open to adding this feature but it's not really a high priority for me. It might be easier to put some time into writing a param parser at the application level. As a side note, this is the main benefit of the {x} syntax for route parameters...

@ibraheemdev ibraheemdev added the enhancement New feature or request label May 23, 2023
@ibraheemdev ibraheemdev mentioned this issue Jul 28, 2023
3 tasks
@ibraheemdev
Copy link
Owner

Now that 0.8 is released with the new syntax, I'll try to implement this. The tricky part is that we have to repeatedly attempt to match the suffix in the case that we don't get a full match earlier in the path, but the suffix exists elsewhere. This also means suffixes might not be as performant as a regular static segment, but hopefully this can be added without affect performance for existing routes.

mladedav added a commit to mladedav/axum that referenced this issue Mar 12, 2024
`axum` has some assumptions about the way paths are structured which may
be potentially changed in a minor release. For example, there are plans
to support [suffixes in matched
segments](ibraheemdev/matchit#17) among other
features which would allow routes such as `/{file}.jpg` which `axum`
currently does not expect.
mladedav added a commit to mladedav/axum that referenced this issue Mar 24, 2024
`axum` has some assumptions about the way paths are structured which may
be potentially changed in a minor release. For example, there are plans
to support [suffixes in matched
segments](ibraheemdev/matchit#17) among other
features which would allow routes such as `/{file}.jpg` which `axum`
currently does not expect.
mladedav added a commit to mladedav/axum that referenced this issue May 2, 2024
`axum` has some assumptions about the way paths are structured which may
be potentially changed in a minor release. For example, there are plans
to support [suffixes in matched
segments](ibraheemdev/matchit#17) among other
features which would allow routes such as `/{file}.jpg` which `axum`
currently does not expect.
mladedav added a commit to mladedav/axum that referenced this issue May 2, 2024
`axum` has some assumptions about the way paths are structured which may
be potentially changed in a minor release. For example, there are plans
to support [suffixes in matched
segments](ibraheemdev/matchit#17) among other
features which would allow routes such as `/{file}.jpg` which `axum`
currently does not expect.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants