utkarshkukreti / draco

Draco is a Rust library for building client side web applications with Web Assembly.
Apache License 2.0
302 stars 18 forks source link

Implement Router URL parsing API like how `warp` does #6

Open utkarshkukreti opened 5 years ago

utkarshkukreti commented 5 years ago

The current parsing API is clunky: the parts which don't have a meaningful return value (which return ()) are sent to the map function:

.alt(("posts", query("sort").optional()), |((), sort)| {
    Route::PostIndex { sort }
})

Warp uses some type system magic to flatten the arguments and filter out the ones which return ():

let hi = warp::path("hello")
    .and(warp::path::param())
    .and(warp::header("user-agent"))
    .map(|param: String, agent: String| {
        format!("Hello {}, whose agent is {}", param, agent)
    });

https://docs.rs/warp/0.1.9/warp/index.html

utkarshkukreti commented 5 years ago

Ideally, this:

parse(url)
    .alt((), |()| Route::Index)
    .alt(("posts", query("sort").optional()), |((), sort)| {
        Route::PostIndex { sort }
    })
    .alt(("posts", param()), |((), id)| Route::PostShow {
        id,
        hash: url.hash.clone(),
    })
    .value()

would become:

ok(|| Route::Index)
    .or(("posts", query("sort").optional()), |sort| {
        Route::PostIndex { sort }
    })
    .or(("posts", param()), |id| Route::PostShow {
        id,
        hash: url.hash.clone(),
    })
    .parse(url);