#![doc = include_str!("../examples/disk.rs")]
use std::path::Path;
use crate::{
contents::Contents,
drivers::Driver,
errors::{DriverError, DriverResult},
};
pub struct Store {
driver: Box<dyn Driver>,
}
impl Clone for Store {
fn clone(&self) -> Self {
Self {
driver: dyn_clone::clone_box(&*self.driver),
}
}
}
impl Store {
#[must_use]
pub fn new(driver: Box<dyn Driver>) -> Self {
Self { driver }
}
pub async fn file_exists(&self, path: &Path) -> DriverResult<bool> {
self.driver.file_exists(path).await
}
pub async fn write<C: AsRef<[u8]> + Send>(&self, path: &Path, content: C) -> DriverResult<()> {
self.driver.write(path, content.as_ref().to_vec()).await
}
pub async fn read<T: TryFrom<Contents>>(&self, path: &Path) -> DriverResult<T> {
Contents::from(self.driver.read(path).await?)
.try_into()
.map_or_else(|_| Err(DriverError::DecodeError), |content| Ok(content))
}
pub async fn delete(&self, path: &Path) -> DriverResult<()> {
self.driver.delete(path).await
}
pub async fn delete_directory(&self, path: &Path) -> DriverResult<()> {
self.driver.delete_directory(path).await
}
pub async fn last_modified(&self, path: &Path) -> DriverResult<std::time::SystemTime> {
self.driver.last_modified(path).await
}
}