[][src]Macro warp::path

macro_rules! path {
    (@start $first:tt $(/ $tail:tt)*) => { ... };
    (@segment $param:ty) => { ... };
    (@segment $s:expr) => { ... };
    ($($pieces:tt)*) => { ... };
}

Convenient way to chain multiple path filters together.

Any number of either type identifiers or string expressions can be passed, each separated by a forward slash (/). Strings will be used to match path segments exactly, and type identifiers are used just like param filters.

Example

use warp::Filter;

// Match `/sum/:a/:b`
let route = path!("sum" / u32 / u32)
    .map(|a, b| {
        format!("{} + {} = {}", a, b, a + b)
    });

The equivalent filter chain without using the path! macro looks this:

use warp::Filter;

let route = warp::path("sum")
    .and(warp::path::param::<u32>())
    .and(warp::path::param::<u32>())
    .map(|a, b| {
        format!("{} + {} = {}", a, b, a + b)
    });

In fact, this is exactly what the macro expands to.

Note that path! does not automatically include an end() filter so you should be careful to avoid letting shorter paths accidentally match longer ones:

use warp::Filter;

let sum = path!("math" / "sum" / u32 /u32)
    .map(|a, b| {
        format!("{} + {} = {}", a, b, a + b)
    });
let help = path!("math" / "sum")
    .map(|| "This API returns the sum of two u32's");
let api = help.or(sum);

In the above example, the sum path won't actually be hit because the help filter will match all requests. We would need to change help to be path!("math" / "sum").and(warp::path::end()) to ensure that the shorter path filter does not match the longer paths.