axum_starter/effect_utils/
router.rs1use std::marker::PhantomData;
2
3use axum::{handler::Handler, routing::MethodRouter, Router};
4
5use crate::prepare_behave::effect_traits::PrepareRouteEffect;
6
7pub struct Route<S>(&'static str, MethodRouter<S>);
12
13impl<S> Route<S> {
14 pub fn new(router: &'static str, service: MethodRouter<S>) -> Self {
15 Self(router, service)
16 }
17}
18
19impl<S: 'static> PrepareRouteEffect<S> for Route<S> {
20 fn set_route(self, route: Router<S>) -> Router<S>
21 where
22 S: Clone + Send + Sync + 'static,
23 {
24 route.route(self.0, self.1)
25 }
26}
27
28pub struct Merge<R>(R);
33
34impl<R> Merge<R> {
35 pub fn new(merge: R) -> Self
36 where
37 Router: From<R>,
38 {
39 Self(merge)
40 }
41}
42impl<S, R> PrepareRouteEffect<S> for Merge<R>
43where
44 R: 'static,
45 Router<S>: From<R>,
46 S: Clone + Send + Sync + 'static,
47{
48 fn set_route(self, route: Router<S>) -> Router<S> {
49 route.merge(self.0)
50 }
51}
52
53pub struct Nest<S> {
58 path: &'static str,
59 router: Router<S>,
60}
61
62impl<S> Nest<S> {
63 pub fn new(path: &'static str, router: Router<S>) -> Self {
64 Self { path, router }
65 }
66}
67
68impl<S> PrepareRouteEffect<S> for Nest<S>
69where
70 S: Clone + Send + Sync + 'static,
71{
72 fn set_route(self, route: Router<S>) -> Router<S> {
73 route.nest(self.path, self.router)
74 }
75}
76
77pub struct Fallback<H, T> {
82 handle: H,
83 __phantom: PhantomData<T>,
84}
85
86impl<R, T> Fallback<R, T> {
87 pub fn new(handle: R) -> Self {
88 Self {
89 handle,
90 __phantom: PhantomData,
91 }
92 }
93}
94impl<S, R, T> PrepareRouteEffect<S> for Fallback<R, T>
95where
96 R: Handler<T, S>,
97 T: 'static,
98 S: Clone + Send + Sync + 'static,
99{
100 fn set_route(self, route: Router<S>) -> Router<S> {
101 route.fallback(self.handle)
102 }
103}