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

Regarding axum update to matchit v0.8 #125

Open
y-haidar opened this issue Mar 20, 2024 · 1 comment
Open

Regarding axum update to matchit v0.8 #125

y-haidar opened this issue Mar 20, 2024 · 1 comment

Comments

@y-haidar
Copy link

The new matching syntax breaks path_colon_params in:

pub fn path_colon_params(s: &str) -> Cow<str> {

This is my, not so good, idea to fix it:

pub fn path_colon_params(s: &str) -> Cow<str> {
    // if !s.contains(':') {
    //     return s.into();
    // }

    let mut rewritten = String::with_capacity(s.len());

    #[derive(Clone, Copy)]
    enum State {
        None,
        // this state is no longer needed?
        // WasParam,
        WasWildcard,
    }
    let mut state = State::None;
    let chars_pair = s.chars().zip(s.chars().skip(1));
    let mut do_escape_next = false;

    // look at https://github.com/ibraheemdev/matchit/blob/36e5398589129896c5dad8a6b2580ede59831875/src/escape.rs#L18
    for (i, (c, c_next)) in chars_pair.enumerate() {
        if do_escape_next {
            do_escape_next = false;
            continue;
        }
        if (c == '{' && c_next == '{') || (c == '}' && c_next == '}') {
            // This poses a problem when the url have '{' or '}' escaped not sure how and where to handle this
            rewritten.push(c);
            // skip the next iteration
            do_escape_next = true;
            continue;
        }
        match (state, c, c_next) {
            (State::None, '{', '*') => {
                rewritten.push('{');
                state = State::WasWildcard;
            }
            // This is if, a normal Param is allowed after a wildcard
            // (not sure but I think this is a planned feature in the future?)
            (State::WasWildcard, _, '}') => {
                rewritten.push(c);
                rewritten += "+}";
                state = State::None;
            }
            (_, _, _) => {
                rewritten.push(c);
            }
        }
        if i == s.len() - 2 {
            rewritten.push(c_next);
        }
    }

    rewritten.into()
}

I haven't tested this fully, especially wildcards params, but tested it briefly on:

[patch.crates-io]
axum = { git = 'https://github.com/mladedav/axum.git', rev = "db825f94ec849d39f02f8e9cfc469613680c08f4" }
@Wicpar
Copy link
Collaborator

Wicpar commented Mar 31, 2024

The new syntax is almost the same except we still need to convert the end if it is {*...} To {...+}.

Code should be:
1: get substring after last /
2: if it starts with {* and ends with } copy string until last / and append contents as { content +} else just return original string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants