Skip to content

Commit

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

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

match fs::metadata(&candidate) {
Ok(metadata) => {
if metadata.is_file() {
return Ok(candidate);
}
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)),
}
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",
)))
if let Some(parent) = directory.parent() {
directory = parent;
} else {
return Err(Error::Io(io::Error::new(
io::ErrorKind::NotFound,
"path not found",
)));
}
}
}

0 comments on commit e7f398b

Please sign in to comment.