Skip to content

Commit

Permalink
🤖 cargo-fmt auto-update
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Oct 27, 2023
1 parent c2851d2 commit f5a874b
Showing 1 changed file with 49 additions and 52 deletions.
101 changes: 49 additions & 52 deletions src/internet_identity/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ pub fn init_assets() {
body_hash,
);

certified_assets.assets.insert(path, (headers, Box::new(content)));
certified_assets
.assets
.insert(path, (headers, Box::new(content)));
}
});
}
Expand Down Expand Up @@ -274,12 +276,7 @@ fn collect_assets_from_dir(dir: &Dir) -> Vec<(String, Vec<u8>, ContentEncoding,
// TODO: add canister/lightweight test to ensure assets are loaded up
// & certified successfully
for asspath in asspaths {
assets.push((
asspath,
content.clone(),
encoding,
content_type,
));
assets.push((asspath, content.clone(), encoding, content_type));
}
}
assets
Expand Down Expand Up @@ -313,51 +310,51 @@ fn filepath_to_urlpaths(file_path: String) -> Vec<String> {
// TODO: explain always return starting with /

// Create paths, WITHOUT leading slash (leading lash is prepended later)
fn inner (elements: Vec<&str>, last: &str) -> Vec<String> {
if elements.is_empty() && last == "index.html" {
// The special case of the root index.html, which we serve
// on both "/" and "/index.html"
vec!["".to_string(), "index.html".to_string()]
} else if last == "index.html" {
// An index.html in a subpath
let page = elements.join("/").to_string();
vec![
format!("{page}"),
format!("{page}/"),
format!("{page}/index.html"),
]
} else if let Some(page) = last.strip_suffix(".html") {
// A (non-index) HTML page
let mut elements = elements.to_vec();
elements.push(page);
let page = elements.join("/").to_string();
vec![
format!("{page}"),
format!("{page}/"),
format!("{page}/index.html"),
]
} else if let Some(file) = last.strip_suffix(".gz") {
// A gzipped asset; remove suffix and retry
// XXX: this recursion is safe (i.e. not infinite) because
// we always reduce the argument (remove ".gz")
inner(elements, file)
} else {
// The default cases for any asset
let mut elements = elements.to_vec().clone();
elements.push(last);
let prefix = elements.join("/").to_string();
vec![prefix]
}
}

let paths = match file_path.split('/').collect::<Vec<&str>>().split_last() {
None => {
// The argument was an empty string
// We can't really do much about this, so we fail explicitly
panic!("Expected non-empty filepath for asset");
}
Some((last, elements)) => inner(elements.to_vec(), last)
};
fn inner(elements: Vec<&str>, last: &str) -> Vec<String> {
if elements.is_empty() && last == "index.html" {
// The special case of the root index.html, which we serve
// on both "/" and "/index.html"
vec!["".to_string(), "index.html".to_string()]
} else if last == "index.html" {
// An index.html in a subpath
let page = elements.join("/").to_string();
vec![
format!("{page}"),
format!("{page}/"),
format!("{page}/index.html"),
]
} else if let Some(page) = last.strip_suffix(".html") {
// A (non-index) HTML page
let mut elements = elements.to_vec();
elements.push(page);
let page = elements.join("/").to_string();
vec![
format!("{page}"),
format!("{page}/"),
format!("{page}/index.html"),
]
} else if let Some(file) = last.strip_suffix(".gz") {
// A gzipped asset; remove suffix and retry
// XXX: this recursion is safe (i.e. not infinite) because
// we always reduce the argument (remove ".gz")
inner(elements, file)
} else {
// The default cases for any asset
let mut elements = elements.to_vec().clone();
elements.push(last);
let prefix = elements.join("/").to_string();
vec![prefix]
}
}

let paths = match file_path.split('/').collect::<Vec<&str>>().split_last() {
None => {
// The argument was an empty string
// We can't really do much about this, so we fail explicitly
panic!("Expected non-empty filepath for asset");
}
Some((last, elements)) => inner(elements.to_vec(), last),
};

// Prefix everything with "/"
paths.into_iter().map(|path| format!("/{path}")).collect()
Expand Down

0 comments on commit f5a874b

Please sign in to comment.