Skip to main content

Source src/render/assets.rs

1//! Emit pinned text/binary assets and check CSS font resources before publication.2use std::path::Path;34macro_rules! font {5    ($name:literal) => {6        (7            concat!("static.files/", $name),8            include_bytes!(concat!("../../assets/vendor/rustdoc/fonts/", $name)),9        )10    };11}12macro_rules! font_license {13    ($name:literal) => {14        (15            concat!("licenses/", $name),16            include_bytes!(concat!("../../assets/vendor/rustdoc/fonts/", $name)),17        )18    };19}20const FILES: &[(&str, &[u8])] = &[21    (22        "licenses/SUMMARY.md",23        include_bytes!("../../assets/vendor/rustdoc/SUMMARY.md"),24    ),25    (26        "static.files/summary.js",27        include_bytes!("../../assets/summary.js"),28    ),29    (30        "licenses/HELP.md",31        include_bytes!("../../assets/vendor/rustdoc/HELP.md"),32    ),33    (34        "static.files/help.js",35        include_bytes!("../../assets/help.js"),36    ),37    (38        "licenses/SUGGESTIONS.md",39        include_bytes!("../../assets/vendor/rustdoc/SUGGESTIONS.md"),40    ),41    (42        "static.files/suggestions.js",43        include_bytes!("../../assets/suggestions.js"),44    ),45    (46        "static.files/settings-tabs.css",47        include_bytes!("../../assets/vendor/rustdoc/settings-tabs.css"),48    ),49    (50        "licenses/SETTINGS.md",51        include_bytes!("../../assets/vendor/rustdoc/SETTINGS.md"),52    ),53    (54        "static.files/settings.js",55        include_bytes!("../../assets/settings.js"),56    ),57    (58        "static.files/settings-storage.js",59        include_bytes!("../../assets/settings-storage.js"),60    ),61    (62        "static.files/search.js",63        include_bytes!("../../assets/search.js"),64    ),65    (66        "static.files/search-engine.js",67        include_bytes!("../../assets/search-engine.js"),68    ),69    (70        "licenses/CLIPPY-CATEGORIES.md",71        include_bytes!("../../assets/vendor/clippy/README.md"),72    ),73    (74        "licenses/clippy-MIT.txt",75        include_bytes!("../../assets/vendor/clippy/LICENSE-MIT"),76    ),77    (78        "static.files/favicon.svg",79        include_bytes!("../../assets/favicon/favicon.svg"),80    ),81    (82        "static.files/favicon.ico",83        include_bytes!("../../assets/favicon/favicon.ico"),84    ),85    (86        "licenses/FAVICON.md",87        include_bytes!("../../assets/favicon/README.md"),88    ),89    (90        "static.files/rustdoc.css",91        include_bytes!("../../assets/vendor/rustdoc/rustdoc.css"),92    ),93    (94        "static.files/normalize.css",95        include_bytes!("../../assets/vendor/rustdoc/normalize.css"),96    ),97    (98        "static.files/fonts.css",99        include_bytes!("../../assets/vendor/rustdoc/fonts.css"),100    ),101    (102        "static.files/clippy-html.css",103        include_bytes!("../../assets/clippy-html.css"),104    ),105    (106        "licenses/rustdoc-MIT.txt",107        include_bytes!("../../assets/vendor/rustdoc/LICENSE-MIT"),108    ),109    (110        "licenses/rustdoc-COPYRIGHT.txt",111        include_bytes!("../../assets/vendor/rustdoc/COPYRIGHT.txt"),112    ),113    (114        "licenses/normalize-MIT.txt",115        include_bytes!("../../assets/vendor/rustdoc/normalize-LICENSE-MIT"),116    ),117    (118        "licenses/UPSTREAM.md",119        include_bytes!("../../assets/vendor/rustdoc/UPSTREAM.md"),120    ),121    (122        "licenses/FONTS.md",123        include_bytes!("../../assets/vendor/rustdoc/FONTS.md"),124    ),125    (126        "licenses/HIGHLIGHTING.md",127        include_bytes!("../../assets/vendor/rustdoc/HIGHLIGHTING.md"),128    ),129    (130        "licenses/SEARCH.md",131        include_bytes!("../../assets/vendor/rustdoc/SEARCH.md"),132    ),133    (134        "licenses/THIRD-PARTY-NOTICES.md",135        include_bytes!("../../THIRD-PARTY-NOTICES.md"),136    ),137    (138        "licenses/generator-MIT.txt",139        include_bytes!("../../LICENSE-MIT"),140    ),141    (142        "licenses/generator-APACHE.txt",143        include_bytes!("../../LICENSE-APACHE"),144    ),145    font!("FiraSans-Regular-0fe48ade.woff2"),146    font!("FiraSans-Italic-81dc35de.woff2"),147    font!("FiraSans-Medium-e1aa3f0a.woff2"),148    font!("FiraSans-MediumItalic-ccf7e434.woff2"),149    font!("SourceSerif4-Regular-6b053e98.ttf.woff2"),150    font!("SourceSerif4-It-ca3b17ed.ttf.woff2"),151    font!("SourceSerif4-Semibold-457a13ac.ttf.woff2"),152    font!("SourceSerif4-Bold-6d4fd4c0.ttf.woff2"),153    font!("SourceCodePro-Regular-8badfe75.ttf.woff2"),154    font!("SourceCodePro-It-fc8b9304.ttf.woff2"),155    font!("SourceCodePro-Semibold-aa29a496.ttf.woff2"),156    font!("NanumBarunGothic-13b3dcba.ttf.woff2"),157    font_license!("Fira-LICENSE.txt"),158    font_license!("SourceCodePro-LICENSE.txt"),159    font_license!("SourceSerif4-LICENSE.md"),160    font_license!("NanumBarunGothic-LICENSE.txt"),161];162163pub(super) fn write_assets(destination: &Path) -> Result<(), String> {164    for directory in ["static.files", "licenses"] {165        std::fs::create_dir_all(destination.join(directory)).map_err(|e| e.to_string())?;166    }167    for (name, bytes) in FILES {168        std::fs::write(destination.join(name), bytes).map_err(|e| e.to_string())?;169    }170    Ok(())171}172173pub(super) fn validate_font_resources(root: &Path) -> Result<(), String> {174    let path = root.join("static.files/fonts.css");175    let css = match std::fs::read_to_string(&path) {176        Ok(css) => css,177        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),178        Err(error) => return Err(error.to_string()),179    };180    let base = url::Url::from_file_path(&path).map_err(|_| "Invalid font CSS URL")?; warning: clippy::ignored_unit_patterns181    // This is the fixed, generator-owned stylesheet, not an arbitrary CSS parser.182    for reference in css.split("url(").skip(1) {183        let (value, _) = reference.split_once(')').ok_or("Unclosed font URL")?;184        let value = value.trim().trim_matches(['\'', '"']);185        let linked = base.join(value).map_err(|e| e.to_string())?;186        let target = linked187            .to_file_path()188            .map_err(|_| "Nonlocal font resource")? warning: clippy::ignored_unit_patterns189            .canonicalize()190            .map_err(|e| format!("Missing font resource {value}: {e}"))?;191        if !target.starts_with(root)192            || !target.is_file()193            || !target.extension().is_some_and(|ext| ext == "woff2") warning: clippy::nonminimal_bool194        {195            return Err(format!("Invalid font resource: {value}"));196        }197    }198    Ok(())199}200

