rumtk_web/utils/
matcher.rs1use crate::components::app_shell::app_shell;
24use crate::utils::defaults::DEFAULT_ROBOT_TXT;
25use crate::utils::types::SharedAppState;
26use crate::utils::{HTMLResult, RUMString};
27use crate::{rumtk_web_get_component, RUMWebData};
28use axum::response::Html;
29
30pub async fn default_robots_matcher(
31 _path: Vec<RUMString>,
32 _params: RUMWebData,
33 _state: SharedAppState,
34) -> HTMLResult {
35 Ok(Html::<String>::from(String::from(DEFAULT_ROBOT_TXT)))
36}
37
38pub async fn default_page_matcher(
39 path: Vec<RUMString>,
40 params: RUMWebData,
41 state: SharedAppState,
42) -> HTMLResult {
43 let path_components = match path.first() {
44 Some(x) => x.split('/').collect::<Vec<&str>>(),
45 None => Vec::new(),
46 };
47
48 app_shell(&path_components, ¶ms, state)
50}
51
52pub async fn default_component_matcher(
53 path: Vec<RUMString>,
54 params: RUMWebData,
55 state: SharedAppState,
56) -> HTMLResult {
57 let path_components = match path.first() {
58 Some(x) => x.split('/').collect::<Vec<&str>>(),
59 None => Vec::new(),
60 };
61
62 let component = match path_components.first() {
63 Some(component) => component,
64 None => return Err(RUMString::from("Missing component name to fetch!")),
65 };
66
67 let component = rumtk_web_get_component!(component);
68
69 component(&path_components[1..], ¶ms, state)
70}
71
72#[macro_export]
73macro_rules! rumtk_web_fetch {
74 ( $matcher:expr ) => {{
75 use axum::extract::{Path, Query, State};
76 use axum::response::Html;
77 use $crate::utils::types::{RouterAppState, RouterComponents, RouterParams};
78
79 async |Path(path_params): RouterComponents,
80 Query(params): RouterParams,
81 State(state): RouterAppState|
82 -> Html<String> {
83 match $matcher(path_params, params, state).await {
84 Ok(res) => res,
85 Err(e) => {
86 error!("{}", e);
87 return Html(String::default());
88 }
89 }
90 }
91 }};
92}
93
94#[macro_export]
95macro_rules! rumtk_web_post {
96 ( $matcher:expr ) => {{
97 use axum::extract::{Multipart, Path, Query, State};
98 use axum::response::Html;
99 use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
100
101 async |Path(path_params): RouterComponents,
102 Query(mut params): RouterParams,
103 State(state): RouterAppState,
104 mut Multipart: RouterForm|
105 -> Html<String> {
106 match $matcher(path_params, params, state).await {
107 Ok(res) => res,
108 Err(e) => {
109 error!("{}", e);
110 return Html(String::default());
111 }
112 }
113 }
114 }};
115}