Skip to content

Commit

Permalink
fix/traverse: if recurse, collect flat files, otherwise traverse dirs
Browse files Browse the repository at this point in the history
Return to the initial traversal via `mod foo` decls and `Cargo.toml`
manifest parsing as soon as one is encountered.
  • Loading branch information
drahnr committed Jul 8, 2021
1 parent 6e7b60d commit 53f5c03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
2 changes: 2 additions & 0 deletions demo/member/stray.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Nobady references this.
struct Lost;
45 changes: 39 additions & 6 deletions src/traverse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,39 @@ pub(crate) fn extract(
let cargo_toml = to_manifest_dir(&path).unwrap().join("Cargo.toml");
if cargo_toml.is_file() {
Extraction::Manifest(cargo_toml)
} else if recurse {
// keep walking directories and feed the path back
// if recursing is wanted
// and if it doesn't contain a manifest file
match fs::read_dir(path) {
Err(err) => warn!("Listing directory contents {} failed", err),
Ok(entries) => {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
// let's try with that path again
flow.push_back(path);
}
}
}
}
continue;
} else {
// TODO should we just collect all .rs files here instead?

// we know it's a directory, and we limit the entries to 0 levels,
// will cause to yield all "^.*\.rs$" files in that dir
// which is what we want in this case
flow.extend(TraverseModulesIter::with_depth_limit(&path, 0)?);
match fs::read_dir(path) {
Err(err) => warn!("Listing directory contents {} failed", err),
Ok(entries) => {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
// let's try attempt with that .rs file
// if we end up here, recursion is off already
if path.is_file() {
flow.push_back(path);
}
}
}
}
}
continue;
}
} else {
Expand Down Expand Up @@ -753,4 +779,11 @@ mod tests {
"src/nested/justtwo.rs",
"src/nested/mod.rs"
]);

extract_test!(traverse_dir_wo_manifest, ["member"] + true => [
"member/true/lib.rs",
"member/true/README.md",
"member/procmacro/src/lib.rs",
"member/stray.rs",
]);
}

0 comments on commit 53f5c03

Please sign in to comment.