diff options
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/assets.rs | 32 | ||||
| -rw-r--r-- | src/ui/mime.rs | 22 | ||||
| -rw-r--r-- | src/ui/mod.rs | 1 | ||||
| -rw-r--r-- | src/ui/routes/path.rs | 7 |
4 files changed, 51 insertions, 11 deletions
diff --git a/src/ui/assets.rs b/src/ui/assets.rs index 342ba59..6a7563a 100644 --- a/src/ui/assets.rs +++ b/src/ui/assets.rs @@ -1,29 +1,31 @@ +use ::mime::{FromStrError, Mime}; use axum::{ http::{header, StatusCode}, response::{IntoResponse, Response}, }; -use mime_guess::Mime; use rust_embed::EmbeddedFile; -use crate::{error::Internal, ui::error::NotFound}; +use super::{error::NotFound, mime}; +use crate::error::Internal; #[derive(rust_embed::Embed)] #[folder = "target/ui"] pub struct Assets; impl Assets { - pub fn load(path: impl AsRef<str>) -> Result<Asset, NotFound<String>> { + pub fn load(path: impl AsRef<str>) -> Result<Asset, Error> { let path = path.as_ref(); - let mime = mime_guess::from_path(path).first_or_octet_stream(); + let mime = mime::from_path(path)?; Self::get(path) .map(|file| Asset(mime, file)) - .ok_or(NotFound(format!("not found: {path}"))) + .ok_or(Error::NotFound(path.into())) } pub fn index() -> Result<Asset, Internal> { // "not found" in this case really is an internal error, as it should - // never happen. `index.html` is a known-valid path. + // never happen. `index.html` is a known-valid path with a known-valid + // file extension. Ok(Self::load("index.html")?) } } @@ -41,3 +43,21 @@ impl IntoResponse for Asset { .into_response() } } + +#[derive(Debug, thiserror::Error)] +pub enum Error { + #[error("not found: {0}")] + NotFound(String), + #[error(transparent)] + Mime(#[from] FromStrError), +} + +impl IntoResponse for Error { + fn into_response(self) -> Response { + #[allow(clippy::match_wildcard_for_single_variants)] + match self { + Self::NotFound(_) => NotFound(self.to_string()).into_response(), + other => Internal::from(other).into_response(), + } + } +} diff --git a/src/ui/mime.rs b/src/ui/mime.rs new file mode 100644 index 0000000..9c724f0 --- /dev/null +++ b/src/ui/mime.rs @@ -0,0 +1,22 @@ +use mime::Mime; +use unix_path::Path; + +// Extremely manual; using `std::path` here would result in platform-dependent behaviour when it's not appropriate (the URLs passed here always use `/` and are parsed like URLs). Using `unix_path` might be an option, but it's not clearly +pub fn from_path<P>(path: P) -> Result<Mime, mime::FromStrError> +where + P: AsRef<Path>, +{ + let path = path.as_ref(); + let extension = path.extension().and_then(|ext| ext.to_str()); + let mime = match extension { + Some("css") => "text/css; charset=utf-8", + Some("js") => "text/javascript; charset=utf-8", + Some("json") => "application/json", + Some("html") => "text/html; charset=utf-8", + Some("png") => "image/png", + _ => "application/octet-stream", + }; + let mime = mime.parse()?; + + Ok(mime) +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index c145382..f8caa48 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,6 +1,7 @@ mod assets; mod error; mod middleware; +mod mime; mod routes; pub use self::routes::router; diff --git a/src/ui/routes/path.rs b/src/ui/routes/path.rs index 2e9a657..a387552 100644 --- a/src/ui/routes/path.rs +++ b/src/ui/routes/path.rs @@ -1,12 +1,9 @@ pub mod get { use axum::extract::Path; - use crate::ui::{ - assets::{Asset, Assets}, - error::NotFound, - }; + use crate::ui::assets::{Asset, Assets, Error}; - pub async fn handler(Path(path): Path<String>) -> Result<Asset, NotFound<String>> { + pub async fn handler(Path(path): Path<String>) -> Result<Asset, Error> { Assets::load(path) } } |
