rumtk_web/utils/
response.rs1use axum::body::Body;
26use axum::response::{Html, IntoResponse, Redirect, Response};
27use rumtk_core::strings::{RUMString, RUMStringConversions};
28
29pub type HTMLBody = Html<String>;
30pub type RedirectBody = Redirect;
31
32#[derive(Default, Debug)]
33pub enum RUMWebRedirect {
34 Redirect(RUMString),
35 RedirectTemporary(RUMString),
36 RedirectPermanent(RUMString),
37 #[default]
38 None,
39}
40
41#[derive(Default, Debug)]
42pub enum RUMWebResponse {
43 GetResponse(HTMLBody),
44 RedirectResponse(RedirectBody),
45 RedirectTemporaryResponse(RedirectBody),
46 RedirectPermanentResponse(RedirectBody),
47 #[default]
48 None,
49}
50
51pub type HTMLResult = Result<RUMWebResponse, RUMString>;
52
53impl RUMWebResponse {
55 pub fn is_redirect(&self) -> bool {
56 match self {
57 RUMWebResponse::RedirectResponse(_) => true,
58 RUMWebResponse::RedirectTemporaryResponse(_) => true,
59 RUMWebResponse::RedirectPermanentResponse(_) => true,
60 _ => false,
61 }
62 }
63
64 pub fn to_rumstring(&self) -> RUMString {
65 match self {
66 RUMWebResponse::GetResponse(res) => res.0.to_rumstring(),
67 _ => RUMString::default(),
68 }
69 }
70
71 pub fn into_html_result(self) -> HTMLResult {
72 Ok(self)
73 }
74}
75
76impl IntoResponse for RUMWebResponse {
77 fn into_response(self) -> Response<Body> {
78 match self {
79 RUMWebResponse::GetResponse(r) => r.into_response(),
80 RUMWebResponse::RedirectResponse(r) => r.into_response(),
81 RUMWebResponse::RedirectTemporaryResponse(r) => r.into_response(),
82 RUMWebResponse::RedirectPermanentResponse(r) => r.into_response(),
83 RUMWebResponse::None => Html(String::default()).into_response(),
84 }
85 }
86}
87
88impl RUMWebRedirect {
89 pub fn into_web_response(self, default: Option<String>) -> RUMWebResponse {
90 match self {
91 RUMWebRedirect::Redirect(url) => {
92 RUMWebResponse::RedirectResponse(RedirectBody::to(&url))
93 }
94 RUMWebRedirect::RedirectTemporary(url) => {
95 RUMWebResponse::RedirectTemporaryResponse(RedirectBody::temporary(&url))
96 }
97 RUMWebRedirect::RedirectPermanent(url) => {
98 RUMWebResponse::RedirectPermanentResponse(RedirectBody::permanent(&url))
99 }
100 RUMWebRedirect::None => {
101 RUMWebResponse::GetResponse(HTMLBody::from(default.unwrap_or(String::default())))
102 }
103 }
104 }
105}