Skip to main content

digdigdig3_station/settings/
mod.rs

1//! General-purpose JSON settings/state persistence store.
2//!
3//! Works on both native (file-backed) and wasm32 (OPFS-backed).
4//! Keys are arbitrary strings; values are `serde_json::Value` so any
5//! `serde::Serialize` / `serde::de::DeserializeOwned` type round-trips
6//! through the store without a custom codec.
7//!
8//! # Usage (native)
9//!
10//! ```rust,no_run
11//! # async fn example() -> Result<(), digdigdig3_station::SettingsError> {
12//! use digdigdig3_station::SettingsStore;
13//!
14//! let mut store = SettingsStore::open("./my_app", "ui-state").await?;
15//! store.set("theme", &"dark")?;
16//! store.save().await?;
17//! # Ok(())
18//! # }
19//! ```
20//!
21//! # Usage (wasm32)
22//!
23//! ```rust,ignore
24//! let mut store = SettingsStore::open("ui-state").await?;
25//! store.set("theme", &"dark")?;
26//! store.save().await?;
27//! ```
28
29use std::fmt;
30
31// ── cfg-split (mirrors series/mod.rs exactly) ─────────────────────────────────
32
33#[cfg(not(target_arch = "wasm32"))]
34mod store;
35
36#[cfg(target_arch = "wasm32")]
37#[path = "store_wasm.rs"]
38mod store;
39
40pub use store::SettingsStore;
41
42// ── Shared error type ─────────────────────────────────────────────────────────
43
44/// Errors from `SettingsStore` operations.
45///
46/// Both native and wasm32 impls use this single enum so call sites compile
47/// unchanged on either target.
48#[derive(Debug)]
49pub enum SettingsError {
50    /// I/O error (native only — file read/write/rename failures).
51    Io(String),
52    /// JSON serialization or deserialization error.
53    Serde(String),
54    /// OPFS error (wasm32 only — browser storage API failures).
55    Opfs(String),
56}
57
58impl fmt::Display for SettingsError {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            SettingsError::Io(msg) => write!(f, "settings I/O error: {msg}"),
62            SettingsError::Serde(msg) => write!(f, "settings JSON error: {msg}"),
63            SettingsError::Opfs(msg) => write!(f, "settings OPFS error: {msg}"),
64        }
65    }
66}
67
68impl std::error::Error for SettingsError {}