mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 07:30:05 +08:00
fix: add loop count
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rust_util"
|
||||
version = "0.6.1"
|
||||
version = "0.6.2"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Hatter's Rust Util"
|
||||
|
||||
@@ -77,10 +77,30 @@ impl Iterator for JoinFilesReader {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_parents_exists_dir(dir: &str) -> Option<PathBuf> {
|
||||
pub fn find_parents_exists_file(file: &str) -> Option<PathBuf> {
|
||||
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<PathBuf> {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user