Skip to main content

rumtk_web/utils/
response.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21
22/* Responses */
23use axum::body::Body;
24use axum::http::StatusCode;
25use axum::response::{Html, IntoResponse, Redirect, Response};
26use rumtk_core::strings::RUMString;
27
28pub type HTMLResponse = Response<Body>;
29
30#[derive(Debug, Clone)]
31pub struct HTMLBody(Html<String>);
32#[derive(Debug, Clone)]
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, Clone)]
45pub enum RUMWebResponse {
46    GetResponse(HTMLBody),
47    RedirectResponse(RedirectBody),
48    RedirectTemporaryResponse(RedirectBody),
49    RedirectPermanentResponse(RedirectBody),
50    #[default]
51    None,
52}
53
54pub type ComponentResult<T> = Result<T, RUMString>;
55pub type HTMLResult = Result<RUMWebResponse, RUMString>;
56
57/* Implementations */
58impl RUMWebResponse {
59    pub fn is_redirect(&self) -> bool {
60        match self {
61            RUMWebResponse::RedirectResponse(_) => true,
62            RUMWebResponse::RedirectTemporaryResponse(_) => true,
63            RUMWebResponse::RedirectPermanentResponse(_) => true,
64            _ => false,
65        }
66    }
67
68    pub fn to_string(&self) -> RUMString {
69        match self {
70            RUMWebResponse::GetResponse(res) => res.to_string(),
71            _ => RUMString::default(),
72        }
73    }
74
75    pub fn get_url(&self) -> RUMString {
76        match self {
77            RUMWebResponse::RedirectResponse(res) => res.location(),
78            RUMWebResponse::RedirectTemporaryResponse(res) => res.location(),
79            RUMWebResponse::RedirectPermanentResponse(res) => res.location(),
80            _ => RUMString::default(),
81        }
82    }
83
84    pub fn get_code(&self) -> StatusCode {
85        match self {
86            RUMWebResponse::RedirectResponse(res) => res.status_code(),
87            RUMWebResponse::RedirectTemporaryResponse(res) => res.status_code(),
88            RUMWebResponse::RedirectPermanentResponse(res) => res.status_code(),
89            _ => StatusCode::OK,
90        }
91    }
92
93    pub fn into_html_result(self) -> HTMLResult {
94        Ok(self)
95    }
96
97    pub fn into_get_response(data: &str) -> Self {
98        RUMWebResponse::GetResponse(HTMLBody::from(String::from(data)))
99    }
100}
101
102impl IntoResponse for RUMWebResponse {
103    fn into_response(self) -> HTMLResponse {
104        match self {
105            RUMWebResponse::GetResponse(r) => r.into_response(),
106            RUMWebResponse::RedirectResponse(r) => r.into_response(),
107            RUMWebResponse::RedirectTemporaryResponse(r) => r.into_response(),
108            RUMWebResponse::RedirectPermanentResponse(r) => r.into_response(),
109            RUMWebResponse::None => Html(String::default()).into_response(),
110        }
111    }
112}
113
114impl RUMWebRedirect {
115    pub fn into_web_response(self, default: Option<String>) -> RUMWebResponse {
116        match self {
117            RUMWebRedirect::Redirect(url) => {
118                RUMWebResponse::RedirectResponse(RedirectBody::to(&url))
119            }
120            RUMWebRedirect::RedirectTemporary(url) => {
121                RUMWebResponse::RedirectTemporaryResponse(RedirectBody::temporary(&url))
122            }
123            RUMWebRedirect::RedirectPermanent(url) => {
124                RUMWebResponse::RedirectPermanentResponse(RedirectBody::permanent(&url))
125            }
126            RUMWebRedirect::None => {
127                RUMWebResponse::GetResponse(HTMLBody::from(default.unwrap_or(String::default())))
128            }
129        }
130    }
131}
132
133impl HTMLBody {
134    pub fn from(body: String) -> Self {
135        Self(Html(body))
136    }
137
138    pub fn into_response(self) -> HTMLResponse {
139        self.0.into_response()
140    }
141
142    pub fn to_string(&self) -> RUMString {
143        self.0 .0.to_string()
144    }
145}
146
147impl PartialEq for HTMLBody {
148    fn eq(&self, other: &Self) -> bool {
149        self.0 .0 == other.0 .0
150    }
151}
152
153impl RedirectBody {
154    pub fn to(url: &RUMString) -> Self {
155        RedirectBody(Redirect::to(url.as_str()))
156    }
157
158    pub fn temporary(url: &RUMString) -> Self {
159        RedirectBody(Redirect::temporary(url.as_str()))
160    }
161
162    pub fn permanent(url: &RUMString) -> Self {
163        RedirectBody(Redirect::permanent(url.as_str()))
164    }
165
166    pub fn location(&self) -> RUMString {
167        self.0.location().to_string()
168    }
169
170    pub fn status_code(&self) -> StatusCode {
171        self.0.status_code()
172    }
173
174    pub fn into_response(self) -> HTMLResponse {
175        self.0.into_response()
176    }
177}
178
179impl PartialEq for RedirectBody {
180    fn eq(&self, other: &Self) -> bool {
181        self.0.location() == other.0.location() && self.0.status_code() == other.0.status_code()
182    }
183}