Skip to content

Commit

Permalink
refactor(common): Use variables to defines link and text
Browse files Browse the repository at this point in the history
Signed-off-by: PrajwalCH <[email protected]>
  • Loading branch information
prajwalch committed Dec 29, 2023
1 parent d10c3f3 commit 2e717b7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ fn create_entry_hyperlinks(root: &Path, path: &Path) -> io::Result<String> {

entries.iter().try_fold(String::new(), |mut output, entry| {
let path = entry.path();
// Convert path to a url path.
let link = fs_path_to_url(root, &path);
// Use icon to represent whether path is a dir or file.
let icon = if path.is_dir() { DIR_ICON } else { FILE_ICON };
// We can directly print it but it adds `"` as prefix and suffix.
let text = entry.file_name();
// Convert to a lossy string so that we can print.
let text = text.to_string_lossy();

// NOTE: Unwrapping is completely safe here.
writeln!(
output,
"<li><a href=\"{}\">{icon} {}</a></li>",
fs_path_to_url(root, &path),
entry.file_name().to_string_lossy()
)
.unwrap();
writeln!(output, "<li><a href=\"{link}\">{icon} {text}</a></li>",).unwrap();
Ok(output)
})
}
Expand Down

0 comments on commit 2e717b7

Please sign in to comment.