update + htmx

This commit is contained in:
JMARyA 2024-12-22 18:37:45 +01:00
parent 40d69b61e1
commit 291949b8c6
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
8 changed files with 277 additions and 10 deletions

8
src/htmx.rs Normal file
View file

@ -0,0 +1,8 @@
use rocket::get;
use crate::request::{StringResponse, respond_script};
#[get("/assets/htmx.min.js")]
pub fn htmx_script_route() -> StringResponse {
respond_script(include_str!("htmx.min.js"))
}

View file

@ -2,6 +2,7 @@ use tokio::sync::OnceCell;
pub mod auth;
pub mod format;
pub mod htmx;
pub mod page;
pub mod request;
pub mod result;

View file

@ -12,5 +12,83 @@ pub use context::RequestContext;
/// HTTP response containing a string payload.
pub type StringResponse = (Status, (ContentType, String));
pub trait RespondString {
fn respond(self) -> StringResponse;
}
impl RespondString for String {
fn respond(self) -> StringResponse {
(Status::Ok, (ContentType::Text, self))
}
}
/// HTTP response containing raw binary data.
pub type RawResponse = (Status, (ContentType, Vec<u8>));
pub trait RespondRaw {
fn respond(self) -> RawResponse;
}
impl RespondRaw for Vec<u8> {
fn respond(self) -> RawResponse {
(Status::Ok, (ContentType::Binary, self))
}
}
impl RespondRaw for StringResponse {
fn respond(self) -> RawResponse {
(self.0, (self.1.0, self.1.1.into_bytes()))
}
}
/// Helper function to create a JSON HTTP response.
///
/// # Parameters
/// - `json`: A reference to a `serde_json::Value` representing the JSON payload.
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `application/json`, and the JSON-encoded body.
pub fn respond_json(json: &serde_json::Value) -> StringResponse {
(
Status::Ok,
(
ContentType::JSON,
serde_json::to_string(json).expect("Failed to serialize JSON"),
),
)
}
/// Helper function to create an HTML HTTP response.
///
/// # Parameters
/// - `html`: A string slice (`&str`) containing the HTML content.
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `text/html`, and the HTML content as the body.
pub fn respond_html(html: &str) -> StringResponse {
(Status::Ok, (ContentType::HTML, html.to_string()))
}
/// Helper function to create an JS HTTP response.
///
/// # Parameters
/// - `html`: A string slice (`&str`) containing the JS content.
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `text/javascript`, and the JS content as the body.
pub fn respond_script(script: &str) -> StringResponse {
(Status::Ok, (ContentType::JavaScript, script.to_string()))
}
/// Creates a custom HTTP response with the specified status, content type, and body.
///
/// # Parameters
/// - `status`: The HTTP status code to include in the response.
/// - `content_type`: The content type of the response body (e.g., `ContentType::JSON` or `ContentType::HTML`).
/// - `body`: The response body as a `Vec<u8>`.
///
/// # Returns
/// A `RawResponse` containing the provided status, content type, and body.
pub fn respond_with(status: Status, content_type: ContentType, body: Vec<u8>) -> RawResponse {
(status, (content_type, body))
}