diff --git a/Cargo.lock b/Cargo.lock index 4dd4641..a582305 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -434,7 +434,7 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "static-serve" -version = "0.2.2" +version = "0.2.3" dependencies = [ "axum", "bytes", @@ -448,7 +448,7 @@ dependencies = [ [[package]] name = "static-serve-macro" -version = "0.2.2" +version = "0.2.3" dependencies = [ "display_full_error", "flate2", diff --git a/Cargo.toml b/Cargo.toml index 67717ed..645d8a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ members = [ resolver = "2" [workspace.package] -version = "0.2.2" +version = "0.2.3" edition = "2021" rust-version = "1.83" description = "A helper for compressing and embedding static assets in an Axum webserver" diff --git a/static-serve-macro/src/error.rs b/static-serve-macro/src/error.rs index c4c6c74..c232402 100644 --- a/static-serve-macro/src/error.rs +++ b/static-serve-macro/src/error.rs @@ -15,6 +15,10 @@ pub(crate) enum Error { InvalidFileExtension(OsString), #[error("Cannot canonicalize assets directory")] CannotCanonicalizeDirectory(#[source] io::Error), + #[error("Cannot canonicalize asset file")] + CannotCanonicalizeFile(#[source] io::Error), + #[error("File path is not utf-8")] + FilePathIsNotUtf8, #[error("Invalid unicode in directory name")] InvalidUnicodeInDirectoryName, #[error("Cannot canonicalize ignore directory")] @@ -33,8 +37,6 @@ pub(crate) enum Error { Glob(#[source] GlobError), #[error("Cannot get entry metadata")] CannotGetMetadata(#[source] io::Error), - #[error("Cannot canonicalize asset file")] - CannotCanonicalizeFile(#[source] io::Error), } struct UnknownFileExtension<'a>(Option<&'a OsStr>); diff --git a/static-serve-macro/src/lib.rs b/static-serve-macro/src/lib.rs index 81c71b2..25b07a7 100644 --- a/static-serve-macro/src/lib.rs +++ b/static-serve-macro/src/lib.rs @@ -385,6 +385,10 @@ fn generate_static_routes( continue; } + let entry = entry + .canonicalize() + .map_err(Error::CannotCanonicalizeFile)?; + let entry_str = entry.to_str().ok_or(Error::FilePathIsNotUtf8)?; let EmbeddedFileInfo { entry_path, content_type, @@ -405,7 +409,12 @@ fn generate_static_routes( #entry_path, #content_type, #etag_str, - #lit_byte_str_contents, + { + // Poor man's `tracked_path` + // https://github.com/rust-lang/rust/issues/99515 + const _: &[u8] = include_bytes!(#entry_str); + #lit_byte_str_contents + }, #maybe_gzip, #maybe_zstd, ); @@ -429,6 +438,7 @@ fn generate_static_handler( let asset_file_abs = Path::new(&asset_file.value()) .canonicalize() .map_err(Error::CannotCanonicalizeFile)?; + let asset_file_abs_str = asset_file_abs.to_str().ok_or(Error::FilePathIsNotUtf8)?; let EmbeddedFileInfo { entry_path: _, @@ -451,7 +461,12 @@ fn generate_static_handler( ::static_serve::static_method_router( #content_type, #etag_str, - #lit_byte_str_contents, + { + // Poor man's `tracked_path` + // https://github.com/rust-lang/rust/issues/99515 + const _: &[u8] = include_bytes!(#asset_file_abs_str); + #lit_byte_str_contents + }, #maybe_gzip, #maybe_zstd, ) diff --git a/static-serve/Cargo.toml b/static-serve/Cargo.toml index 6bb377b..2b3d678 100644 --- a/static-serve/Cargo.toml +++ b/static-serve/Cargo.toml @@ -10,7 +10,7 @@ categories.workspace = true repository.workspace = true [dependencies] -static-serve-macro = { path = "../static-serve-macro", version = "0.2.2" } +static-serve-macro = { path = "../static-serve-macro", version = "=0.2.3" } axum = { version = "0.8", default-features = false } bytes = "1.10"