Skip to main content

StorageBackend

Trait StorageBackend 

Source
pub trait StorageBackend:
    Clone
    + Send
    + Sync {
    // Required methods
    fn extension(&self) -> &str;
    fn serialize<T: Serialize>(&self, data: &T) -> Result<String>;
    fn deserialize<T: DeserializeOwned>(&self, content: &str) -> Result<T>;

    // Provided methods
    fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T> { ... }
    fn write<T: Serialize>(&self, path: &Path, data: &T) -> Result<()> { ... }
}
Expand description

JSON storage backend (default). Trait for storage backend implementations

This allows swapping JSON for TOML, YAML, or other formats in the future.

Required Methods§

Source

fn extension(&self) -> &str

File extension for this storage format (e.g., “json”, “toml”)

§Returns
  • &str - File extension for this storage format
Source

fn serialize<T: Serialize>(&self, data: &T) -> Result<String>

Serialize data to string

§Arguments
  • data - Data to serialize
§Returns
  • Result<String> - Serialized data
§Errors
  • Error::Io - If the data cannot be serialized
Source

fn deserialize<T: DeserializeOwned>(&self, content: &str) -> Result<T>

Deserialize data from string

§Arguments
  • content - Data to deserialize
§Returns
  • Result<T> - Deserialized data
§Errors
  • Error::Io - If the data cannot be deserialized

Provided Methods§

Source

fn read<T: DeserializeOwned>(&self, path: &Path) -> Result<T>

Read and deserialize from file

§Arguments
  • path - Path to file to read
§Returns
  • Result<T> - Deserialized data
§Errors
  • Error::FileRead - If the file cannot be read
Source

fn write<T: Serialize>(&self, path: &Path, data: &T) -> Result<()>

Serialize and write to file

Uses atomic write: writes to temp file then renames to prevent corruption.

§Arguments
  • path - Path to file to write
  • data - Data to serialize and write
§Returns
  • Result<()> - Success or error
§Errors
  • Error::FileWrite - If the file cannot be written

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl StorageBackend for JsonStorage

Source§

impl StorageBackend for TomlStorage

Available on crate feature toml only.