use std::ffi::OsString;
use std::path::Path;
pub fn cmd_path() -> String {
std::env::current_dir()
.unwrap()
.into_os_string()
.into_string()
.unwrap()
}
pub fn path_join(basepath: &str, joinpath: &str) -> String {
Path::new(basepath)
.join(joinpath)
.into_os_string()
.into_string()
.unwrap()
}
pub fn cmd_path_resolve(path: &str) -> String {
std::env::current_dir()
.unwrap()
.join(path)
.into_os_string()
.into_string()
.unwrap()
}
pub fn path_resolve(path: &str) -> String {
let work_cwd = env!("CARGO_MANIFEST_DIR");
let os_work_cwd = OsString::from(work_cwd);
Path::new(&os_work_cwd)
.join(path)
.into_os_string()
.into_string()
.unwrap()
}
pub fn readfile(path: &str) -> Result<String, String> {
let filepath = Path::new(path);
if filepath.exists() {
if filepath.is_dir() {
return Err(format!(
"file is not file maybe is dir ?! filepath is{}",
path
));
}
match std::fs::read_to_string(filepath) {
Ok(content) => Ok(content),
Err(ex) => Err(ex.to_string()),
}
} else {
Err(format!("file is not exists filepath is {}", path))
}
}
pub fn get_dir(path_value: &str) -> Result<String, String> {
let path = Path::new(path_value);
if path.is_file() {
Ok(path.parent().unwrap().to_str().unwrap().to_string())
} else if path.is_dir() {
Ok(path_value.to_string())
} else {
Err(format!(
"path type is file or dir please check {}",
path_value
))
}
}