1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#![cfg(not(feature = "web"))] use crate::storage::{StorageEngine, StorageError, StorageResult}; use std::{ fs::{read, write}, path::{Path, PathBuf}, }; #[derive(Default, Clone)] pub struct FsStorageEngine { pub root: PathBuf, } impl FsStorageEngine { pub fn new<P>(path: P) -> Self where P: AsRef<Path>, { Self { root: path.as_ref().to_path_buf(), } } } impl StorageEngine for FsStorageEngine { fn load(&mut self, path: &str) -> StorageResult<Vec<u8>> { let path = self.root.join(path); match read(&path) { Ok(data) => Ok(data), Err(error) => Err(StorageError::CouldNotLoadData(error.to_string())), } } fn store(&mut self, path: &str, data: &[u8]) -> StorageResult<()> { let path = self.root.join(path); match write(&path, data) { Ok(_) => Ok(()), Err(error) => Err(StorageError::CouldNotStoreData(error.to_string())), } } }