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

Compare commits

..

2 Commits

Author SHA1 Message Date
7811e29921 update 2020-04-12 00:55:37 +08:00
b8da734669 add util_file::read_file_content 2020-04-12 00:39:16 +08:00
3 changed files with 21 additions and 25 deletions

View File

@@ -2,8 +2,5 @@
use std::env; use std::env;
pub fn is_env_on(var: &str) -> bool { pub fn is_env_on(var: &str) -> bool {
match env::var(var) { env::var(var).map(|v| v.to_lowercase()).map(|v| (v == "true" || v == "yes" || v == "1")).unwrap_or(false)
Err(_) => false,
Ok(v) => (v == "TRUE" || v == "true" || v =="YES" || v == "yes" || v == "1"),
}
} }

View File

@@ -6,17 +6,15 @@ use std::{
}; };
use super::{ use super::{
iff,
util_os, util_os,
util_io,
new_box_ioerror, new_box_ioerror,
XResult, XResult,
}; };
pub fn get_home_str() -> Option<String> { pub fn get_home_str() -> Option<String> {
if util_os::is_macos_or_linux() { iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
env::var("HOME").ok()
} else {
None
}
} }
pub fn get_home_path() -> Option<PathBuf> { pub fn get_home_path() -> Option<PathBuf> {
@@ -24,19 +22,22 @@ pub fn get_home_path() -> Option<PathBuf> {
} }
pub fn get_absolute_path(path: &str) -> Option<PathBuf> { pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
if path == "~" { match path {
return Some(PathBuf::from(get_home_str()?)); "~" => Some(PathBuf::from(get_home_str()?)),
} else if path.starts_with("~/") { path if path.starts_with("~/") => Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))),
return Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))); path => fs::canonicalize(path).ok(),
}
}
pub fn read_file_content(file: &str) -> XResult<String> {
match get_absolute_path(file) {
None => return Err(new_box_ioerror(&format!("File not found: {}", file))),
Some(p) => util_io::read_to_string(&mut fs::File::open(p)?),
} }
fs::canonicalize(path).ok()
} }
pub fn is_symlink(path: &Path) -> bool { pub fn is_symlink(path: &Path) -> bool {
match path.symlink_metadata() { path.symlink_metadata().map(|meta| meta.file_type().is_symlink()).unwrap_or(false)
Err(_) => false,
Ok(meta) => meta.file_type().is_symlink(),
}
} }
pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path, pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path,
@@ -60,19 +61,17 @@ fn walk_dir_with_depth_check<FError, FProcess, FFilter>(depth: &mut u32, dir: &P
return Err(new_box_ioerror(&format!("Depth exceed, depth: {}, path: {:?}", *depth, dir))); return Err(new_box_ioerror(&format!("Depth exceed, depth: {}, path: {:?}", *depth, dir)));
} }
let read_dir = match dir.read_dir() { let read_dir = match dir.read_dir() {
Err(err) => { Ok(rd) => rd, Err(err) => {
func_walk_error(&dir, Box::new(err)); func_walk_error(&dir, Box::new(err));
return Ok(()); return Ok(());
}, },
Ok(rd) => rd,
}; };
for dir_entry_item in read_dir { for dir_entry_item in read_dir {
let dir_entry = match dir_entry_item { let dir_entry = match dir_entry_item {
Err(err) => { Ok(item) => item, Err(err) => {
func_walk_error(&dir, Box::new(err)); func_walk_error(&dir, Box::new(err));
continue; // Ok? continue; // Ok?
}, },
Ok(item) => item,
}; };
let path_buf = dir_entry.path(); let path_buf = dir_entry.path();