pub struct SettingsStore { /* private fields */ }Expand description
Native JSON settings store.
All get/set/remove/contains/keys operations are synchronous (in-memory).
Only open and save are async (matching the wasm32 sibling API).
Implementations§
Source§impl SettingsStore
impl SettingsStore
Sourcepub async fn open(
root: impl AsRef<Path>,
namespace: &str,
) -> Result<Self, SettingsError>
pub async fn open( root: impl AsRef<Path>, namespace: &str, ) -> Result<Self, SettingsError>
Open (or create) a settings store.
root — directory that will contain the settings file.
namespace — base name of the file ({namespace}.json).
Creates parent directories if missing. If the file exists it is parsed
into the in-memory map; a corrupt or unreadable file returns Err
rather than silently wiping it.
Sourcepub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
pub fn get<T: DeserializeOwned>(&self, key: &str) -> Option<T>
Look up a key and deserialize it as T.
Returns None when the key is absent or the value cannot be
deserialized as T (wrong type — not a panic).
Sourcepub fn set<T: Serialize>(
&mut self,
key: &str,
value: &T,
) -> Result<(), SettingsError>
pub fn set<T: Serialize>( &mut self, key: &str, value: &T, ) -> Result<(), SettingsError>
Serialize value as JSON and insert it under key.
The store is only modified in memory; call save() to persist.
Sourcepub async fn save(&self) -> Result<(), SettingsError>
pub async fn save(&self) -> Result<(), SettingsError>
Persist the in-memory map to disk atomically.
Writes to {path}.tmp first, then renames it over path. This ensures
the previous file is never partially overwritten on a crash.