1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use std::path::PathBuf;
use std::error::Error;

pub fn see_dir<'a>(dir: PathBuf, with_dirs: bool) -> Result<Vec<PathBuf>, &'a str> {
    let mut list: Vec<PathBuf> = Vec::new();
    for entry in match std::fs::read_dir(dir.clone()) {Ok(e) => e, Err(_s) => return Err("Could not read dir")} {
        let entry = match  entry {
            Ok(e) => e,
            Err(_e) => return Err("Could not convert to DirEntry"),
        };
        if entry.path().is_dir() {
            if with_dirs {
                list.push(entry.path().to_owned());
            }
            let mut sub: Vec<PathBuf> = Vec::new();
            sub = see_dir(entry.path(), with_dirs)?;
            list.extend(sub);
        } else {
            list.push(entry.path().to_owned());
        }
    }
    Ok(list)
}