refactor
All checks were successful
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
JMARyA 2024-12-29 20:39:10 +01:00
parent 3837302161
commit 439467f730
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 45 additions and 32 deletions

View file

@ -62,6 +62,7 @@ pub fn to_uuid(id: &str) -> Result<uuid::Uuid, ApiError> {
///
/// # Returns
/// * `ApiError` - A `BadRequest` error with a JSON payload describing the issue.
#[must_use]
pub fn no_uuid_error() -> ApiError {
api_error("No valid UUID")
}
@ -76,6 +77,7 @@ pub fn no_uuid_error() -> ApiError {
///
/// # Returns
/// * `ApiError` - A `BadRequest` error with a JSON payload describing the issue.
#[must_use]
pub fn api_error(msg: &str) -> ApiError {
BadRequest(json!({
"error": msg

View file

@ -14,6 +14,7 @@ pub struct DataResponse {
}
impl DataResponse {
#[must_use]
pub fn new(data: Vec<u8>, content_type: &str, cache_duration: Option<u64>) -> Self {
Self {
data,
@ -47,11 +48,10 @@ impl<'r> Responder<'r, 'static> for DataResponse {
}
// Add caching headers for static files
let cache_control_header = if let Some(duration) = self.cache_duration {
Header::new("Cache-Control", format!("public, max-age={}", duration))
} else {
Header::new("Cache-Control", "no-cache")
};
let cache_control_header = self.cache_duration.map_or_else(
|| Header::new("Cache-Control", "no-cache"),
|duration| Header::new("Cache-Control", format!("public, max-age={duration}")),
);
Ok(Response::build()
.header(cache_control_header)

View file

@ -17,11 +17,7 @@ impl<'r> FromRequest<'r> for RequestContext {
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
rocket::outcome::Outcome::Success(RequestContext {
is_htmx: !req
.headers()
.get("HX-Request")
.collect::<Vec<&str>>()
.is_empty(),
is_htmx: req.headers().get("HX-Request").next().is_some(),
})
}
}

View file

@ -48,6 +48,7 @@ impl RespondRaw for StringResponse {
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `application/json`, and the JSON-encoded body.
#[must_use]
pub fn respond_json(json: &serde_json::Value) -> StringResponse {
(
Status::Ok,
@ -65,6 +66,7 @@ pub fn respond_json(json: &serde_json::Value) -> StringResponse {
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `text/html`, and the HTML content as the body.
#[must_use]
pub fn respond_html(html: &str) -> StringResponse {
(Status::Ok, (ContentType::HTML, html.to_string()))
}
@ -76,6 +78,7 @@ pub fn respond_html(html: &str) -> StringResponse {
///
/// # Returns
/// A `StringResponse` with status `200 OK`, content type `text/javascript`, and the JS content as the body.
#[must_use]
pub fn respond_script(script: &str) -> StringResponse {
(Status::Ok, (ContentType::JavaScript, script.to_string()))
}
@ -89,6 +92,7 @@ pub fn respond_script(script: &str) -> StringResponse {
///
/// # Returns
/// A `RawResponse` containing the provided status, content type, and body.
pub fn respond_with(status: Status, content_type: ContentType, body: Vec<u8>) -> RawResponse {
#[must_use]
pub const fn respond_with(status: Status, content_type: ContentType, body: Vec<u8>) -> RawResponse {
(status, (content_type, body))
}