pub struct DirStorage { /* private fields */ }Expand description
Directory-based entity storage with ACID guarantees and automatic migrations.
Manages one file per entity, providing:
- Atomicity: Updates are all-or-nothing via tmp file + atomic rename
- Consistency: Format validation on load/save
- Isolation: Each entity has its own file
- Durability: Explicit fsync before rename
Implementations§
Source§impl DirStorage
impl DirStorage
Sourcepub fn new(
paths: AppPaths,
domain_name: &str,
migrator: Migrator,
strategy: DirStorageStrategy,
) -> Result<Self, MigrationError>
pub fn new( paths: AppPaths, domain_name: &str, migrator: Migrator, strategy: DirStorageStrategy, ) -> Result<Self, MigrationError>
Create a new DirStorage instance.
§Arguments
paths- Application paths managerdomain_name- Domain-specific subdirectory name (e.g., “sessions”, “tasks”)migrator- Migrator instance with registered migration pathsstrategy- Storage strategy configuration
§Behavior
- Resolves the base path using
paths.data_dir()?.join(domain_name) - Creates the directory if it doesn’t exist
- Does not load existing files (lazy loading)
§Errors
Returns MigrationError::IoError if directory creation fails.
§Example
ⓘ
let paths = AppPaths::new("myapp");
let storage = DirStorage::new(
paths,
"sessions",
migrator,
DirStorageStrategy::default(),
)?;Sourcepub fn save<T>(
&self,
entity_name: &str,
id: &str,
entity: T,
) -> Result<(), MigrationError>where
T: Serialize,
pub fn save<T>(
&self,
entity_name: &str,
id: &str,
entity: T,
) -> Result<(), MigrationError>where
T: Serialize,
Save an entity to a file.
§Arguments
entity_name- The entity name registered in the migratorid- The unique identifier for this entity (used as filename)entity- The entity to save
§Process
- Converts the entity to its latest versioned DTO
- Serializes to the configured format (JSON/TOML)
- Writes atomically using temporary file + rename
§Errors
Returns error if:
- Entity name not registered in migrator
- ID contains invalid characters (for Direct encoding)
- Serialization fails
- File write fails
§Example
ⓘ
let session = SessionEntity {
id: "session-123".to_string(),
user_id: "user-456".to_string(),
};
storage.save("session", "session-123", session)?;Sourcepub fn load<D>(&self, entity_name: &str, id: &str) -> Result<D, MigrationError>where
D: DeserializeOwned,
pub fn load<D>(&self, entity_name: &str, id: &str) -> Result<D, MigrationError>where
D: DeserializeOwned,
Load an entity from a file.
§Arguments
entity_name- The entity name registered in the migratorid- The unique identifier for the entity
§Process
- Gets the file path using
id_to_path - Reads the file content to a string
- Deserializes the content to a
serde_json::Value - Migrates the
Valueto the target domain type
§Errors
Returns error if:
- Entity name not registered in migrator
- File not found
- Deserialization fails
- Migration fails
§Example
ⓘ
let session: SessionEntity = storage.load("session", "session-123")?;Sourcepub fn load_all<D>(
&self,
entity_name: &str,
) -> Result<Vec<(String, D)>, MigrationError>where
D: DeserializeOwned,
pub fn load_all<D>(
&self,
entity_name: &str,
) -> Result<Vec<(String, D)>, MigrationError>where
D: DeserializeOwned,
Load all entities from the storage directory.
§Arguments
entity_name- The entity name registered in the migrator
§Returns
A vector of (id, entity) tuples.
§Errors
Returns error if any entity fails to load. This operation is atomic: if any load fails, the whole operation fails.
§Example
ⓘ
let sessions: Vec<(String, SessionEntity)> = storage.load_all("session")?;
for (id, session) in sessions {
println!("Loaded session {} for user {}", id, session.user_id);
}Auto Trait Implementations§
impl Freeze for DirStorage
impl !RefUnwindSafe for DirStorage
impl Send for DirStorage
impl Sync for DirStorage
impl Unpin for DirStorage
impl !UnwindSafe for DirStorage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more