Macro warp::path

source ·
macro_rules! path {
    ($($pieces:tt)*) => { ... };
}
Expand description

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 = warp::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>())
    .and(warp::path::end())
    .map(|a, b| {
        format!("{} + {} = {}", a, b, a + b)
    });

Path Prefixes

The path! macro automatically assumes the path should include an end() filter. To build up a path filter prefix, such that the end() isn’t included, use the / .. syntax.

use warp::Filter;

let prefix = warp::path!("math" / "sum" / ..);

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

let help = warp::path::end()
    .map(|| "This API returns the sum of two u32's");

let api = prefix.and(sum.or(help));