#![allow(
dead_code,
clippy::same_name_method,
reason = "PHASE-02 foundation + RustEmbed derive conflict"
)]
use rust_embed::RustEmbed;
#[derive(RustEmbed)]
#[cfg_attr(debug_assertions, folder = "web/map/")]
#[cfg_attr(not(debug_assertions), folder = "web/map/dist/")]
pub(crate) struct Assets;
pub(crate) fn content_type_for(path: &str) -> &'static str {
match path.rsplit('.').next() {
Some("html") => "text/html; charset=utf-8",
Some("css") => "text/css; charset=utf-8",
Some("js") => "application/javascript; charset=utf-8",
Some("svg") => "image/svg+xml",
Some("json") => "application/json",
Some("woff2") => "font/woff2",
_ => "application/octet-stream",
}
}
pub(crate) fn serve_embedded(
path: &str,
) -> Result<(axum::http::HeaderMap, Vec<u8>), crate::map_server::error::MapServerError> {
let ct = content_type_for(path);
let asset = Assets::get(path)
.ok_or_else(|| crate::map_server::error::MapServerError::AssetNotFound(path.to_owned()))?;
let mut headers = axum::http::HeaderMap::new();
headers.insert(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static(ct),
);
Ok((headers, asset.data.to_vec()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn content_type_known_extensions() {
assert_eq!(
content_type_for("app.js"),
"application/javascript; charset=utf-8"
);
assert_eq!(content_type_for("style.css"), "text/css; charset=utf-8");
assert_eq!(content_type_for("index.html"), "text/html; charset=utf-8");
assert_eq!(content_type_for("graph.svg"), "image/svg+xml");
assert_eq!(content_type_for("data.json"), "application/json");
assert_eq!(content_type_for("font.woff2"), "font/woff2");
}
#[test]
fn content_type_unknown_extension() {
assert_eq!(content_type_for("file.bin"), "application/octet-stream");
}
#[test]
fn content_type_no_extension() {
assert_eq!(content_type_for("README"), "application/octet-stream");
}
#[test]
fn assets_get_index_html() {
let asset = Assets::get("index.html");
assert!(asset.is_some(), "index.html should be embedded");
}
#[test]
fn serve_embedded_index_returns_html() {
let result = serve_embedded("index.html");
assert!(result.is_ok(), "index.html should be found");
let (headers, body) = result.unwrap();
assert_eq!(
headers.get("content-type").unwrap(),
"text/html; charset=utf-8"
);
assert!(!body.is_empty());
}
#[test]
fn serve_embedded_missing_returns_asset_not_found() {
let result = serve_embedded("nonexistent.js");
assert!(result.is_err());
match result.unwrap_err() {
crate::map_server::error::MapServerError::AssetNotFound(path) => {
assert_eq!(path, "nonexistent.js");
}
other => panic!("expected AssetNotFound, got {:?}", other),
}
}
}