diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..0645269 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,17 @@ +use axum::body::Full; +use axum::http::{StatusCode}; +use axum::response::{IntoResponse, Response}; + +pub enum TimeBannerError { + ParseError(String), + RenderError(String), + RasterizeError(String), +} + +pub fn get_error_response(error: TimeBannerError) -> (StatusCode, Full) { + match error { + TimeBannerError::RenderError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Template Could Not Be Rendered :: {}", msg))), + TimeBannerError::ParseError(msg) => (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", msg))), + TimeBannerError::RasterizeError(msg) => (StatusCode::INTERNAL_SERVER_ERROR, Full::from(format!("Failed to rasterize :: {}", msg))) + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9968658..f3961b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ mod abbr; mod routes; mod parse; mod template; +mod error; #[tokio::main] diff --git a/src/routes.rs b/src/routes.rs index b3b1436..9b57c6b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,13 +1,10 @@ -use std::io::Read; -use std::process::Output; -use std::time::SystemTime; - use axum::{http::StatusCode, response::IntoResponse}; use axum::body::{Bytes, Full}; use axum::extract::{Path}; use axum::http::{header}; use axum::response::Response; -use chrono::{DateTime, FixedOffset, NaiveDateTime, Offset, Utc}; +use chrono::{DateTime, NaiveDateTime, Offset, Utc}; +use crate::error::{get_error_response, TimeBannerError}; use crate::parse::split_on_extension; @@ -21,12 +18,6 @@ fn parse_path(path: &str) -> (&str, &str) { .unwrap() } -enum TimeBannerError { - ParseError(String), - RenderError(String), - RasterizeError(String), -} - fn handle_rasterize(data: String, extension: &str) -> Result<(&str, Bytes), TimeBannerError> { match extension { "svg" => Ok(("image/svg+xml", Bytes::from(data))), @@ -60,10 +51,7 @@ pub async fn implicit_handler(Path(path): Path) -> impl IntoResponse { // Parse epoch let parsed_epoch = raw_time.parse::(); if parsed_epoch.is_err() { - return Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Full::from(format!("Failed to parse epoch :: {}", parsed_epoch.unwrap_err()))) - .unwrap(); + return (StatusCode::BAD_REQUEST, Full::from(format!("Failed to parse epoch :: {}", parsed_epoch.unwrap_err()))).into_response(); } // Convert epoch to DateTime @@ -93,27 +81,8 @@ pub async fn implicit_handler(Path(path): Path) -> impl IntoResponse { let rasterize_result = handle_rasterize(rendered_template.unwrap(), extension); match rasterize_result { Ok((mime_type, bytes)) => { - Response::builder() - .status(StatusCode::OK) - .header(header::CONTENT_TYPE, mime_type) - .body(Full::from(bytes)) - .unwrap() - } - Err(e) => { - match e { - TimeBannerError::RenderError(msg) => Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(Full::from(format!("Template Could Not Be Rendered :: {}", msg))) - .unwrap(), - TimeBannerError::ParseError(msg) => Response::builder() - .status(StatusCode::BAD_REQUEST) - .body(Full::from(format!("Failed to parse epoch :: {}", msg))) - .unwrap(), - TimeBannerError::RasterizeError(msg) => Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(Full::from(format!("Failed to rasterize :: {}", msg))) - .unwrap(), - } + (StatusCode::OK, [(header::CONTENT_TYPE, mime_type)], bytes).into_response() } + Err(e) => get_error_response(e).into_response() } } \ No newline at end of file