spring_web/
handler.rs

1use crate::Router;
2
3pub use inventory::submit;
4
5/// TypeHandler is used to configure the spring-macro marked route handler
6pub trait TypedHandlerRegistrar: Send + Sync + 'static {
7    /// install route
8    fn install_route(&self, router: Router) -> Router;
9}
10
11/// Add typed routes marked with procedural macros
12pub trait TypeRouter {
13    /// Add typed routes marked with procedural macros
14    fn typed_route<F: TypedHandlerRegistrar>(self, factory: F) -> Self;
15}
16
17impl TypeRouter for Router {
18    fn typed_route<F: TypedHandlerRegistrar>(self, factory: F) -> Self {
19        factory.install_route(self)
20    }
21}
22
23inventory::collect!(&'static dyn TypedHandlerRegistrar);
24
25/// auto_config
26#[macro_export]
27macro_rules! submit_typed_handler {
28    ($ty:ident) => {
29        ::spring_web::handler::submit! {
30            &$ty as &dyn ::spring_web::handler::TypedHandlerRegistrar
31        }
32    };
33}
34
35/// auto_config
36pub fn auto_router() -> Router {
37    let mut router = Router::new();
38    for handler in inventory::iter::<&dyn TypedHandlerRegistrar> {
39        router = handler.install_route(router);
40    }
41    router
42}