use crate::components::app_shell::app_shell;
use crate::components::div::div;
use crate::utils::defaults::DEFAULT_ROBOT_TXT;
use crate::utils::form_data::compile_form_data;
use crate::utils::types::SharedAppState;
use crate::utils::{HTMLResult, RUMString};
use crate::{rumtk_web_get_api_endpoint, rumtk_web_get_component, rumtk_web_render_component, RUMWebData, RUMWebResponse, RouterForm};
use axum::body::Body;
use axum::http::Response;
use axum::response::{Html, IntoResponse};
use rumtk_core::strings::rumtk_format;
pub async fn default_robots_matcher(
_path: Vec<RUMString>,
_params: RUMWebData,
_state: SharedAppState,
) -> HTMLResult {
RUMWebResponse::into_get_response(DEFAULT_ROBOT_TXT).into_html_result()
}
pub async fn default_page_matcher(
path: Vec<RUMString>,
params: RUMWebData,
state: SharedAppState,
) -> HTMLResult {
let path_components = match path.first() {
Some(x) => x.split('/').collect::<Vec<&str>>(),
None => Vec::new(),
};
app_shell(&path_components, ¶ms, state)
}
pub async fn default_api_matcher(
path: RUMString,
params: RUMWebData,
mut form: RouterForm,
state: SharedAppState,
) -> HTMLResult {
let form_data = compile_form_data(&mut form).await?;
let api_endpoint = match rumtk_web_get_api_endpoint!(&path) {
Some(endpoint) => endpoint,
None => return Err(rumtk_format!("Requested endpoint is not registered!"))
};
api_endpoint(path, params, form_data, state)
}
pub async fn default_component_matcher(
path: Vec<RUMString>,
params: RUMWebData,
state: SharedAppState,
) -> HTMLResult {
let path_components = match path.first() {
Some(x) => x.split('/').collect::<Vec<&str>>(),
None => Vec::new(),
};
let component = match path_components.last() {
Some(component) => component,
None => return Err(RUMString::from("Missing component name to fetch!")),
};
rumtk_web_render_component!(component, &[""], params, state)
}
pub fn match_maker(match_response: HTMLResult) -> Response<Body> {
match match_response {
Ok(res) => res.into_response(),
Err(e) => Html(String::default()).into_response(),
}
}
#[macro_export]
macro_rules! rumtk_web_fetch {
( $matcher:expr ) => {{
use axum::extract::{Path, Query, State};
use axum::response::{Html, Response};
use $crate::matcher::match_maker;
use $crate::utils::types::{RouterAppState, RouterComponents, RouterForm, RouterParams};
async |Path(path_params): RouterComponents,
Query(params): RouterParams,
State(state): RouterAppState|
-> Response {
let r = $matcher(path_params, params, state).await;
match_maker(r)
}
}};
}
#[macro_export]
macro_rules! rumtk_web_api_process {
( $matcher:expr ) => {{
use axum::extract::{Multipart, Path, Query, State};
use axum::response::{Html, Response};
use $crate::matcher::match_maker;
use $crate::utils::types::{RouterAPIPath, RouterAppState, RouterForm, RouterParams};
async |Path(path_params): RouterAPIPath,
Query(params): RouterParams,
State(state): RouterAppState,
mut form: RouterForm|
-> Response {
let r = $matcher(path_params, params, form, state).await;
match_maker(r)
}
}};
}