use std::fmt; use camino::{Utf8Path, Utf8PathBuf}; use confindent::Confindent; use scurvy::{Argument, Scurvy, arg, formula::Utf8PathFormula}; fn main() { let args = vec![ arg!(["conf", "c"] = "path"), arg!(["webroot", "r"] = "path"), ]; let scurvy = Scurvy::make(args); let webroot: Utf8PathBuf = match scurvy.parse("webroot", Utf8PathFormula::new()) { Some(path) => path, None => { let conf_path = scurvy.parse_req("conf", Utf8PathFormula::new()); let conf = Confindent::from_file(conf_path).unwrap(); conf.child_parse("Webroot").unwrap() } }; let rootdir = map_directory(webroot.clone(), &webroot); println!("{rootdir}") } fn map_directory(root: Utf8PathBuf, path: &Utf8Path) -> Directory { let name = path.file_name().unwrap(); let mut files = vec![]; let mut children = vec![]; let mut has_dirfile = false; for res_entry in path.read_dir_utf8().unwrap() { let entry = res_entry.unwrap(); let entry_path = entry.path().to_owned(); let entry_name = entry.file_name(); let entry_stem = entry_path.file_stem().unwrap(); let entry_meta = entry.metadata().unwrap(); if entry_meta.is_file() { if let Some("html") = entry_path.extension() { if name == entry_stem { has_dirfile = true; continue; } files.push(Page { name: entry_name.to_owned(), path: entry_path, }); } } else if entry_meta.is_dir() { let entry_dir = map_directory(root.clone(), &entry_path); if entry_dir.files.len() > 0 { children.push(entry_dir); } } } Directory { name: name.to_owned(), path: path.to_owned(), has_dirfile, files, children, root, } } struct Directory { name: String, path: Utf8PathBuf, has_dirfile: bool, files: Vec, children: Vec, root: Utf8PathBuf, } impl fmt::Display for Directory { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let root = &self.root; writeln!(f, "# {}", rel_or_abs(root, &self.path))?; for page in &self.files { writeln!(f, "{}", rel_or_abs(root, &page.path))?; } for subdir in &self.children { write!(f, "\n{subdir}")?; } Ok(()) } } fn rel_or_abs<'r, 'p>(root: &'r Utf8Path, path: &'p Utf8Path) -> &'p Utf8Path { path.strip_prefix(root).unwrap_or(path) } struct Page { name: String, path: Utf8PathBuf, }