FileStorage

Struct FileStorage 

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

File storage with ACID guarantees and automatic migrations.

Provides:

  • Atomicity: Updates are all-or-nothing via tmp file + atomic rename
  • Consistency: Format validation on load/save
  • Isolation: File locking prevents concurrent modifications
  • Durability: Explicit fsync before rename

Implementations§

Source§

impl FileStorage

Source

pub fn new( path: PathBuf, migrator: Migrator, strategy: FileStorageStrategy, ) -> Result<Self, MigrationError>

Create a new FileStorage instance and load data from file.

This combines initialization and loading into a single operation.

§Arguments
  • path - Path to the storage file
  • migrator - Migrator instance with registered migration paths
  • strategy - Storage strategy configuration
§Behavior

Depends on strategy.load_behavior:

  • CreateIfMissing: Creates empty config if file doesn’t exist
  • ErrorIfMissing: Returns error if file doesn’t exist
§Example
let strategy = FileStorageStrategy::default();
let migrator = Migrator::new();
let storage = FileStorage::new(
    PathBuf::from("config.toml"),
    migrator,
    strategy
)?;
Source

pub fn save(&self) -> Result<(), MigrationError>

Save current state to file atomically.

Uses a temporary file + atomic rename to ensure durability. Retries according to strategy.atomic_write.retry_count.

Source

pub fn config(&self) -> &ConfigMigrator

Get immutable reference to the ConfigMigrator.

Source

pub fn config_mut(&mut self) -> &mut ConfigMigrator

Get mutable reference to the ConfigMigrator.

Source

pub fn query<T>(&self, key: &str) -> Result<Vec<T>, MigrationError>
where T: Queryable + for<'de> Deserialize<'de>,

Query entities from storage.

Delegates to ConfigMigrator::query().

Source

pub fn update<T>( &mut self, key: &str, value: Vec<T>, ) -> Result<(), MigrationError>
where T: Queryable + Serialize,

Update entities in memory (does not save to file).

Delegates to ConfigMigrator::update().

Source

pub fn update_and_save<T>( &mut self, key: &str, value: Vec<T>, ) -> Result<(), MigrationError>
where T: Queryable + Serialize,

Update entities and immediately save to file atomically.

Source

pub fn path(&self) -> &Path

Returns a reference to the storage file path.

§Returns

A reference to the file path where the configuration is 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.