Remove some code duplication

This commit is contained in:
Eric Huss 2025-02-20 10:19:04 -08:00
parent 8a9ecd212d
commit e7b69114ed

View file

@ -1,6 +1,7 @@
//! Support for writing static files. //! Support for writing static files.
use log::{debug, warn}; use log::{debug, warn};
use once_cell::sync::Lazy;
use crate::config::HtmlConfig; use crate::config::HtmlConfig;
use crate::errors::*; use crate::errors::*;
@ -231,27 +232,32 @@ impl StaticFiles {
use std::io::Read; use std::io::Read;
// The `{{ resource "name" }}` directive in static resources look like // The `{{ resource "name" }}` directive in static resources look like
// handlebars syntax, even if they technically aren't. // handlebars syntax, even if they technically aren't.
let resource = Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap(); static RESOURCE: Lazy<Regex> =
for static_file in self.static_files { Lazy::new(|| Regex::new(r#"\{\{ resource "([^"]+)" \}\}"#).unwrap());
match static_file { fn replace_all<'a>(
StaticFile::Builtin { filename, data } => { hash_map: &HashMap<String, String>,
debug!("Writing builtin -> {}", filename); data: &'a [u8],
let hash_map = &self.hash_map; filename: &str,
let data = if filename.ends_with(".css") || filename.ends_with(".js") { ) -> Cow<'a, [u8]> {
resource.replace_all(&data, |captures: &Captures<'_>| { RESOURCE.replace_all(data, move |captures: &Captures<'_>| {
let name = captures let name = captures
.get(1) .get(1)
.expect("capture 1 in resource regex") .expect("capture 1 in resource regex")
.as_bytes(); .as_bytes();
let name = let name = std::str::from_utf8(name).expect("resource name with invalid utf8");
std::str::from_utf8(name).expect("resource name with invalid utf8"); let resource_filename = hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
let resource_filename =
hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
let path_to_root = utils::fs::path_to_root(&filename); let path_to_root = utils::fs::path_to_root(&filename);
format!("{}{}", path_to_root, resource_filename) format!("{}{}", path_to_root, resource_filename)
.as_bytes() .as_bytes()
.to_owned() .to_owned()
}) })
}
for static_file in &self.static_files {
match static_file {
StaticFile::Builtin { filename, data } => {
debug!("Writing builtin -> {}", filename);
let data = if filename.ends_with(".css") || filename.ends_with(".js") {
replace_all(&self.hash_map, data, filename)
} else { } else {
Cow::Borrowed(&data[..]) Cow::Borrowed(&data[..])
}; };
@ -272,25 +278,11 @@ impl StaticFiles {
.with_context(|| format!("Unable to create {}", parent.display()))?; .with_context(|| format!("Unable to create {}", parent.display()))?;
} }
if filename.ends_with(".css") || filename.ends_with(".js") { if filename.ends_with(".css") || filename.ends_with(".js") {
let hash_map = &self.hash_map;
let mut file = File::open(input_location)?; let mut file = File::open(input_location)?;
let mut data = Vec::new(); let mut data = Vec::new();
file.read_to_end(&mut data)?; file.read_to_end(&mut data)?;
let data = resource.replace_all(&data, |captures: &Captures<'_>| { let data = replace_all(&self.hash_map, &data, filename);
let name = captures write_file(destination, filename, &data)?;
.get(1)
.expect("capture 1 in resource regex")
.as_bytes();
let name =
std::str::from_utf8(name).expect("resource name with invalid utf8");
let resource_filename =
hash_map.get(name).map(|s| &s[..]).unwrap_or(&name);
let path_to_root = utils::fs::path_to_root(&filename);
format!("{}{}", path_to_root, resource_filename)
.as_bytes()
.to_owned()
});
write_file(destination, &filename, &data)?;
} else { } else {
fs::copy(&input_location, &output_location).with_context(|| { fs::copy(&input_location, &output_location).with_context(|| {
format!( format!(