Skip to content

Commit

Permalink
Revert "Removed recursion in find method (allan2#80)"
Browse files Browse the repository at this point in the history
This reverts commit e7f398b.
  • Loading branch information
sonro committed Jul 17, 2024
1 parent 6a5462a commit 3817478
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions dotenv/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,29 @@ impl<'a> Finder<'a> {
}

/// Searches for `filename` in `directory` and parent directories until found or root is reached.
pub fn find(mut directory: &Path, filename: &Path) -> Result<PathBuf> {
loop {
let candidate = directory.join(filename);
#[allow(clippy::option_if_let_else)]
pub fn find(directory: &Path, filename: &Path) -> Result<PathBuf> {
let candidate = directory.join(filename);

match fs::metadata(&candidate) {
Ok(metadata) if metadata.is_file() => return Ok(candidate),
Ok(_) => {}
Err(error) if matches!(error.kind(), io::ErrorKind::NotFound) => {}
Err(error) => return Err(Error::Io(error)),
match fs::metadata(&candidate) {
Ok(metadata) => {
if metadata.is_file() {
return Ok(candidate);
}
}

if let Some(parent) = directory.parent() {
directory = parent;
} else {
return Err(Error::Io(io::Error::new(
io::ErrorKind::NotFound,
"path not found",
)));
Err(error) => {
if error.kind() != io::ErrorKind::NotFound {
return Err(Error::Io(error));
}
}
}

if let Some(parent) = directory.parent() {
find(parent, filename)
} else {
Err(Error::Io(io::Error::new(
io::ErrorKind::NotFound,
"dotenv file not found in parent directory",
)))
}
}

0 comments on commit 3817478

Please sign in to comment.