rustdoc/html/
static_files.rs

1//! Static files bundled with documentation output.
2//!
3//! All the static files are included here for centralized access in case anything other than the
4//! HTML rendering code (say, the theme checker) needs to access one of these files.
5
6use std::path::{Path, PathBuf};
7use std::{fmt, str};
8
9pub(crate) struct StaticFile {
10    pub(crate) filename: PathBuf,
11    pub(crate) src_bytes: &'static [u8],
12    pub(crate) minified_bytes: &'static [u8],
13}
14
15impl StaticFile {
16    fn new(
17        filename: &str,
18        src_bytes: &'static [u8],
19        minified_bytes: &'static [u8],
20        sha256: &'static str,
21    ) -> StaticFile {
22        Self { filename: static_filename(filename, sha256), src_bytes, minified_bytes }
23    }
24
25    pub(crate) fn output_filename(&self) -> &Path {
26        &self.filename
27    }
28}
29
30/// The Display implementation for a StaticFile outputs its filename. This makes it
31/// convenient to interpolate static files into HTML templates.
32impl fmt::Display for StaticFile {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        write!(f, "{}", self.output_filename().display())
35    }
36}
37
38/// Insert the provided suffix into a filename just before the extension.
39pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf {
40    // We use splitn vs Path::extension here because we might get a filename
41    // like `style.min.css` and we want to process that into
42    // `style-suffix.min.css`.  Path::extension would just return `css`
43    // which would result in `style.min-suffix.css` which isn't what we
44    // want.
45    let (base, ext) = filename.split_once('.').unwrap();
46    let filename = format!("{base}{suffix}.{ext}");
47    filename.into()
48}
49
50pub(crate) fn static_filename(filename: &str, sha256: &str) -> PathBuf {
51    let filename = filename.rsplit('/').next().unwrap();
52    suffix_path(filename, sha256)
53}
54
55macro_rules! static_files {
56    ($($field:ident => $file_path:literal,)+) => {
57        pub(crate) struct StaticFiles {
58            $(pub $field: StaticFile,)+
59        }
60
61        // sha256 files are generated in build.rs
62        pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
63            $($field: StaticFile::new($file_path, include_bytes!($file_path), include_bytes!(concat!(env!("OUT_DIR"), "/", $file_path, ".min")), include_str!(concat!(env!("OUT_DIR"), "/", $file_path, ".sha256"))),)+
64        });
65
66        pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {
67            for sf in [
68            $(&STATIC_FILES.$field,)+
69            ] {
70                f(sf)?
71            }
72            Ok(())
73        }
74    }
75}
76
77static_files! {
78    rustdoc_css => "static/css/rustdoc.css",
79    noscript_css => "static/css/noscript.css",
80    normalize_css => "static/css/normalize.css",
81    main_js => "static/js/main.js",
82    search_js => "static/js/search.js",
83    settings_js => "static/js/settings.js",
84    src_script_js => "static/js/src-script.js",
85    storage_js => "static/js/storage.js",
86    scrape_examples_js => "static/js/scrape-examples.js",
87    copyright => "static/COPYRIGHT.txt",
88    license_apache => "static/LICENSE-APACHE.txt",
89    license_mit => "static/LICENSE-MIT.txt",
90    rust_logo_svg => "static/images/rust-logo.svg",
91    rust_favicon_svg => "static/images/favicon.svg",
92    rust_favicon_png_32 => "static/images/favicon-32x32.png",
93    fira_sans_italic => "static/fonts/FiraSans-Italic.woff2",
94    fira_sans_regular => "static/fonts/FiraSans-Regular.woff2",
95    fira_sans_medium => "static/fonts/FiraSans-Medium.woff2",
96    fira_sans_medium_italic => "static/fonts/FiraSans-MediumItalic.woff2",
97    fira_mono_regular => "static/fonts/FiraMono-Regular.woff2",
98    fira_mono_medium => "static/fonts/FiraMono-Medium.woff2",
99    fira_sans_license => "static/fonts/FiraSans-LICENSE.txt",
100    source_serif_4_regular => "static/fonts/SourceSerif4-Regular.ttf.woff2",
101    source_serif_4_semibold => "static/fonts/SourceSerif4-Semibold.ttf.woff2",
102    source_serif_4_bold => "static/fonts/SourceSerif4-Bold.ttf.woff2",
103    source_serif_4_italic => "static/fonts/SourceSerif4-It.ttf.woff2",
104    source_serif_4_license => "static/fonts/SourceSerif4-LICENSE.md",
105    source_code_pro_regular => "static/fonts/SourceCodePro-Regular.ttf.woff2",
106    source_code_pro_semibold => "static/fonts/SourceCodePro-Semibold.ttf.woff2",
107    source_code_pro_italic => "static/fonts/SourceCodePro-It.ttf.woff2",
108    source_code_pro_license => "static/fonts/SourceCodePro-LICENSE.txt",
109    nanum_barun_gothic_regular => "static/fonts/NanumBarunGothic.ttf.woff2",
110    nanum_barun_gothic_license => "static/fonts/NanumBarunGothic-LICENSE.txt",
111}
112
113pub(crate) static SCRAPE_EXAMPLES_HELP_MD: &str = include_str!("static/scrape-examples-help.md");