Warp: Regex pattern matching route parameters

Created on 4 Aug 2018  路  3Comments  路  Source: seanmonstar/warp

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?

Most helpful comment

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>();

All 3 comments

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>();

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

Yep!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weiznich picture weiznich  路  7Comments

silvioprog picture silvioprog  路  3Comments

alexreg picture alexreg  路  7Comments

seanmonstar picture seanmonstar  路  3Comments

hwchen picture hwchen  路  3Comments