Closed ololduck closed 3 years ago
Thanks for asking a question. I definitely encourage that. However, when asking questions, please do your best to at least provide the following things:
When you don't provide these things, you make it more difficult for people to answer your questions. For example, in this case:
filter_entry
and it's not behaving as you would expect, but I don't know what you're expectations are. I could guess, but maybe I'd be wrong and we'd wind up going in circles.Please consider doing these things next time you ask a question. Not doing these things puts an additional burden on the folks you're asking to help you.
In the interest of helping you, from what I can see, my guess is that your regexes don't match base_dir
and thus filter_entry
excludes it and your program won't descend into that directory.
Thank you for your input, i edited the original post to (hopefully) make it more understandable.
From your last sentence, I think i understand what you mean. since my _match
function returns false on dirs, filter_entry
won't go down any directory. It makes sense!
I guess i'll make the dir evaluation in a second pass, once all the files have been collected.
Hello!
I ditched filter_entry
, and used plain old Iter.filter
, and everything works as expected. I'm closing this issue, and appologize for my incomplete comprehension of filter_entry
...
Here's the "final" get_files
:
pub fn get_files<T: AsRef<str> + Display>(
base_dir: &str,
regexps: &[T],
) -> anyhow::Result<Vec<String>> {
let final_list = WalkDir::new(base_dir)
.into_iter()
.filter_map(Result::ok)
.filter(|e| {
debug!("trying file {}", e.path().display());
_matches(e.path(), regexps)
})
.map(|e| {
debug!("Adding file {:?}", e);
e.path().display().to_string()
})
.collect();
debug!("final list: {:?}", final_list);
Ok(final_list)
}
Thanks, paul
Right. From the docs:
Yields only entries which satisfy the given predicate and skips descending into directories that do not satisfy the given predicate.
So if you're returning false for a directory, then walkdir won't descend into it.
If you only care about files and only want to apply a filter to files, then using filter
is fine. That is, there is no advantage to using filter_entry
.
Thank you for following up and sharing your solution and also for filling in the original issue! I appreciate it.
Hello,
Sorry to bother you with this, but I'm having a weird behavior with filter_entry. It may be due to the incompleteness of my rust understanding.
I'm trying to filter a file tree to return all regular files (as in: not directories) matching at least one element in a set of regular expressions.
So if i had a simple rust project, it would return
src/main.rs
andcargo.toml
, if the regexps were.*\.rs
&.*\.toml
.Here's some code supposed to produced this behavior:
src/main.rs:
cargo.toml:
Here's it's output:
I'm still in the design phase, so the code is not properly optimized, I hope to change it in the future.
If the previous code worked as intended, i would have at least:
trying file {file}
File {file} didn't match
ORFound matching file {file}
Adding file {file}
if matched in previous step.As we can see in the output, it only considers the first directory (the
base_dir
parameter) and stops its evaluation.Do you have any pointer that would help me understand what i did wrong?
paul