use std::{error::Error, fs::File, io::prelude::*, path::Path, time::SystemTime};
use crate::types::ResultVoid;
pub fn read_file(file_path: &Path) -> Result<String, Box<dyn Error + Send + Sync>> {
let file = File::open(file_path);
match file {
Ok(mut file) => {
let mut contents = String::new();
match file.read_to_string(&mut contents) {
Ok(_) => Ok(contents),
Err(e) => Err(Box::new(e)),
}
}
Err(e) => Err(Box::new(e)),
}
}
pub fn get_last_modified(file_path: &Path) -> Result<SystemTime, Box<dyn Error + Send + Sync>> {
let metadata = std::fs::metadata(file_path);
match metadata {
Ok(metadata) => Ok(metadata.modified().unwrap()),
Err(e) => Err(Box::new(e)),
}
}
pub fn write_file(file_path: &Path, data: &str) -> ResultVoid {
let mut file = File::create(file_path)?;
file.write_all(data.as_bytes())?;
Ok(())
}
pub fn read_file_str(file_path_str: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
let file_path = Path::new(file_path_str);
read_file(file_path)
}
pub fn write_file_str(file_path_str: &str, data: &str) -> ResultVoid {
let file_path = Path::new(file_path_str);
write_file(file_path, data)
}
pub fn get_last_modified_str(file_path: &str) -> Result<SystemTime, Box<dyn Error + Send + Sync>> {
let file_path = Path::new(file_path);
get_last_modified(file_path)
}