Crate easy_prefs

Source
Expand description

§easy_prefs

A simple, safe, and performant preferences library for Rust applications.

Created by Ever Accountable – an app that helps people overcome compulsive porn use and become their best selves. Visit everaccountable.com for more details.

This library provides an intuitive API for managing preferences using a struct-like interface. Its key design goals are:

  • Ease of Use: Read/write preferences as easily as struct fields.
  • Safety: Uses temporary files for writes to prevent data corruption.
  • Performance: Optimized for fast operations.
  • Testability: Integrates seamlessly with unit tests.
  • Cross-Platform: Works on native platforms and WebAssembly (WASM).

Limitation: Not suited for large datasets. All data is held in memory, and the entire file is rewritten on save. For substantial data, use a database instead.

§Single-Instance Constraint

The load() method enforces that only one instance of a preferences struct exists at a time, using a static atomic flag. This prevents data races in production but can cause issues in parallel test execution. Tests using load() are combined into a single test to avoid conflicts.

§Error Handling

The library provides two loading methods:

  • load() - Always succeeds by returning defaults on errors (panics in debug mode)
  • load_with_error() - Returns Result<Self, LoadError> for explicit error handling

§WASM Support

This library supports WebAssembly targets for use in browser extensions and web applications. When compiled to WASM, preferences are stored in localStorage instead of the file system.

Re-exports§

pub use once_cell;
pub use paste;
pub use toml;
pub use web_time;

Modules§

storage

Macros§

easy_prefs
Macro to define a preferences struct with persistence.

Enums§

LoadError
Errors that can occur when loading preferences.