use std::fs;
use std::io::{self, Write, Read};
#[warn(dead_code)]
pub trait FileSystem {
fn create_file(&self, file: &str, content: &[u8]) -> io::Result<()>;
fn create_dir(&self, path: &str) -> io::Result<()>;
fn write_file(&self, file: &str, content: &[u8]) -> io::Result<()>;
fn read_file(&self, file: &str) -> io::Result<String>;
fn delete_file(&self, file: &str) -> io::Result<()>;
fn delete_dir(&self, path: &str) -> io::Result<()>;
}
pub struct LocalFileSystem;
impl FileSystem for LocalFileSystem {
fn create_file(&self, file: &str, content: &[u8]) -> io::Result<()> {
let mut f = fs::File::create(file)?;
f.write_all(content)
}
fn create_dir(&self, path: &str) -> io::Result<()> {
fs::create_dir_all(path)
}
fn write_file(&self, file: &str, content: &[u8]) -> io::Result<()> {
let mut f = fs::OpenOptions::new().write(true).append(true).open(file)?;
f.write_all(content)
}
fn read_file(&self, file: &str) -> io::Result<String> {
let mut f = fs::File::open(file)?;
let mut content = String::new();
f.read_to_string(&mut content)?;
Ok(content)
}
fn delete_file(&self, file: &str) -> io::Result<()> {
fs::remove_file(file)
}
fn delete_dir(&self, path: &str) -> io::Result<()> {
fs::remove_dir_all(path)
}
}