1use termtree::Tree;
2
3use std::path::Path;
4use std::{env, fs, io};
5
6fn label<P: AsRef<Path>>(p: P) -> String {
7 p.as_ref().file_name().unwrap().to_str().unwrap().to_owned()
8}
9
10fn tree<P: AsRef<Path>>(p: P) -> io::Result<Tree<String>> {
11 let result = fs::read_dir(&p)?.filter_map(|e| e.ok()).fold(
12 Tree::new(label(p.as_ref().canonicalize()?)),
13 |mut root, entry| {
14 let dir = entry.metadata().unwrap();
15 if dir.is_dir() {
16 root.push(tree(entry.path()).unwrap());
17 } else {
18 root.push(Tree::new(label(entry.path())));
19 }
20 root
21 },
22 );
23 Ok(result)
24}
25
26fn main() {
27 let dir = env::args().nth(1).unwrap_or_else(|| String::from("."));
28 match tree(dir) {
29 Ok(tree) => println!("{}", tree),
30 Err(err) => println!("error: {}", err),
31 }
32}
33