use core::str;
use std::path::Path;
use std::io;
use std::fs;
pub fn read_file(file_path: &str) -> String {
let file_contents = std::fs::read(file_path);
if file_contents.is_err() {
return "".to_string();
}
str::from_utf8(&file_contents.unwrap()).unwrap().to_string()
}
pub fn write_file(file_path: &str, contents: &str) {
write_file_with_dirs(file_path, contents).expect(format!("Unable to write file: {}", file_path).as_str());
}
fn write_file_with_dirs<P: AsRef<Path>>(file_path: P, content: &str) -> io::Result<()> {
let file_path = file_path.as_ref();
if let Some(parent) = file_path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(file_path, content)?;
Ok(())
}