Skip to content

Commit b49fe50

Browse files
committed
Return bool instead of CargoResult for filter
Since `StripPrefixError` returned from [`Path::strip_prefix`] is just an error with "prefix not found. We can simply return false for it. Also use `Match::is_ignore` instead of manually matching. [`Path::strip_prefix`]: https://doc.rust-lang.org/1.57.0/std/path/struct.Path.html#method.strip_prefix
1 parent 6fa0f01 commit b49fe50

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/cargo/sources/path.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use anyhow::Context as _;
1010
use cargo_util::paths;
1111
use filetime::FileTime;
1212
use ignore::gitignore::GitignoreBuilder;
13-
use ignore::Match;
1413
use log::{trace, warn};
1514
use walkdir::WalkDir;
1615

@@ -131,39 +130,36 @@ impl<'cfg> PathSource<'cfg> {
131130
}
132131
let ignore_include = include_builder.build()?;
133132

134-
let ignore_should_package = |relative_path: &Path, is_dir: bool| -> CargoResult<bool> {
133+
let ignore_should_package = |relative_path: &Path, is_dir: bool| {
135134
// "Include" and "exclude" options are mutually exclusive.
136135
if no_include_option {
137-
match ignore_exclude.matched_path_or_any_parents(relative_path, is_dir) {
138-
Match::None => Ok(true),
139-
Match::Ignore(_) => Ok(false),
140-
Match::Whitelist(_) => Ok(true),
141-
}
136+
!ignore_exclude
137+
.matched_path_or_any_parents(relative_path, is_dir)
138+
.is_ignore()
142139
} else {
143140
if is_dir {
144141
// Generally, include directives don't list every
145142
// directory (nor should they!). Just skip all directory
146143
// checks, and only check files.
147-
return Ok(true);
144+
return true;
148145
}
149-
match ignore_include
146+
ignore_include
150147
.matched_path_or_any_parents(relative_path, /* is_dir */ false)
151-
{
152-
Match::None => Ok(false),
153-
Match::Ignore(_) => Ok(true),
154-
Match::Whitelist(_) => Ok(false),
155-
}
148+
.is_ignore()
156149
}
157150
};
158151

159-
let mut filter = |path: &Path, is_dir: bool| -> CargoResult<bool> {
160-
let relative_path = path.strip_prefix(root)?;
152+
let mut filter = |path: &Path, is_dir: bool| {
153+
let relative_path = match path.strip_prefix(root) {
154+
Ok(p) => p,
155+
Err(_) => return false,
156+
};
161157

162158
let rel = relative_path.as_os_str();
163159
if rel == "Cargo.lock" {
164-
return Ok(pkg.include_lockfile());
160+
return pkg.include_lockfile();
165161
} else if rel == "Cargo.toml" {
166-
return Ok(true);
162+
return true;
167163
}
168164

169165
ignore_should_package(relative_path, is_dir)
@@ -225,7 +221,7 @@ impl<'cfg> PathSource<'cfg> {
225221
&self,
226222
pkg: &Package,
227223
repo: &git2::Repository,
228-
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
224+
filter: &mut dyn FnMut(&Path, bool) -> bool,
229225
) -> CargoResult<Vec<PathBuf>> {
230226
warn!("list_files_git {}", pkg.package_id());
231227
let index = repo.index()?;
@@ -347,7 +343,7 @@ impl<'cfg> PathSource<'cfg> {
347343
PathSource::walk(&file_path, &mut ret, false, filter)?;
348344
}
349345
}
350-
} else if (*filter)(&file_path, is_dir)? {
346+
} else if filter(&file_path, is_dir) {
351347
assert!(!is_dir);
352348
// We found a file!
353349
warn!(" found {}", file_path.display());
@@ -379,7 +375,7 @@ impl<'cfg> PathSource<'cfg> {
379375
fn list_files_walk(
380376
&self,
381377
pkg: &Package,
382-
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
378+
filter: &mut dyn FnMut(&Path, bool) -> bool,
383379
) -> CargoResult<Vec<PathBuf>> {
384380
let mut ret = Vec::new();
385381
PathSource::walk(pkg.root(), &mut ret, true, filter)?;
@@ -390,7 +386,7 @@ impl<'cfg> PathSource<'cfg> {
390386
path: &Path,
391387
ret: &mut Vec<PathBuf>,
392388
is_root: bool,
393-
filter: &mut dyn FnMut(&Path, bool) -> CargoResult<bool>,
389+
filter: &mut dyn FnMut(&Path, bool) -> bool,
394390
) -> CargoResult<()> {
395391
let walkdir = WalkDir::new(path)
396392
.follow_links(true)
@@ -400,7 +396,7 @@ impl<'cfg> PathSource<'cfg> {
400396
let at_root = is_root && entry.depth() == 0;
401397
let is_dir = entry.file_type().is_dir();
402398

403-
if !at_root && !filter(path, is_dir).unwrap() {
399+
if !at_root && !filter(path, is_dir) {
404400
return false;
405401
}
406402

0 commit comments

Comments
 (0)