Skip to main content

DirStorage

Struct DirStorage 

Source
pub struct DirStorage { /* private fields */ }
Expand description

Directory-based entity storage with ACID guarantees and automatic migrations.

Manages one file per entity. Raw IO (atomic rename, fsync, temp-file cleanup, ID encoding/decoding, directory listing) is fully delegated to local_store::DirStorage.

§Responsibilities

  • Serialising/deserialising entities to/from the configured format.
  • Delegating all ACID / atomic-rename / lock operations to inner.
  • Applying Migrator-based schema evolution on load.

Implementations§

Source§

impl DirStorage

Source

pub fn new( paths: AppPaths, domain_name: &str, migrator: Migrator, strategy: DirStorageStrategy, ) -> Result<Self, MigrationError>

Create a new DirStorage instance.

§Arguments
  • paths - Application paths manager.
  • domain_name - Domain-specific subdirectory name (e.g., "sessions").
  • migrator - Migrator instance with registered migration paths.
  • strategy - Storage strategy (format, encoding, atomic-write config).
§Behavior

Delegates directory creation to local_store::DirStorage::new. The base path is resolved as data_dir/domain_name and created if absent.

§Errors

Returns MigrationError::Store wrapping a StoreError::IoError if directory creation fails.

Source

pub fn save<T>( &self, entity_name: &str, id: &str, entity: T, ) -> Result<(), MigrationError>
where T: Serialize,

Save an entity to its file atomically.

§Arguments
  • entity_name - Entity name registered in the migrator.
  • id - Unique identifier for this entity (used as the filename stem).
  • entity - Value to persist; must implement serde::Serialize.
§Process
  1. Converts entity to its latest versioned DTO via migrator.save_domain_flat.
  2. Serialises to the configured format (JSON or TOML).
  3. Delegates atomic write (tmp file + fsync + rename) to inner.save_raw_string.
§Errors

Returns MigrationError if:

  • entity_name is not registered in the migrator.
  • id contains invalid characters for the configured encoding.
  • Serialisation or format conversion fails.
  • The underlying file write fails.
Source

pub fn load<D>(&self, entity_name: &str, id: &str) -> Result<D, MigrationError>

Load an entity from its file, applying schema migrations if needed.

§Arguments
  • entity_name - Entity name registered in the migrator.
  • id - Unique identifier for the entity.
§Process
  1. Reads raw string content via inner.load_raw_string.
  2. Deserialises to serde_json::Value (converting from TOML if needed).
  3. Applies schema migration via migrator.load_flat_from.
§Errors

Returns MigrationError if the file is missing, parsing fails, or migration fails.

Source

pub fn list_ids(&self) -> Result<Vec<String>, MigrationError>

List all entity IDs in the storage directory in lexicographic ascending order.

§Returns

A Vec<String> of decoded entity IDs, sorted lexicographically.

§Errors

Returns MigrationError if the directory cannot be read or a filename cannot be decoded.

Source

pub fn load_all<D>( &self, entity_name: &str, ) -> Result<Vec<(String, D)>, MigrationError>

Load all entities from the storage directory.

§Arguments
  • entity_name - Entity name registered in the migrator.
§Returns

A Vec<(id, entity)> of all stored entities, ordered by ID.

§Errors

Returns MigrationError if any entity fails to load. The whole operation fails atomically.

Source

pub fn exists(&self, id: &str) -> Result<bool, MigrationError>

Check whether an entity file exists.

§Arguments
  • id - Entity identifier.
§Returns

true if the file exists; false otherwise.

§Errors

Returns MigrationError if id cannot be encoded.

Source

pub fn delete(&self, id: &str) -> Result<(), MigrationError>

Delete an entity file (idempotent).

§Arguments
  • id - Entity identifier.
§Behavior

Deleting a non-existent entity is not an error (local_store::DirStorage guarantees idempotency).

§Errors

Returns MigrationError if the underlying file deletion fails.

Source

pub fn base_path(&self) -> &Path

Returns a reference to the base directory path.

§Returns

A reference to the resolved base directory path where entity files are stored.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.