diff options
Diffstat (limited to 'filamento/src/files.rs')
-rw-r--r-- | filamento/src/files.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/filamento/src/files.rs b/filamento/src/files.rs index 644f883..dcc9cd2 100644 --- a/filamento/src/files.rs +++ b/filamento/src/files.rs @@ -9,6 +9,7 @@ use std::{ use tokio::io; use tokio::sync::Mutex; +#[cfg(not(target_arch = "wasm32"))] pub trait FileStore { type Err: Clone + Send + Error; @@ -27,6 +28,19 @@ pub trait FileStore { ) -> impl std::future::Future<Output = Result<(), Self::Err>> + std::marker::Send; } +#[cfg(target_arch = "wasm32")] +pub trait FileStore { + type Err: Clone + Send + Error; + + fn is_stored(&self, name: &str) -> impl std::future::Future<Output = Result<bool, Self::Err>>; + fn store( + &self, + name: &str, + data: &[u8], + ) -> impl std::future::Future<Output = Result<(), Self::Err>>; + fn delete(&self, name: &str) -> impl std::future::Future<Output = Result<(), Self::Err>>; +} + #[derive(Clone, Debug)] pub struct FilesMem { files: Arc<Mutex<HashMap<String, Vec<u8>>>>, @@ -38,8 +52,19 @@ impl FilesMem { files: Arc::new(Mutex::new(HashMap::new())), } } + + pub async fn get_file(&self, name: impl AsRef<str>) -> Option<Vec<u8>> { + let name = name.as_ref(); + self.files.lock().await.get(name).cloned() + } } +#[cfg(all(feature = "opfs", target_arch = "wasm32"))] +pub mod opfs; + +#[cfg(all(feature = "opfs", target_arch = "wasm32"))] +pub use opfs::FilesOPFS; + impl FileStore for FilesMem { type Err = Infallible; |