Skip to content

Commit dada280

Browse files
feat(macro): make macro rerun when any of the included files changes
1 parent 41141b7 commit dada280

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

static-serve-macro/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ pub(crate) enum Error {
1717
CannotCanonicalizeDirectory(#[source] io::Error),
1818
#[error("Cannot canonicalize asset file")]
1919
CannotCanonicalizeFile(#[source] io::Error),
20+
#[error("File path is not utf-8")]
21+
FilePathIsNotUtf8,
2022
#[error("Invalid unicode in directory name")]
2123
InvalidUnicodeInDirectoryName,
2224
#[error("Cannot canonicalize ignore directory")]

static-serve-macro/src/lib.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ fn generate_static_routes(
385385
continue;
386386
}
387387

388+
let entry = entry
389+
.canonicalize()
390+
.map_err(Error::CannotCanonicalizeFile)?;
391+
let entry_str = entry.to_str().ok_or(Error::FilePathIsNotUtf8)?;
388392
let EmbeddedFileInfo {
389393
entry_path,
390394
content_type,
@@ -405,7 +409,12 @@ fn generate_static_routes(
405409
#entry_path,
406410
#content_type,
407411
#etag_str,
408-
#lit_byte_str_contents,
412+
{
413+
// Poor man's `tracked_path`
414+
// https://github.com/rust-lang/rust/issues/99515
415+
const _: &[u8] = include_bytes!(#entry_str);
416+
#lit_byte_str_contents
417+
},
409418
#maybe_gzip,
410419
#maybe_zstd,
411420
);
@@ -429,6 +438,7 @@ fn generate_static_handler(
429438
let asset_file_abs = Path::new(&asset_file.value())
430439
.canonicalize()
431440
.map_err(Error::CannotCanonicalizeFile)?;
441+
let asset_file_abs_str = asset_file_abs.to_str().ok_or(Error::FilePathIsNotUtf8)?;
432442

433443
let EmbeddedFileInfo {
434444
entry_path: _,
@@ -451,7 +461,12 @@ fn generate_static_handler(
451461
::static_serve::static_method_router(
452462
#content_type,
453463
#etag_str,
454-
#lit_byte_str_contents,
464+
{
465+
// Poor man's `tracked_path`
466+
// https://github.com/rust-lang/rust/issues/99515
467+
const _: &[u8] = include_bytes!(#asset_file_abs_str);
468+
#lit_byte_str_contents
469+
},
455470
#maybe_gzip,
456471
#maybe_zstd,
457472
)

0 commit comments

Comments
 (0)