use crate::errors::*;
#[cfg(engine)]
use tokio::{
fs::{create_dir_all, File},
io::{AsyncReadExt, AsyncWriteExt},
};
#[async_trait::async_trait]
pub trait MutableStore: std::fmt::Debug + Clone + Send + Sync {
async fn read(&self, name: &str) -> Result<String, StoreError>;
async fn write(&self, name: &str, content: &str) -> Result<(), StoreError>;
}
#[derive(Clone, Debug)]
pub struct FsMutableStore {
#[cfg(engine)]
root_path: String,
}
#[cfg(engine)]
impl FsMutableStore {
#[cfg(engine)]
pub fn new(root_path: String) -> Self {
Self { root_path }
}
}
#[async_trait::async_trait]
impl MutableStore for FsMutableStore {
#[cfg(engine)]
async fn read(&self, name: &str) -> Result<String, StoreError> {
let asset_path = format!("{}/{}", self.root_path, name);
let file_res = File::open(&asset_path).await;
let mut file = match file_res {
Ok(file) => file,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
return Err(StoreError::NotFound { name: asset_path })
}
Err(err) => {
return Err(StoreError::ReadFailed {
name: asset_path,
source: err.into(),
})
}
};
let metadata = file.metadata().await;
match metadata {
Ok(_) => {
let mut contents = String::new();
file.read_to_string(&mut contents)
.await
.map_err(|err| StoreError::ReadFailed {
name: asset_path,
source: err.into(),
})?;
Ok(contents)
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
Err(StoreError::NotFound { name: asset_path })
}
Err(err) => Err(StoreError::ReadFailed {
name: asset_path,
source: err.into(),
}),
}
}
#[cfg(engine)]
async fn write(&self, name: &str, content: &str) -> Result<(), StoreError> {
let asset_path = format!("{}/{}", self.root_path, name);
let mut dir_tree: Vec<&str> = asset_path.split('/').collect();
dir_tree.pop();
create_dir_all(dir_tree.join("/"))
.await
.map_err(|err| StoreError::WriteFailed {
name: asset_path.clone(),
source: err.into(),
})?;
let mut file = File::create(&asset_path)
.await
.map_err(|err| StoreError::WriteFailed {
name: asset_path.clone(),
source: err.into(),
})?;
file.write_all(content.as_bytes())
.await
.map_err(|err| StoreError::WriteFailed {
name: asset_path.clone(),
source: err.into(),
})?;
file.sync_all()
.await
.map_err(|err| StoreError::WriteFailed {
name: asset_path,
source: err.into(),
})?;
Ok(())
}
#[cfg(client)]
async fn read(&self, _name: &str) -> Result<String, StoreError> {
Ok(String::new())
}
#[cfg(client)]
async fn write(&self, _name: &str, _content: &str) -> Result<(), StoreError> {
Ok(())
}
}