rumtk_web/utils/
matcher.rs1use crate::components::app::app_shell::app_shell;
22use crate::components::job_loader::job_loader;
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_render_template, 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 let page = app_shell(&path_components, ¶ms, state)?;
53
54 rumtk_web_render_template!(page)
55}
56
57pub async fn default_job_matcher(
58 path: Vec<RUMString>,
59 params: RUMWebData,
60 state: SharedAppState,
61) -> HTMLResult {
62 let loader = job_loader(&[], ¶ms, state)?;
63
64 rumtk_web_render_template!(loader)
65}
66
67pub async fn default_api_matcher(
68 path: RUMString,
69 params: RUMWebData,
70 mut form: RouterForm,
71 state: SharedAppState,
72) -> HTMLResult {
73 let form_data = compile_form_data(&mut form).await?;
74 let api_endpoint = match rumtk_web_get_api_endpoint!(&path) {
75 Some(endpoint) => endpoint,
76 None => return Err(rumtk_format!("Requested endpoint is not registered!"))
77 };
78 api_endpoint(path, params, form_data, state)
79}
80
81pub fn match_maker(match_response: HTMLResult) -> Response<Body> {
82 match match_response {
83 Ok(res) => res.into_response(),
84 Err(e) => {
85 println!("Error: {}", e);
86 Html(String::default()).into_response()
87 },
88 }
89}
90
91#[macro_export]
92macro_rules! rumtk_web_fetch {
93 ( $matcher:expr ) => {{
94 use axum::extract::{Path, Query, State};
95 use axum::response::{Html, Response};
96 use $crate::matcher::match_maker;
97 use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
98
99 async |Path(path_params): RouterComponents,
100 Query(params): RouterParams,
101 State(state): RouterAppState|
102 -> Response {
103 let r = $matcher(path_params, params, state).await;
104 match_maker(r)
105 }
106 }};
107}
108
109#[macro_export]
110macro_rules! rumtk_web_api_process {
111 ( $matcher:expr ) => {{
112 use axum::extract::{Multipart, Path, Query, State};
113 use axum::response::{Html, Response};
114 use $crate::matcher::match_maker;
115 use $crate::utils::types::{RouterAPIPath, RouterAppState, RouterForm, RouterParams};
116
117 async |Path(path_params): RouterAPIPath,
118 Query(params): RouterParams,
119 State(state): RouterAppState,
120 mut form: RouterForm|
121 -> Response {
122 let r = $matcher(path_params, params, form, state).await;
123 match_maker(r)
124 }
125 }};
126}