brinkhaustools

Struct Settings

Source
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: bool

Implementations§

Source§

impl Settings

Source

pub fn new(file_path: impl Into<PathBuf>) -> Self

Create settings backed by a file. Loads existing data on creation.

Source

pub fn in_memory() -> Self

Create in-memory-only settings (no file persistence).

Source

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.

Source

pub fn set<T: Serialize>(&self, key: &str, value: T)

Set a value at the given key path. Creates intermediate nodes as needed.

Source

pub fn exists(&self, key: &str) -> bool

Check if a key path exists.

Source

pub fn length(&self, key: &str) -> i64

Return the length of an array or object at the given key, or -1 if missing.

Source

pub fn delete(&self, key: &str) -> bool

Delete a key. Returns true if the key existed.

Source

pub fn move_key(&self, from: &str, to: &str) -> bool

Move a value from one key to another. Returns true on success.

Source

pub fn save(&self) -> Result<()>

Persist settings to disk with atomic write and backup rotation.

Source

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.

Source

pub fn to_json(&self) -> String

Serialize current settings to a JSON string.

Source

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.

Source

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.

Source

pub fn raw_data(&self) -> Value

Get raw JSON data (for testing and inspection).

Source

pub fn recovered_from(&self) -> RecoveredFrom

Where the last reload sourced its data from.

Source

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.

Source

pub fn data_loss_info(&self) -> Option<Value>

Return a clone of the breadcrumb object if it is present.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more