rumtk_web/utils/
response.rs1use axum::body::Body;
24use axum::http::StatusCode;
25use axum::response::{Html, IntoResponse, Redirect, Response};
26use rumtk_core::strings::{RUMString, RUMStringConversions, ToCompactString};
27
28pub type HTMLResponse = Response<Body>;
29
30#[derive(Debug)]
31pub struct HTMLBody(Html<String>);
32#[derive(Debug)]
33pub struct RedirectBody(Redirect);
34
35#[derive(Default, Debug)]
36pub enum RUMWebRedirect {
37 Redirect(RUMString),
38 RedirectTemporary(RUMString),
39 RedirectPermanent(RUMString),
40 #[default]
41 None,
42}
43
44#[derive(Default, Debug, PartialEq)]
45pub enum RUMWebResponse {
46 GetResponse(HTMLBody),
47 RedirectResponse(RedirectBody),
48 RedirectTemporaryResponse(RedirectBody),
49 RedirectPermanentResponse(RedirectBody),
50 #[default]
51 None,
52}
53
54pub type HTMLResult = Result<RUMWebResponse, RUMString>;
55
56impl RUMWebResponse {
58 pub fn is_redirect(&self) -> bool {
59 match self {
60 RUMWebResponse::RedirectResponse(_) => true,
61 RUMWebResponse::RedirectTemporaryResponse(_) => true,
62 RUMWebResponse::RedirectPermanentResponse(_) => true,
63 _ => false,
64 }
65 }
66
67 pub fn to_rumstring(&self) -> RUMString {
68 match self {
69 RUMWebResponse::GetResponse(res) => res.to_rumstring(),
70 _ => RUMString::default(),
71 }
72 }
73
74 pub fn get_url(&self) -> RUMString {
75 match self {
76 RUMWebResponse::RedirectResponse(res) => res.location(),
77 RUMWebResponse::RedirectTemporaryResponse(res) => res.location(),
78 RUMWebResponse::RedirectPermanentResponse(res) => res.location(),
79 _ => RUMString::default(),
80 }
81 }
82
83 pub fn get_code(&self) -> StatusCode {
84 match self {
85 RUMWebResponse::RedirectResponse(res) => res.status_code(),
86 RUMWebResponse::RedirectTemporaryResponse(res) => res.status_code(),
87 RUMWebResponse::RedirectPermanentResponse(res) => res.status_code(),
88 _ => StatusCode::OK,
89 }
90 }
91
92 pub fn into_html_result(self) -> HTMLResult {
93 Ok(self)
94 }
95
96 pub fn into_get_response(data: &str) -> Self {
97 RUMWebResponse::GetResponse(HTMLBody::from(String::from(data)))
98 }
99}
100
101impl IntoResponse for RUMWebResponse {
102 fn into_response(self) -> HTMLResponse {
103 match self {
104 RUMWebResponse::GetResponse(r) => r.into_response(),
105 RUMWebResponse::RedirectResponse(r) => r.into_response(),
106 RUMWebResponse::RedirectTemporaryResponse(r) => r.into_response(),
107 RUMWebResponse::RedirectPermanentResponse(r) => r.into_response(),
108 RUMWebResponse::None => Html(String::default()).into_response(),
109 }
110 }
111}
112
113impl RUMWebRedirect {
114 pub fn into_web_response(self, default: Option<String>) -> RUMWebResponse {
115 match self {
116 RUMWebRedirect::Redirect(url) => {
117 RUMWebResponse::RedirectResponse(RedirectBody::to(&url))
118 }
119 RUMWebRedirect::RedirectTemporary(url) => {
120 RUMWebResponse::RedirectTemporaryResponse(RedirectBody::temporary(&url))
121 }
122 RUMWebRedirect::RedirectPermanent(url) => {
123 RUMWebResponse::RedirectPermanentResponse(RedirectBody::permanent(&url))
124 }
125 RUMWebRedirect::None => {
126 RUMWebResponse::GetResponse(HTMLBody::from(default.unwrap_or(String::default())))
127 }
128 }
129 }
130}
131
132impl HTMLBody {
133 pub fn from(body: String) -> Self {
134 Self(Html(body))
135 }
136
137 pub fn into_response(self) -> HTMLResponse {
138 self.0.into_response()
139 }
140
141 pub fn to_rumstring(&self) -> RUMString {
142 self.0 .0.to_compact_string()
143 }
144}
145
146impl PartialEq for HTMLBody {
147 fn eq(&self, other: &Self) -> bool {
148 self.0 .0 == other.0 .0
149 }
150}
151
152impl RedirectBody {
153 pub fn to(url: &RUMString) -> Self {
154 RedirectBody(Redirect::to(url.as_str()))
155 }
156
157 pub fn temporary(url: &RUMString) -> Self {
158 RedirectBody(Redirect::temporary(url.as_str()))
159 }
160
161 pub fn permanent(url: &RUMString) -> Self {
162 RedirectBody(Redirect::permanent(url.as_str()))
163 }
164
165 pub fn location(&self) -> RUMString {
166 self.0.location().to_rumstring()
167 }
168
169 pub fn status_code(&self) -> StatusCode {
170 self.0.status_code()
171 }
172
173 pub fn into_response(self) -> HTMLResponse {
174 self.0.into_response()
175 }
176}
177
178impl PartialEq for RedirectBody {
179 fn eq(&self, other: &Self) -> bool {
180 self.0.location() == other.0.location() && self.0.status_code() == other.0.status_code()
181 }
182}