data_modelling_core/storage/
mod.rs

1//! Storage backend abstraction
2//!
3//! Defines the StorageBackend trait and implementations for different storage systems:
4//! - FileSystemStorageBackend: Native file system (for native apps)
5//! - BrowserStorageBackend: Browser storage APIs (for WASM apps)
6//! - ApiStorageBackend: HTTP API (for online mode, default)
7
8use async_trait::async_trait;
9
10/// Error type for storage operations
11#[derive(Debug, thiserror::Error)]
12pub enum StorageError {
13    #[error("File not found: {0}")]
14    FileNotFound(String),
15    #[error("Directory not found: {0}")]
16    DirectoryNotFound(String),
17    #[error("IO error: {0}")]
18    IoError(String),
19    #[error("Serialization error: {0}")]
20    SerializationError(String),
21    #[error("Network error: {0}")]
22    NetworkError(String),
23    #[error("Permission denied: {0}")]
24    PermissionDenied(String),
25    #[error("Storage backend error: {0}")]
26    BackendError(String),
27}
28
29/// Trait for storage backends
30///
31/// This trait abstracts file operations, directory operations, and model-specific operations
32/// across different storage systems (file system, browser storage, HTTP API).
33#[async_trait(?Send)]
34pub trait StorageBackend: Send + Sync {
35    /// Read a file from storage
36    async fn read_file(&self, path: &str) -> Result<Vec<u8>, StorageError>;
37
38    /// Write a file to storage
39    async fn write_file(&self, path: &str, content: &[u8]) -> Result<(), StorageError>;
40
41    /// List files in a directory
42    async fn list_files(&self, dir: &str) -> Result<Vec<String>, StorageError>;
43
44    /// Check if a file exists
45    async fn file_exists(&self, path: &str) -> Result<bool, StorageError>;
46
47    /// Delete a file
48    async fn delete_file(&self, path: &str) -> Result<(), StorageError>;
49
50    /// Create a directory
51    async fn create_dir(&self, path: &str) -> Result<(), StorageError>;
52
53    /// Check if a directory exists
54    async fn dir_exists(&self, path: &str) -> Result<bool, StorageError>;
55}
56
57// Storage backend implementations
58#[cfg(feature = "native-fs")]
59pub mod filesystem;
60
61#[cfg(feature = "api-backend")]
62pub mod api;
63
64#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
65pub mod browser;