Skip to content

Commit

Permalink
Handle bad file names.
Browse files Browse the repository at this point in the history
More ways to create a working rust symbol name from a "strange" static
file name.  Illegal characters are replaced by `_`, and if the file
name starts with a number it is prefixed with `n`.
  • Loading branch information
kaj committed Jul 18, 2023
1 parent 93c9bb1 commit d8903e6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,10 @@ project adheres to
Thanks @kornelski!
* Fixed handling of `MULTI_WORD_CONSTANTS` in templates (Issue #129, PR #130).
Thanks @wezm!
* More ways to create a working rust symbol name from a "strange"
static file name. Illegal characters are replaced by `_`, and if
the file name starts with a number it is prefixed with `n` (Issue
#82, PR #132). Thanks @Aedius for reporting!
* Fixed more clippy lints (PR #123, #127). Thanks @vbrandl!
* Updated `rsass` to 0.28.0 and `itertools` to 0.11.0.

Expand Down
16 changes: 15 additions & 1 deletion examples/statics/src/main.rs
Expand Up @@ -59,10 +59,24 @@ fn test_all_statics_known() {
use templates::statics::STATICS;
assert_eq!(
STATICS.iter().map(|s| s.name).collect::<Vec<_>>(),
["foo-JckCHvyv.css", "foo-R-7hhHLr.js", "style-o2rFo1lI.css"]
[
"17-C0lZWdZX.css",
"foo-JckCHvyv.css",
"foo-R-7hhHLr.js",
"style-o2rFo1lI.css"
]
);
}

/// Test for issue #82.
#[test]
fn test_num_gets_prefixed() {
// The rust name is usable in rust.
let style = &templates::statics::n17_css;
// Url name is not prefixed (but has a content hash).
assert_eq!(style.name, "17-C0lZWdZX.css");
}

#[cfg(test)]
fn r2s<Call>(call: Call) -> String
where
Expand Down
1 change: 1 addition & 0 deletions examples/statics/static/17.css
@@ -0,0 +1 @@
body { font-family: serif; }
11 changes: 10 additions & 1 deletion src/staticfiles.rs
Expand Up @@ -517,7 +517,16 @@ impl StaticFile {
content: &impl Display,
suffix: &str,
) -> Result<&mut Self> {
let rust_name = rust_name.replace(['/', '-', '.'], "_");
let mut rust_name =
rust_name.replace(|c: char| !c.is_alphanumeric(), "_");
if rust_name
.as_bytes()
.first()
.map(|c| c.is_ascii_digit())
.unwrap_or(true)
{
rust_name.insert(0, 'n');
}
writeln!(
self.src,
"\n/// From {path:?}\
Expand Down

0 comments on commit d8903e6

Please sign in to comment.