rumtk_web/utils/
matcher.rs1use crate::components::app_shell::app_shell;
22use crate::components::div::div;
23use crate::utils::defaults::DEFAULT_ROBOT_TXT;
24use crate::utils::form_data::compile_form_data;
25use crate::utils::types::SharedAppState;
26use crate::utils::{HTMLResult, RUMString};
27use crate::{rumtk_web_get_api_endpoint, rumtk_web_get_component, rumtk_web_render_component, RUMWebData, RUMWebResponse, RouterForm};
28use axum::body::Body;
29use axum::http::Response;
30use axum::response::{Html, IntoResponse};
31use rumtk_core::strings::rumtk_format;
32
33pub async fn default_robots_matcher(
34 _path: Vec<RUMString>,
35 _params: RUMWebData,
36 _state: SharedAppState,
37) -> HTMLResult {
38 RUMWebResponse::into_get_response(DEFAULT_ROBOT_TXT).into_html_result()
39}
40
41pub async fn default_page_matcher(
42 path: Vec<RUMString>,
43 params: RUMWebData,
44 state: SharedAppState,
45) -> HTMLResult {
46 let path_components = match path.first() {
47 Some(x) => x.split('/').collect::<Vec<&str>>(),
48 None => Vec::new(),
49 };
50
51 app_shell(&path_components, ¶ms, state)
53}
54
55pub async fn default_api_matcher(
56 path: RUMString,
57 params: RUMWebData,
58 mut form: RouterForm,
59 state: SharedAppState,
60) -> HTMLResult {
61 let form_data = compile_form_data(&mut form).await?;
62 let api_endpoint = match rumtk_web_get_api_endpoint!(&path) {
63 Some(endpoint) => endpoint,
64 None => return Err(rumtk_format!("Requested endpoint is not registered!"))
65 };
66 api_endpoint(path, params, form_data, state)
67}
68
69pub async fn default_component_matcher(
70 path: Vec<RUMString>,
71 params: RUMWebData,
72 state: SharedAppState,
73) -> HTMLResult {
74 let path_components = match path.first() {
75 Some(x) => x.split('/').collect::<Vec<&str>>(),
76 None => Vec::new(),
77 };
78
79 let component = match path_components.last() {
80 Some(component) => component,
81 None => return Err(RUMString::from("Missing component name to fetch!")),
82 };
83
84 rumtk_web_render_component!(component, &[""], params, state)
85}
86
87pub fn match_maker(match_response: HTMLResult) -> Response<Body> {
88 match match_response {
89 Ok(res) => res.into_response(),
90 Err(e) => Html(String::default()).into_response(),
91 }
92}
93
94#[macro_export]
95macro_rules! rumtk_web_fetch {
96 ( $matcher:expr ) => {{
97 use axum::extract::{Path, Query, State};
98 use axum::response::{Html, Response};
99 use $crate::matcher::match_maker;
100 use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
101
102 async |Path(path_params): RouterComponents,
103 Query(params): RouterParams,
104 State(state): RouterAppState|
105 -> Response {
106 let r = $matcher(path_params, params, state).await;
107 match_maker(r)
108 }
109 }};
110}
111
112#[macro_export]
113macro_rules! rumtk_web_api_process {
114 ( $matcher:expr ) => {{
115 use axum::extract::{Multipart, Path, Query, State};
116 use axum::response::{Html, Response};
117 use $crate::matcher::match_maker;
118 use $crate::utils::types::{RouterAPIPath, RouterAppState, RouterForm, RouterParams};
119
120 async |Path(path_params): RouterAPIPath,
121 Query(params): RouterParams,
122 State(state): RouterAppState,
123 mut form: RouterForm|
124 -> Response {
125 let r = $matcher(path_params, params, form, state).await;
126 match_maker(r)
127 }
128 }};
129}