In Go I can return a function from a function, like this:
func newHandler(arg string) http.HandlerFunc {
return service.Handler(arg)
}
// in other files
handler := newHandler(config.Arg)
router.route("/", func(group *router.Group) {
group.GET("", handler)
})
In Rust is really hard to me today understand how to do this. I'm desperately trying with code like this:
use axum::{handler::Handler, response::Html, response::IntoResponse, routing::get, Router};
fn new_handler(arg: &str) {
async fn custom_handler() -> impl IntoResponse {
Html(source(HandlerConfig::new(arg)))
}
}
let handler = new_handler(&config.arg);
let router = Router::new().route("/", get(handler))
but I get this error:
error[E0277]: the trait bound `(): Handler<_, _>` is not satisfied
--> src\router.rs:22:50
|
22 | router = router.route("/", get(handler));
| --- ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for `()`
| |
| required by a bound introduced by this call
|
note: required by a bound in `axum::routing::get`
--> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\axum-0.5.4\src\routing\method_routing.rs:394:1
|
394 | top_level_handler_fn!(get, GET);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `axum::routing::get`
If I use this code instead it works:
async fn custom_handler() -> impl IntoResponse {
Html(source(HandlerConfig::new(arg)))
}
let router = Router::new().route("/", get(custom_handler))
Why that error?