-
SummaryI'm trying to do something similar to what's done in this example: https://github.com/tokio-rs/axum/blob/main/examples/key-value-store/src/main.rs#L116 But I am getting an error that I cannot fully understand nor fix, I hope someone can shine some light on this issue :) Compilation errorerror[E0277]: the trait bound `Router: From<Router<AppState>>` is not satisfied --> compmgr_server/src/main.rs:77:16 | 77 | .merge(web_index()) | ----- ^^^^^^^^^^^ the trait `From<Router<AppState>>` is not implemented for `Router`, which is required by `Router<AppState>: Into<Router>` | | | required by a bound introduced by this call | = note: required for `Router<AppState>` to implement `Into<Router>` note: required by a bound in `Router::<S>::merge` --> /home/sofia/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.4/src/routing/mod.rs:226:12 | 224 | pub fn merge<R>(self, other: R) -> Self | ----- required by a bound in this associated function 225 | where 226 | R: Into<Router<S>>, | ^^^^^^^^^^^^^^^ required by this bound in `Router::<S>::merge` // src/main.rs
// A macro to simplify the process of representing `askama` templates
macro_rules! template_route {
//--snip
($name:ident, $path:literal => $file:literal: $struct:ident ($id:literal)) => {
#[derive(Template)]
#[template(path = $file)]
struct $struct<'a> {
active: &'a str,
}
fn $name() -> Router<$crate::AppState> {
async fn route() -> impl IntoResponse {
$struct { active: $id }
}
Router::<$crate::AppState>::new().route($path, get(route))
}
}
}
template_route!(web_index, "/" => "dashboard.html": IndexTemplate ("dashboard"));
template_route!(first_boot_tmpl, "/boot" => "first_boot.html": FirstBootTemplate ("first_boot"));
fn other_router(token: Option<String>) -> Router<crate::AppState> {
#[debug_handler]
async fn handler(
State(state): State<crate::AppState>,
//--snip
) -> Html<&'static str> {
//--snip
}
if token.is_some() {
Router::<crate::AppState>::new()
.route("/boot", put(handler))
.merge(first_boot_tmpl())
} else {
Router::<crate::AppState>::new()
}
}
#[derive(Clone)]
struct AppState {
// --snip
}
#[tokio::main]
async fn main() {
// --snip
let state = AppState {
// --snip
};
let token: Option<String> = // --snip
let router = Router::new()
.with_state(state)
// --snip
.merge(web_index())
.merge(other_router(token))
.layer(TraceLayer::new_for_http())
.layer(CookieManagerLayer::new());
} axum version0.7.4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
with_state
only supplies the state to all routes added prior to the call. You need to supply the state after merging.