simpel01
simpel01

Reputation: 1782

Iterate through leaf directories

I want to print all leaf directories of a tree structure starting from a given path. My definition for "leaf directory" is any directory with no subdirectories.

I have this code:

for e in WalkDir::new(path)
   .into_iter()
   .filter_map(|e| e.ok()) {
   if e.metadata().unwrap().is_dir() {
       let mut has_subdir = false;
       for entry in fs::read_dir(e.path()).unwrap() {
           let path = entry.unwrap().path();
           if path.is_dir() {
               has_subdir = true;
               break;
           }
       }
       if (!has_subdir) {
           println!("{}", e.path().display());
       }
   }
}

It does the work, but I do not think it is idiomatic. I would like to have the result as a list comprehension.

Upvotes: 0

Views: 461

Answers (1)

simpel01
simpel01

Reputation: 1782

I ended up resolving this as follows:

fn is_leaf(entry: DirEntry) -> Result<DirEntry, Error> {
    if !entry.metadata()?.is_dir() {
        return Err(Error::new(ErrorKind::Other, "not leaf dir"))
    }

    for e in fs::read_dir(entry.path())? {
        if e?.path().is_dir() {
            return Err(Error::new(ErrorKind::Other, "not leaf dir"))
        }
    }

    Ok(entry)
}

fn leaf_dirs(path: &Path) -> impl Iterator<Item = PathBuf> {
    WalkDir::new(path)
        .follow_links(true)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter_map(|e| is_leaf(e).ok())
            .map(|e| e.into_path().to_path_buf())
}

Not sure if it is idiomatic Rust, but it does what I want.

Upvotes: 1

Related Questions