Skip to content

Commit

Permalink
Move extension translation to a helper
Browse files Browse the repository at this point in the history
  • Loading branch information
przydatek committed Feb 1, 2024
1 parent fdf5a3f commit 3c36b03
Showing 1 changed file with 107 additions and 42 deletions.
149 changes: 107 additions & 42 deletions src/asset_util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,7 @@ pub fn collect_assets(dir: &Dir, html_transformer: Option<fn(&str) -> String>) -
fn collect_assets_rec(dir: &Dir, assets: &mut Vec<Asset>) {
for asset in dir.files() {
let content = asset.contents().to_vec();
let (extension, encoding) = file_extension_and_encoding(asset);
let content_type = match extension {
"css" => ContentType::CSS,
"html" => ContentType::HTML,
"ico" => ContentType::ICO,
"json" => ContentType::JSON,
"js" => ContentType::JS,
"png" => ContentType::PNG,
"svg" => ContentType::SVG,
"webp" => ContentType::WEBP,
"woff2" => ContentType::WOFF2,
ext => panic!(
"Unknown asset type '{}' for asset '{}'",
ext,
asset.path().display()
),
};
let (content_type, encoding) = content_type_and_encoding(asset);

let url_paths = filepath_to_urlpaths(asset.path().to_str().unwrap().to_string());
for url_path in url_paths {
Expand All @@ -464,16 +448,15 @@ fn collect_assets_rec(dir: &Dir, assets: &mut Vec<Asset>) {
}
}

/// Returns the extension of the given file, and the encoding type.
/// If the text after the last dot is "gz" (i.e. this is a gzipped file), then the extension
/// is the text after the second to last dot and the last dot in the file name
/// (e.g. "js" for "some.gzipped.file.js.gz"), and the encoding is `ContentEncoding::GZip`.
/// Otherwise the extension is the text after the last dot in the file name,
/// Returns the content type and the encoding type of the given file, based on the extension(s).
/// If the text after the last dot is "gz" (i.e. this is a gzipped file), then content type
/// is determined by the text after the second to last dot and the last dot in the file name,
/// e.g. `ContentType::JS` for "some.gzipped.file.js.gz", and the encoding is `ContentEncoding::GZip`.
/// Otherwise the content type is determined by the text after the last dot in the file name,
/// and the encoding is `ContentEncoding::Identity`.
/// This corresponds to the file extension for the assets handled by this crate.
fn file_extension_and_encoding<'a>(asset: &'a File) -> (&'a str, ContentEncoding) {
fn content_type_and_encoding<'a>(asset: &'a File) -> (ContentType, ContentEncoding) {
let extension = asset.path().extension().unwrap().to_str().unwrap();
if extension == "gz" {
let (extension, encoding) = if extension == "gz" {
let type_extension = asset
.path()
.file_name()
Expand All @@ -486,7 +469,24 @@ fn file_extension_and_encoding<'a>(asset: &'a File) -> (&'a str, ContentEncoding
(type_extension, ContentEncoding::GZip)
} else {
(extension, ContentEncoding::Identity)
}
};
let content_type = match extension {
"css" => ContentType::CSS,
"html" => ContentType::HTML,
"ico" => ContentType::ICO,
"json" => ContentType::JSON,
"js" => ContentType::JS,
"png" => ContentType::PNG,
"svg" => ContentType::SVG,
"webp" => ContentType::WEBP,
"woff2" => ContentType::WOFF2,
ext => panic!(
"Unknown asset type '{}' for asset '{}'",
ext,
asset.path().display()
),
};
(content_type, encoding)
}

/// Returns the URL paths for a given asset filepath. For instance:
Expand Down Expand Up @@ -616,25 +616,90 @@ fn test_filepath_urlpaths() {
#[test]
fn should_return_correct_extension() {
let path_extension_encoding = [
("path1/some_css_file.css", "css", ContentEncoding::Identity),
("path2/an_html_file.html", "html", ContentEncoding::Identity),
("path3/an_ico_file.ico", "ico", ContentEncoding::Identity),
("path4/json_file.json", "json", ContentEncoding::Identity),
("path5/js_file.js", "js", ContentEncoding::Identity),
("path6/gzipped_js_file.js.gz", "js", ContentEncoding::GZip),
("path7/a_png_file.png", "png", ContentEncoding::Identity),
("path8/svg_file.svg", "svg", ContentEncoding::Identity),
("path9/webp_file.webp", "webp", ContentEncoding::Identity),
("path10/a_file.woff2", "woff2", ContentEncoding::Identity),
("path11/gz_woff2.woff2.gz", "woff2", ContentEncoding::GZip),
("path.dots/ico_file.ico", "ico", ContentEncoding::Identity),
("path13/file.with.dots.js", "js", ContentEncoding::Identity),
("path14.dot/gz_js_file.js.gz", "js", ContentEncoding::GZip),
("path15.dots/.dots.woff2.gz", "woff2", ContentEncoding::GZip),
(
"path1/some_css_file.css",
ContentType::CSS,
ContentEncoding::Identity,
),
(
"path2/an_html_file.html",
ContentType::HTML,
ContentEncoding::Identity,
),
(
"path3/an_ico_file.ico",
ContentType::ICO,
ContentEncoding::Identity,
),
(
"path4/json_file.json",
ContentType::JSON,
ContentEncoding::Identity,
),
(
"path5/js_file.js",
ContentType::JS,
ContentEncoding::Identity,
),
(
"path6/gzipped_js_file.js.gz",
ContentType::JS,
ContentEncoding::GZip,
),
(
"path7/a_png_file.png",
ContentType::PNG,
ContentEncoding::Identity,
),
(
"path8/svg_file.svg",
ContentType::SVG,
ContentEncoding::Identity,
),
(
"path9/webp_file.webp",
ContentType::WEBP,
ContentEncoding::Identity,
),
(
"path10/a_file.woff2",
ContentType::WOFF2,
ContentEncoding::Identity,
),
(
"path11/gz_woff2.woff2.gz",
ContentType::WOFF2,
ContentEncoding::GZip,
),
(
"path12.dot/ico_file.ico",
ContentType::ICO,
ContentEncoding::Identity,
),
(
"path13/file.with.dots.js",
ContentType::JS,
ContentEncoding::Identity,
),
(
"path14.dot/gz_js_file.js.gz",
ContentType::JS,
ContentEncoding::GZip,
),
(
"path15.dots/.dots.woff2.gz",
ContentType::WOFF2,
ContentEncoding::GZip,
),
(
"path16.dot/gz_json_file.json.gz",
ContentType::JSON,
ContentEncoding::GZip,
),
];
for (path, expected_extension, expected_encoding) in path_extension_encoding {
assert_eq!(
file_extension_and_encoding(&File::new(path, &[42])),
content_type_and_encoding(&File::new(path, &[42])),
(expected_extension, expected_encoding)
);
}
Expand Down

0 comments on commit 3c36b03

Please sign in to comment.