BurntSushi / walkdir

Rust library for walking directories recursively.
The Unlicense
1.3k stars 109 forks source link

[feature request] files helper function #195

Closed dev-ardi closed 9 months ago

dev-ardi commented 9 months ago

Probably one of the most common use cases for walkdir is to recursively get a list of files given a folder. Why not add something like

impl Walkdir {
  fn files(self) -> impl Iterator<Item = walkdir::DirEntry> {
    self
        .into_iter()
        .filter_map(Result::ok)
        .filter(|x| x.file_type().is_file())
        // .map(|x| x.path()) Possibly as paths?
}

So that a user can just do let files = walkdir::WalkDir::new("my/folder").files();?

BurntSushi commented 9 months ago

I'm not sure this is really something I want to add.

First, I'm skeptical that it is one of the most common use cases. I've used walkdir a fair bit myself, and I don't think I've ever written that code.

Second, it silently drops errors. (This is probably one of the reasons why I've never written something like what you've provided.)

Third, I don't use impl Trait in public APIs unless there is no other choice. So this would need to return a new named iterator type. Not a deal breaker, but it's more of an API addition than just a simple method.

You could address the second concern by returning a Item = Result<walkdir::DirEntry, walkdir::Error>, but then the helper routine just turns into a pretty trivial filter function. I'm generally happy for callers to need to write that themselves.

I think I'm just going to say no to do this. Thank you for the suggestion though.

dev-ardi commented 9 months ago

That's fair, it's just a pattern that I've used a bunch of times for quick scripts, thanks for answering.

Third, I don't use impl Trait in public APIs unless there is no other choice.

Why is this?