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) => {
91 println!("Error: {}", e);
92 Html(String::default()).into_response()
93 },
94 }
95}
96
97#[macro_export]
98macro_rules! rumtk_web_fetch {
99 ( $matcher:expr ) => {{
100 use axum::extract::{Path, Query, State};
101 use axum::response::{Html, Response};
102 use $crate::matcher::match_maker;
103 use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
104
105 async |Path(path_params): RouterComponents,
106 Query(params): RouterParams,
107 State(state): RouterAppState|
108 -> Response {
109 let r = $matcher(path_params, params, state).await;
110 match_maker(r)
111 }
112 }};
113}
114
115#[macro_export]
116macro_rules! rumtk_web_api_process {
117 ( $matcher:expr ) => {{
118 use axum::extract::{Multipart, Path, Query, State};
119 use axum::response::{Html, Response};
120 use $crate::matcher::match_maker;
121 use $crate::utils::types::{RouterAPIPath, RouterAppState, RouterForm, RouterParams};
122
123 async |Path(path_params): RouterAPIPath,
124 Query(params): RouterParams,
125 State(state): RouterAppState,
126 mut form: RouterForm|
127 -> Response {
128 let r = $matcher(path_params, params, form, state).await;
129 match_maker(r)
130 }
131 }};
132}