diff options
author | 2025-04-17 11:03:51 +0100 | |
---|---|---|
committer | 2025-04-17 11:03:51 +0100 | |
commit | b9d75f38743113c054be3d97af36bdd2a7dd0d69 (patch) | |
tree | 537623664010d26d5f2574e2d51d03f8c25e08ac /filamento/src/files.rs | |
parent | cf51dcf052af89f8742d887bde2c93d735309bdd (diff) | |
download | luz-b9d75f38743113c054be3d97af36bdd2a7dd0d69.tar.gz luz-b9d75f38743113c054be3d97af36bdd2a7dd0d69.tar.bz2 luz-b9d75f38743113c054be3d97af36bdd2a7dd0d69.zip |
feat(filamento): compiles on wasm
Diffstat (limited to '')
-rw-r--r-- | filamento/src/files.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/filamento/src/files.rs b/filamento/src/files.rs index 3acc871..644f883 100644 --- a/filamento/src/files.rs +++ b/filamento/src/files.rs @@ -1,10 +1,13 @@ use std::{ + collections::HashMap, + convert::Infallible, error::Error, path::{Path, PathBuf}, sync::Arc, }; use tokio::io; +use tokio::sync::Mutex; pub trait FileStore { type Err: Clone + Send + Error; @@ -25,10 +28,47 @@ pub trait FileStore { } #[derive(Clone, Debug)] +pub struct FilesMem { + files: Arc<Mutex<HashMap<String, Vec<u8>>>>, +} + +impl FilesMem { + pub fn new() -> Self { + Self { + files: Arc::new(Mutex::new(HashMap::new())), + } + } +} + +impl FileStore for FilesMem { + type Err = Infallible; + + async fn is_stored(&self, name: &str) -> Result<bool, Self::Err> { + Ok(self.files.lock().await.contains_key(name)) + } + + async fn store(&self, name: &str, data: &[u8]) -> Result<(), Self::Err> { + self.files + .lock() + .await + .insert(name.to_string(), data.to_owned()); + + Ok(()) + } + + async fn delete(&self, name: &str) -> Result<(), Self::Err> { + self.files.lock().await.remove(name); + Ok(()) + } +} + +#[cfg(not(target_arch = "wasm32"))] +#[derive(Clone, Debug)] pub struct Files { root: PathBuf, } +#[cfg(not(target_arch = "wasm32"))] impl Files { pub fn new(root: impl AsRef<Path>) -> Self { let root = root.as_ref(); @@ -41,6 +81,7 @@ impl Files { } } +#[cfg(not(target_arch = "wasm32"))] impl FileStore for Files { type Err = Arc<io::Error>; |