picoserve: Type error when using `picoserve::routing::Router::nest`

While attempting to nest a router like so: (simplified example)

pub(crate) fn app_router() -> picoserve::Router<AppRouter, GlobalState> {
    picoserve::Router::new()
        .nest("/api", api_router())
        .route(
            "/",
            get_service(picoserve::response::File::html(include_str!(
                "../dist/index.html"
            ))),
        )
}

pub(crate) fn api_router() -> picoserve::Router<Apiouter, GlobalState> {
    picoserve::Router::new().route(
        ("/", parse_path_segment()),
        picoserve::routing::get(
            |on_off| async move {
                info!("Setting to {}", if on_off { "ON" } else { "OFF" });

                DebugValue(on_off)
            },
        ),
    )
}

I encountered the following error:

error[E0308]: mismatched types
    --> src/net.rs:192:23
     |
156  | type AppRouter = impl picoserve::routing::PathRouter<GlobalState>;
     |                  ------------------------------------------------ the found opaque type
...
192  |         .nest("/api", api_router())
     |          ----         ^^^^^^^^^^^^ expected `Router<_>`, found `Router<AppRouter, GlobalState>`
     |          |
     |          arguments to this method are incorrect
     |
     = note: expected struct `picoserve::Router<_, ()>`
                found struct `picoserve::Router<net::AppRouter, net::GlobalState>`
note: method defined here
    --> /Users/louis/.cargo/registry/src/index.crates.io-6f17d22bba15001f/picoserve-0.12.2/src/routing.rs:1185:12
     |
1185 |     pub fn nest<PD: PathDescription<CurrentPathParameters>>(
     |            ^^^^

which happens regardless of if the impl picoserve::routing::PathRouter<GlobalState> is different for each router or not.

Is this a bug, and if not what have I done wrong here? an example of how to use nesting could be useful to work out what should be done instead.

Amazing project btw!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

As suggested on the Embassy Matrix chat, you need to wrap the type into in its own module like so

mod approuter {
    use picoserve::routing::get;

    pub struct GlobalState {
        pub led_on: bool,
    }

    pub type AppRouter = impl picoserve::routing::PathRouter<GlobalState>;
    pub fn make_app() -> picoserve::Router<AppRouter, GlobalState> {
        picoserve::Router::new().route("/", get(|| async move { "Hello World" }))
    }
}


// and then use it like this e.g.

#[embassy_executor::task(pool_size = WEB_TASK_POOL_SIZE)]
async fn web_task(
    id: usize,
    stack: &'static embassy_net::Stack<Device<'static>>,
    app: &'static picoserve::Router<approuter::AppRouter, approuter::GlobalState>,
    config: &'static picoserve::Config<Duration>,
    state: approuter::GlobalState,
) -> ! {
//...

I’ll close the issue when the release is out.