pub struct Settings {
pub auto_save: bool,
/* private fields */
}Expand description
Thread-safe JSON settings with hierarchical key access and atomic persistence.
Supports dot-notation keys ("section.key.0") where numeric segments
become array indices. Atomic writes with two-generation backup ensure
crash safety; a SHA256 integrity trailer catches silent truncation;
a __data_loss__ breadcrumb is persisted when reload finds all
on-disk files unusable, so application-level self-diagnosis can
surface the data-loss event until an operator clears it.
Fields§
§auto_save: boolImplementations§
Source§impl Settings
impl Settings
Sourcepub fn new(file_path: impl Into<PathBuf>) -> Self
pub fn new(file_path: impl Into<PathBuf>) -> Self
Create settings backed by a file. Loads existing data on creation.
Sourcepub fn get<T: DeserializeOwned + Serialize + Clone>(
&self,
key: &str,
default: T,
) -> T
pub fn get<T: DeserializeOwned + Serialize + Clone>( &self, key: &str, default: T, ) -> T
Get a value at the given key path with a default fallback.
If the key doesn’t exist, it is created with the default value. If the stored type is incompatible with the default’s type, the default is returned.
Sourcepub fn set<T: Serialize>(&self, key: &str, value: T)
pub fn set<T: Serialize>(&self, key: &str, value: T)
Set a value at the given key path. Creates intermediate nodes as needed.
Sourcepub fn length(&self, key: &str) -> i64
pub fn length(&self, key: &str) -> i64
Return the length of an array or object at the given key, or -1 if missing.
Sourcepub fn move_key(&self, from: &str, to: &str) -> bool
pub fn move_key(&self, from: &str, to: &str) -> bool
Move a value from one key to another. Returns true on success.
Sourcepub fn save(&self) -> Result<()>
pub fn save(&self) -> Result<()>
Persist settings to disk with atomic write and backup rotation.
Sourcepub fn reload(&self) -> Result<()>
pub fn reload(&self) -> Result<()>
Reload settings from disk. Falls back to .bak and .bak2, verifies
integrity trailers, and writes a __data_loss__ breadcrumb if all
three files are unusable.
Sourcepub fn load_json(&self, json: &str) -> Result<()>
pub fn load_json(&self, json: &str) -> Result<()>
Replace all settings with data from a JSON string.
Reserved keys (__integrity__, __data_loss__) are stripped from
the incoming payload — they are library-controlled and must not
be set by clients. Any existing __data_loss__ breadcrumb is
carried forward so a partial UI save cannot silently hide the
warning; it is cleared only by clear_data_loss_flag.
Sourcepub fn get_from_env<T: DeserializeOwned + Serialize + Clone + FromStr>(
&self,
env_name: &str,
key: &str,
default: T,
) -> T
pub fn get_from_env<T: DeserializeOwned + Serialize + Clone + FromStr>( &self, env_name: &str, key: &str, default: T, ) -> T
Read from environment variable first, then fall back to settings key.
Sourcepub fn recovered_from(&self) -> RecoveredFrom
pub fn recovered_from(&self) -> RecoveredFrom
Where the last reload sourced its data from.
Sourcepub fn has_data_loss_flag(&self) -> bool
pub fn has_data_loss_flag(&self) -> bool
Return true while the __data_loss__ breadcrumb is present.
The breadcrumb is written on boot when all three settings files
were unreadable and persists across reboots until
Self::clear_data_loss_flag is called. Use this as the
trigger for a critical self-diagnosis message.
Sourcepub fn data_loss_info(&self) -> Option<Value>
pub fn data_loss_info(&self) -> Option<Value>
Return a clone of the breadcrumb object if it is present.
Sourcepub fn clear_data_loss_flag(&self) -> Result<()>
pub fn clear_data_loss_flag(&self) -> Result<()>
Remove the data-loss breadcrumb and persist.
Call this after an operator has acknowledged the data-loss
event. Because get never auto-creates the breadcrumb, it
stays cleared unless a fresh data-loss event occurs. No-op if
the flag is not set.