1 //! Static files bundled with documentation output.
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.
6 use rustc_data_structures::fx::FxHasher;
8 use std::path::{Path, PathBuf};
11 pub(crate) struct StaticFile {
12 pub(crate) filename: PathBuf,
13 pub(crate) bytes: &'static [u8],
17 fn new(filename: &str, bytes: &'static [u8]) -> StaticFile {
18 Self { filename: static_filename(filename, bytes), bytes }
21 pub(crate) fn minified(&self) -> Vec<u8> {
22 let extension = match self.filename.extension() {
24 None => return self.bytes.to_owned(),
26 if extension == "css" {
27 minifier::css::minify(str::from_utf8(self.bytes).unwrap()).unwrap().to_string().into()
28 } else if extension == "js" {
29 minifier::js::minify(str::from_utf8(self.bytes).unwrap()).to_string().into()
35 pub(crate) fn output_filename(&self) -> &Path {
40 /// The Display implementation for a StaticFile outputs its filename. This makes it
41 /// convenient to interpolate static files into HTML templates.
42 impl fmt::Display for StaticFile {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "{}", self.output_filename().display())
48 /// Insert the provided suffix into a filename just before the extension.
49 pub(crate) fn suffix_path(filename: &str, suffix: &str) -> PathBuf {
50 // We use splitn vs Path::extension here because we might get a filename
51 // like `style.min.css` and we want to process that into
52 // `style-suffix.min.css`. Path::extension would just return `css`
53 // which would result in `style.min-suffix.css` which isn't what we
55 let (base, ext) = filename.split_once('.').unwrap();
56 let filename = format!("{}{}.{}", base, suffix, ext);
60 pub(crate) fn static_filename(filename: &str, contents: &[u8]) -> PathBuf {
61 let filename = filename.rsplit('/').next().unwrap();
62 suffix_path(filename, &static_suffix(contents))
65 fn static_suffix(bytes: &[u8]) -> String {
66 let mut hasher = FxHasher::default();
68 format!("-{:016x}", hasher.finish())
71 macro_rules! static_files {
72 ($($field:ident => $file_path:literal,)+) => {
73 pub(crate) struct StaticFiles {
74 $(pub $field: StaticFile,)+
77 pub(crate) static STATIC_FILES: std::sync::LazyLock<StaticFiles> = std::sync::LazyLock::new(|| StaticFiles {
78 $($field: StaticFile::new($file_path, include_bytes!($file_path)),)+
81 pub(crate) fn for_each<E>(f: impl Fn(&StaticFile) -> Result<(), E>) -> Result<(), E> {
83 $(&STATIC_FILES.$field,)+
93 rustdoc_css => "static/css/rustdoc.css",
94 settings_css => "static/css/settings.css",
95 noscript_css => "static/css/noscript.css",
96 normalize_css => "static/css/normalize.css",
97 main_js => "static/js/main.js",
98 search_js => "static/js/search.js",
99 settings_js => "static/js/settings.js",
100 source_script_js => "static/js/source-script.js",
101 storage_js => "static/js/storage.js",
102 scrape_examples_js => "static/js/scrape-examples.js",
103 wheel_svg => "static/images/wheel.svg",
104 clipboard_svg => "static/images/clipboard.svg",
105 down_arrow_svg => "static/images/down-arrow.svg",
106 toggle_minus_png => "static/images/toggle-minus.svg",
107 toggle_plus_png => "static/images/toggle-plus.svg",
108 copyright => "static/COPYRIGHT.txt",
109 license_apache => "static/LICENSE-APACHE.txt",
110 license_mit => "static/LICENSE-MIT.txt",
111 rust_logo_svg => "static/images/rust-logo.svg",
112 rust_favicon_svg => "static/images/favicon.svg",
113 rust_favicon_png_16 => "static/images/favicon-16x16.png",
114 rust_favicon_png_32 => "static/images/favicon-32x32.png",
115 theme_light_css => "static/css/themes/light.css",
116 theme_dark_css => "static/css/themes/dark.css",
117 theme_ayu_css => "static/css/themes/ayu.css",
118 fira_sans_regular => "static/fonts/FiraSans-Regular.woff2",
119 fira_sans_medium => "static/fonts/FiraSans-Medium.woff2",
120 fira_sans_license => "static/fonts/FiraSans-LICENSE.txt",
121 source_serif_4_regular => "static/fonts/SourceSerif4-Regular.ttf.woff2",
122 source_serif_4_bold => "static/fonts/SourceSerif4-Bold.ttf.woff2",
123 source_serif_4_italic => "static/fonts/SourceSerif4-It.ttf.woff2",
124 source_serif_4_license => "static/fonts/SourceSerif4-LICENSE.md",
125 source_code_pro_regular => "static/fonts/SourceCodePro-Regular.ttf.woff2",
126 source_code_pro_semibold => "static/fonts/SourceCodePro-Semibold.ttf.woff2",
127 source_code_pro_italic => "static/fonts/SourceCodePro-It.ttf.woff2",
128 source_code_pro_license => "static/fonts/SourceCodePro-LICENSE.txt",
129 nanum_barun_gothic_regular => "static/fonts/NanumBarunGothic.ttf.woff2",
130 nanum_barun_gothic_license => "static/fonts/NanumBarunGothic-LICENSE.txt",
133 pub(crate) static SCRAPE_EXAMPLES_HELP_MD: &str = include_str!("static/js/scrape-examples.js");