diff --git a/Cargo.toml b/Cargo.toml index ce115aa..0338048 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.1" +version = "0.6.2" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/src/util_file.rs b/src/util_file.rs index 75e93d4..6a1cad6 100644 --- a/src/util_file.rs +++ b/src/util_file.rs @@ -77,10 +77,30 @@ impl Iterator for JoinFilesReader { } } -pub fn find_parents_exists_dir(dir: &str) -> Option { +pub fn find_parents_exists_file(file: &str) -> Option { + let mut loop_count = 0_usize; match PathBuf::from(".").canonicalize() { Err(_) => None, Ok(mut path) => loop { + loop_count += 1; + if loop_count > 100 { panic!("Loop count more than 100!"); } + if path.join(file).is_file() { + return Some(path); + } + if !path.pop() { + return None; + } + } + } +} + +pub fn find_parents_exists_dir(dir: &str) -> Option { + let mut loop_count = 0_usize; + match PathBuf::from(".").canonicalize() { + Err(_) => None, + Ok(mut path) => loop { + loop_count += 1; + if loop_count > 100 { panic!("Loop count more than 100!"); } if path.join(dir).is_dir() { return Some(path); }