1
0
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:
2020-08-02 14:43:13 +08:00
parent a298b6ffcb
commit 31a2910393
2 changed files with 22 additions and 2 deletions

View File

@@ -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"

View File

@@ -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);
}