use crate::rubase::rudto::filedto::FileDto;
use crate::rulog::ru_log;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::{env, fs, io};
use walkdir::WalkDir;
pub fn if_windows() -> bool {
cfg!(target_os = "windows")
}
pub fn if_linux() -> bool {
cfg!(target_os = "linux")
}
pub fn if_macos() -> bool {
cfg!(target_os = "macos")
}
pub fn find_dir(basedir: &str, dir: &str) -> Vec<std::path::PathBuf> {
WalkDir::new(basedir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
e.file_type().is_dir()
&& e.path()
.file_name()
.unwrap()
.to_string_lossy()
.ends_with(dir)
})
.map(|e| e.path().to_path_buf())
.collect()
}
#[allow(dead_code)]
pub fn find_dirs(basedir: &str, dir: &str) -> Vec<std::path::PathBuf> {
println!("find_dirs: {}", basedir);
let dirs = WalkDir::new(basedir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_dir())
.map(|e| e.path().to_path_buf())
.collect();
dirs
}
pub fn list_all_files(dir: &str) -> Vec<std::path::PathBuf> {
WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_type().is_file())
.map(|e| e.path().to_path_buf())
.collect()
}
pub fn list_rs_files(dir: &str) -> Vec<String> {
let allfiles = list_all_files(dir);
let mut files = Vec::new();
for file in allfiles {
ru_log::info(file.to_str().unwrap());
if file.extension().and_then(|s| s.to_str()) == Some("rs") {
files.push(file.display().to_string());
}
}
files
}
pub fn list_all_files_suffix(dir: &str, suffix: &str) -> Vec<String> {
let allfiles = list_all_files(dir);
let mut files = Vec::new();
for file in allfiles {
ru_log::info(file.to_str().unwrap());
if file.extension().and_then(|s| s.to_str()) == Some(suffix) {
files.push(file.display().to_string());
}
}
files
}
pub fn is_rs(f: std::path::PathBuf) -> bool {
return f.extension().and_then(|s| s.to_str()) == Some("rs");
}
pub fn check_file_exist(pathf: &String) -> bool {
Path::new(&pathf).exists() }
pub fn read_file(file_name: &str) -> Result<String, io::Error> {
let file = File::open(file_name); let mut contents = String::new();
let ret = file?.read_to_string(&mut contents); if ret.is_err() {
return Err(ret.unwrap_err());
}
Ok(contents)
}
pub fn write_file(file_name: &str, content: &str) -> Result<String, io::Error> {
let file = File::create(file_name); let contents = String::new();
let ret = file?.write(content.as_bytes()); if ret.is_err() {
return Err(ret.unwrap_err());
}
Ok(contents)
}
pub fn list_files(dto: &FileDto) -> Vec<String> {
let allfiles = list_all_files(dto.home_dir.as_str());
let mut files = Vec::new();
for file in allfiles {
ru_log::info(file.to_str().unwrap());
if file.extension().and_then(|s| s.to_str()) == Some(dto.suffix.as_str()) {
let fs = file_size_m(&file);
if fs > dto.size {
files.push(file.display().to_string());
}
}
}
files
}
pub fn file_size_m(file: &std::path::PathBuf) -> u64 {
let metadata = fs::metadata(file.as_path());
let size = metadata.unwrap().len(); size / 1024 / 1024
}
#[allow(dead_code)]
fn file_size_g(file: &std::path::PathBuf) -> u64 {
let metadata = fs::metadata(file.as_path());
let size = metadata.unwrap().len(); size / 1024 / 1024 / 1024
}
pub fn current_dir() -> Result<PathBuf, io::Error> {
env::current_dir()
}
pub fn list_dir(dto: &FileDto) -> Vec<String> {
let allfiles = list_all_files(dto.home_dir.as_str());
let mut files = Vec::new();
for file in allfiles {
ru_log::info(file.to_str().unwrap());
if file.extension().and_then(|s| s.to_str()) == Some(dto.suffix.as_str()) {
let fs = crate::rubase::rutils::file_utils::file_size_m(&file);
if fs > dto.size {
files.push(file.display().to_string());
}
}
}
files
}