use crate::{error::Error, Result};
use std::path::Path;
use tokio::fs;
#[derive(Debug)]
pub struct FileSystem;
impl FileSystem {
pub async fn new() -> Result<Self> {
Ok(Self)
}
pub async fn exists(&self, path: &Path) -> Result<bool> {
Ok(fs::metadata(path).await.is_ok())
}
pub async fn create_dir(&self, path: &Path) -> Result<()> {
fs::create_dir(path).await.map_err(Error::from)
}
pub async fn create_dir_all(&self, path: &Path) -> Result<()> {
fs::create_dir_all(path).await.map_err(Error::from)
}
pub async fn read_dir(&self, path: &Path) -> Result<fs::ReadDir> {
fs::read_dir(path).await.map_err(Error::from)
}
pub async fn remove_file(&self, path: &Path) -> Result<()> {
fs::remove_file(path).await.map_err(Error::from)
}
pub async fn copy(&self, from: &Path, to: &Path) -> Result<()> {
fs::copy(from, to).await.map_err(Error::from)?;
Ok(())
}
pub async fn create_file(&self, path: &Path) -> Result<tokio::fs::File> {
tokio::fs::File::create(path).await.map_err(Error::from)
}
pub async fn open_file(&self, path: &Path) -> Result<tokio::fs::File> {
tokio::fs::File::open(path).await.map_err(Error::from)
}
pub async fn read_file(&self, path: &Path) -> Result<Vec<u8>> {
fs::read(path).await.map_err(Error::from)
}
pub async fn write_file(&self, path: &Path, contents: &[u8]) -> Result<()> {
fs::write(path, contents).await.map_err(Error::from)
}
pub async fn file_size(&self, path: &Path) -> Result<u64> {
let metadata = fs::metadata(path).await.map_err(Error::from)?;
Ok(metadata.len())
}
pub async fn metadata(&self, path: &Path) -> Result<std::fs::Metadata> {
fs::metadata(path).await.map_err(Error::from)
}
}