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.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23
24/* Responses */
25use 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
53/* Implementations */
54impl 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}