use std::fs::File;
use std::io;
use std::io::{Read, Write};
use std::path::Path;
use walkdir::{DirEntry, WalkDir};
use crate::rulog::rulog;
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()
}
pub fn find_dirs(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( ) )
.map(|e| e.path().to_path_buf())
.collect()
}
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_all_files_rs(dir: &str) -> Vec<String> {
let allfiles=list_all_files(dir);
let mut files=Vec::new();
for file in allfiles {
rulog::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 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 mut file = File::open(file_name); let mut contents = String::new();
file?.read_to_string(&mut contents); Ok(contents)
}
pub fn write_file(file_name :&str, content: &str) -> Result<String, io::Error> {
let mut file = File::create(file_name); let mut contents = String::new();
file?.write( content.as_bytes()); Ok(contents)
}