Findings in this file§

warning clippy::ignored_unit_patterns §

matching over `()` is more explicit

src/render/assets.rs:180:57

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/render/assets.rs:180:57

    let base = url::Url::from_file_path(&path).map_err(|_| "Invalid font CSS URL")?;

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#ignored_unit_patterns

help: use `()` instead of `_`

Suggestion: use `()` instead of `_`

MachineApplicable

--- "a/src/render/assets.rs"
+++ "b/src/render/assets.rs"
@@ -177,7 +177,7 @@
         Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
         Err(error) => return Err(error.to_string()),
     };
-    let base = url::Url::from_file_path(&path).map_err(|_| "Invalid font CSS URL")?;
+    let base = url::Url::from_file_path(&path).map_err(|()| "Invalid font CSS URL")?;
     // This is the fixed, generator-owned stylesheet, not an arbitrary CSS parser.
     for reference in css.split("url(").skip(1) {
         let (value, _) = reference.split_once(')').ok_or("Unclosed font URL")?;
Rendered diagnostic (text)
warning: matching over `()` is more explicit
   --> src/render/assets.rs:180:57
    |
180 |     let base = url::Url::from_file_path(&path).map_err(|_| "Invalid font CSS URL")?;
    |                                                         ^ help: use `()` instead of `_`: `()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#ignored_unit_patterns

warning clippy::ignored_unit_patterns §

matching over `()` is more explicit

src/render/assets.rs:188:23

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/render/assets.rs:188:23

            .map_err(|_| "Nonlocal font resource")?

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#ignored_unit_patterns

help: use `()` instead of `_`

Suggestion: use `()` instead of `_`

MachineApplicable

--- "a/src/render/assets.rs"
+++ "b/src/render/assets.rs"
@@ -185,7 +185,7 @@
         let linked = base.join(value).map_err(|e| e.to_string())?;
         let target = linked
             .to_file_path()
-            .map_err(|_| "Nonlocal font resource")?
+            .map_err(|()| "Nonlocal font resource")?
             .canonicalize()
             .map_err(|e| format!("Missing font resource {value}: {e}"))?;
         if !target.starts_with(root)
Rendered diagnostic (text)
warning: matching over `()` is more explicit
   --> src/render/assets.rs:188:23
    |
188 |             .map_err(|_| "Nonlocal font resource")?
    |                       ^ help: use `()` instead of `_`: `()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#ignored_unit_patterns

warning clippy::nonminimal_bool §

this boolean expression can be simplified

src/render/assets.rs:193:16

Clippy · Category: pedantic · cargo-clippy-html

Official Clippy documentation

Targets (1)
  • cargo-clippy-html (bin)

Primary: src/render/assets.rs:193:16

            || !target.extension().is_some_and(|ext| ext == "woff2")

help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#nonminimal_bool

note: `-W clippy::nonminimal-bool` implied by `-W clippy::pedantic`

help: to override `-W clippy::pedantic` add `#[allow(clippy::nonminimal_bool)]`

help: try

Suggestion: try

MachineApplicable

--- "a/src/render/assets.rs"
+++ "b/src/render/assets.rs"
@@ -190,7 +190,7 @@
             .map_err(|e| format!("Missing font resource {value}: {e}"))?;
         if !target.starts_with(root)
             || !target.is_file()
-            || !target.extension().is_some_and(|ext| ext == "woff2")
+            || target.extension().is_none_or(|ext| ext != "woff2")
         {
             return Err(format!("Invalid font resource: {value}"));
         }
Rendered diagnostic (text)
warning: this boolean expression can be simplified
   --> src/render/assets.rs:193:16
    |
193 |             || !target.extension().is_some_and(|ext| ext == "woff2")
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `target.extension().is_none_or(|ext| ext != "woff2")`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.100.0/index.html#nonminimal_bool
    = note: `-W clippy::nonminimal-bool` implied by `-W clippy::pedantic`
    = help: to override `-W clippy::pedantic` add `#[allow(clippy::nonminimal_bool)]`