Skip to content

Commit

Permalink
use phf for icon extension set
Browse files Browse the repository at this point in the history
  • Loading branch information
b5 committed Oct 13, 2022
1 parent 0d9e7c2 commit 71dc871
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion iroh-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ reqwest = { version = "0.11.10", features = ["rustls-tls"], default-features = f
hex-literal = "0.3.4"
hex = "0.4.3"
http-body = "0.4.5"
lazy_static = "1.4.0"
phf = { version = "0.11", features = ["macros"] }

[dev-dependencies]
axum-macros = "0.2.0" # use #[axum_macros::debug_handler] for better error messages on handlers
Expand Down
1 change: 0 additions & 1 deletion iroh-gateway/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ async fn serve_fs_dir<T: ContentLoader + std::marker::Unpin>(
let mut el = HashMap::new();
let path = match accum.last() {
Some(prev) => {
let base = prev.get("path");
match prev.get("path") {
Some(base) => format!("/{}/{}", base, encode(path_el)),
None => format!("/{}", encode(path_el)),
Expand Down
16 changes: 7 additions & 9 deletions iroh-gateway/src/templates.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
use lazy_static::lazy_static;
use std::{collections::HashSet, ffi::OsStr, path::Path};
use phf::{Set, phf_set};
use std::{ffi::OsStr, path::Path};

pub const DIR_LIST_TEMPLATE: &str = include_str!("../assets/dir_list.html");
pub const NOT_FOUND_TEMPLATE: &str = include_str!("../assets/404.html");
pub const STYLESHEET: &str = include_str!("../assets/style.css");
pub const ICONS_STYLESHEET: &str = include_str!("../assets/icons.css");

lazy_static! {
static ref KNOWN_ICONS: HashSet<&'static str> = HashSet::from([
".aac", ".aiff", ".ai", ".avi", ".bmp", ".c", ".cpp", ".css", ".dat", ".dmg", ".doc",
static KNOWN_ICONS: Set<&'static str> = phf_set! {
".aac", ".aiff", ".ai", ".avi", ".bmp", ".c", ".cpp", ".css", ".dat", ".dmg", ".doc",
".dotx", ".dwg", ".dxf", ".eps", ".exe", ".flv", ".gif", ".h", ".hpp", ".html", ".ics",
".iso", ".java", ".jpg", ".jpeg", ".js", ".key", ".less", ".mid", ".mkv", ".mov", ".mp3",
".mp4", ".mpg", ".odf", ".ods", ".odt", ".otp", ".ots", ".ott", ".pdf", ".php", ".png",
".ppt", ".psd", ".py", ".qt", ".rar", ".rb", ".rtf", ".sass", ".scss", ".sql", ".tga",
".tgz", ".tiff", ".txt", ".wav", ".wmv", ".xls", ".xlsx", ".xml", ".yml", ".zip",
]);
}
};


pub fn icon_class_name(path: &str) -> String {
let ext = Path::new(path)
.extension()
.and_then(OsStr::to_str)
.unwrap_or("");

let icon = KNOWN_ICONS.get(ext).unwrap_or(&"_blank");
let icon = KNOWN_ICONS.contains(ext).then(|| ext).unwrap_or(&"_blank");
format!("icon-{}", icon)
}

0 comments on commit 71dc871

Please sign in to comment.