Skip to content

Commit

Permalink
refactor(urls_table): Improve build_directory_listing_page implemen…
Browse files Browse the repository at this point in the history
…tation

Signed-off-by: PrajwalCH <[email protected]>
  • Loading branch information
prajwalch committed Dec 26, 2023
1 parent 4cf86e2 commit 2f82290
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/urls_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::fmt::Write;
use std::io;
use std::io::{Error, ErrorKind};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -139,38 +140,36 @@ impl UrlsTable {
url
}

fn build_directory_listing_page(&self, url: &str, dir_path: &Path) -> Vec<u8> {
let mut matched_entries = self
fn build_directory_listing_page(&self, url: &str, fs_path: &Path) -> Vec<u8> {
let mut entries = self
.table
.iter()
.filter(|(_, url_entry)| url_entry.fs_path.parent().is_some_and(|p| p == dir_path))
.filter(|(_, entry)| entry.fs_path.parent().is_some_and(|p| p == fs_path))
.collect::<Vec<(&String, &UrlEntry)>>();

// Sort the entries so that directories shows first and then files
matched_entries.sort_by_cached_key(|(_, url_entry)| url_entry.fs_path.is_file());
// Sort the entries so that the directories shows up first and then files.
entries.sort_by_key(|(_, url_entry)| url_entry.fs_path.is_file());

let entries_hyperlinks = matched_entries
let links = entries
.iter()
.map(|(mapped_url, url_entry)| {
let icon = if url_entry.fs_path.is_dir() {
.fold(String::new(), |mut output, (url, entry)| {
let icon = if entry.fs_path.is_dir() {
FOLDER_SVG_ICON
} else {
FILE_SVG_ICON
};

let basename = url_entry
let basename = entry
.fs_path
.file_name()
.unwrap_or(url_entry.fs_path.as_ref())
.unwrap_or(entry.fs_path.as_ref())
.to_string_lossy();

format!(r#"<li><a href="{mapped_url}">{icon} {basename}</a></li>"#)
})
.collect::<String>();

let mut content = format!("<h1>Directory Listing for {url}</h1><ul>");
content.push_str(&entries_hyperlinks);
content.push_str("</ul>");
// NOTE: Unwrapping is completely safe here.
write!(output, r#"<li><a href="{url}">{icon} {basename}</a></li>"#).unwrap();
output
});
let content = format!("<h1>Directory Listing for {url}</h1>\n<ul>{links}</ul>");

PAGE_TEMPLATE
.replace("{title}", "Directory Listing")
Expand Down

0 comments on commit 2f82290

Please sign in to comment.