1
0
mirror of https://github.com/jht5945/rust_util.git synced 2026-01-12 23:30:05 +08:00

Compare commits

...

8 Commits

6 changed files with 75 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "rust_util"
version = "0.3.0"
version = "0.6.3"
authors = ["Hatter Jiang <jht5945@gmail.com>"]
edition = "2018"
description = "Hatter's Rust Util"

View File

@@ -2,7 +2,7 @@
extern crate lazy_static;
extern crate term;
use std::io::{ Error, ErrorKind, };
use std::io::{ Error, ErrorKind };
pub mod util_io;
pub mod util_os;
@@ -15,11 +15,23 @@ pub mod util_file;
pub mod util_time;
/// iff!(condition, result_when_true, result_when_false)
#[macro_export]
macro_rules! iff {
($c:expr, $t:expr, $f:expr) => {
if $c { $t } else { $f }
};
#[macro_export] macro_rules! iff {
($c:expr, $t:expr, $f:expr) => ( if $c { $t } else { $f } )
}
#[macro_export] macro_rules! information {
($($arg:tt)+) => ( rust_util::util_msg::print_info(&format!($($arg)+)); )
}
#[macro_export] macro_rules! success {
($($arg:tt)+) => ( rust_util::util_msg::print_ok(&format!($($arg)+)); )
}
#[macro_export] macro_rules! warning {
($($arg:tt)+) => ( rust_util::util_msg::print_warn(&format!($($arg)+)); )
}
#[macro_export] macro_rules! failure {
($($arg:tt)+) => ( rust_util::util_msg::print_error(&format!($($arg)+)); )
}
#[macro_export] macro_rules! debugging {
($($arg:tt)+) => ( rust_util::util_msg::print_debug(&format!($($arg)+)); )
}
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;

View File

@@ -1,6 +1,5 @@
use std::{
io::{self, Error, ErrorKind},
io::{ self, Error, ErrorKind },
process::Command,
};

View File

@@ -1,15 +1,16 @@
use std::{
env,
fs::{self, File},
io::{Lines, BufReader},
path::{Path, PathBuf},
fs::{ self, File },
io::{ Lines, BufReader },
path::{ Path, PathBuf },
};
use super::{
use crate::{
iff,
util_os,
util_io,
util_msg,
new_box_ioerror,
XResult,
};
@@ -77,6 +78,46 @@ impl Iterator for JoinFilesReader {
}
}
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 > 1000 {
util_msg::print_error("Loop count more than 1000!");
return None;
}
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 > 1000 {
util_msg::print_error("Loop count more than 1000!");
return None;
}
if path.join(dir).is_dir() {
return Some(path);
}
if !path.pop() {
return None;
}
}
}
}
pub fn locate_file(files: &[String]) -> Option<PathBuf> {
for f in files {
match PathBuf::from(&resolve_file_path(f)) {
@@ -129,8 +170,8 @@ pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path,
func_walk_error: &FError,
func_process_file: &FProcess,
func_filter_dir: &FFilter) -> XResult<()>
where FError: Fn(&Path, Box<dyn std::error::Error>) -> (),
FProcess: Fn(&Path) -> (),
where FError: Fn(&Path, Box<dyn std::error::Error>),
FProcess: Fn(&Path),
FFilter: Fn(&Path) -> bool {
walk_dir_with_depth_check(&mut 0u32, dir, func_walk_error, func_process_file, func_filter_dir)
}
@@ -139,8 +180,8 @@ fn walk_dir_with_depth_check<FError, FProcess, FFilter>(depth: &mut u32, dir: &P
func_walk_error: &FError,
func_process_file: &FProcess,
func_filter_dir: &FFilter) -> XResult<()>
where FError: Fn(&Path, Box<dyn std::error::Error>) -> (),
FProcess: Fn(&Path) -> (),
where FError: Fn(&Path, Box<dyn std::error::Error>),
FProcess: Fn(&Path),
FFilter: Fn(&Path) -> bool {
if *depth > 100u32 {
return Err(new_box_ioerror(&format!("Depth exceed, depth: {}, path: {:?}", *depth, dir)));

View File

@@ -1,11 +1,7 @@
use std::{
fs::File,
io::{self,
ErrorKind,
prelude::*,
},
time::{SystemTime, Duration},
io::{ self, ErrorKind, prelude::* },
time::{ SystemTime, Duration },
};
use super::{ XResult, new_box_ioerror, };
@@ -66,12 +62,9 @@ pub fn print_status_last_line(head: &str, total: i64, written: i64, cost: Durati
pub fn copy_io_with_head<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, total: i64, head: &str) -> io::Result<u64>
where R: io::Read, W: io::Write {
//let written_cell = RefCell::new(0u64);
let start = SystemTime::now();
let written = copy_io_callback(reader, writer, total, &|total, written, _len| {
//written_cell.replace_with(|&mut w| w + len as u64);
//let written = *written_cell.borrow();
let cost = SystemTime::now().duration_since(start.clone()).unwrap();
let cost = SystemTime::now().duration_since(start).unwrap();
print_status_last_line(head, total, written as i64, cost);
});
println!();
@@ -81,7 +74,7 @@ pub fn copy_io_with_head<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W, t
pub fn copy_io_callback<R: ?Sized, W: ?Sized, FCallback>(reader: &mut R, writer: &mut W, total: i64, callback: &FCallback) -> io::Result<u64>
where R: io::Read,
W: io::Write,
FCallback: Fn(i64, u64, usize) -> () {
FCallback: Fn(i64, u64, usize) {
let mut written = 0u64;
let mut buf: [u8; DEFAULT_BUF_SIZE] = [0u8; DEFAULT_BUF_SIZE];
loop {

View File

@@ -1,6 +1,6 @@
use std::{
io::{self, Write},
sync::{Arc, Mutex},
io::{ self, Write },
sync::{ Arc, Mutex },
};
lazy_static! {