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

Regex pattern matching route parameters #19

Closed
whitfin opened this issue Aug 3, 2018 · 3 comments
Closed

Regex pattern matching route parameters #19

whitfin opened this issue Aug 3, 2018 · 3 comments

Comments

@whitfin
Copy link

whitfin commented Aug 3, 2018

It might be that there's a way to do this by composing filters, but it seems to escape me for the moment.

If I have a route of /:something, is there a way to provide a pattern to filter? For example, say I wanted to accept /:something, but only where :something is value1 or value2. It seems I could maybe join some handlers up together to make this happen, but not in a way that would provide access to the value of :something?

@seanmonstar
Copy link
Owner

seanmonstar commented Aug 3, 2018

You could make a stronger type than a generic regex, by using warp::path::param::<T>(). For example:

struct Something;

impl FromStr for Something {
    type Err = ();
    fn from_str(s: &str) -> Result<Something, ()> {
        match s {
            "value1" | "value2" => Ok(Something),
            _ => Err(())
        }
    }
}

let something = warp::path::param::<Something>();

@whitfin
Copy link
Author

whitfin commented Aug 3, 2018

@seanmonstar interesting, so an error will just 404 the request?

@seanmonstar
Copy link
Owner

Yep!

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