pub struct State { /* private fields */ }Implementations§
Source§impl State
impl State
Sourcepub fn load(file: &Path) -> Self
pub fn load(file: &Path) -> Self
Reads the state file. A missing or unreadable file gives the defaults.
An entry whose value is not a string is kept apart (see
State::has_invalid_value) and does not affect the other entries.
A file that cannot be used gives the defaults: a read error other than
“not found”, or content that is not a JSON object. A missing or blank
file is a normal first run. After an unusable file, the next
State::save first copies whatever file it finds, unless it already
holds exactly the document to save, to
<name>.invalid-<unix seconds>-<n>. That covers a file that was
malformed, and one that was only unreadable or half-written at load
time and is valid by the time of the save. State::save fails,
without replacing the file, if it cannot read the file or write the
copy. Only space, tab, carriage return and line feed count as blank.
A non-string value keeps its exact JSON text, such as a number with
more digits than u64 or f64 hold.
Keys and values saved under a Space’s former id are read as the
Space’s own (see crate::former_ids); the file is not changed.
Sourcepub fn get(&self, key: &str) -> Option<&str>
pub fn get(&self, key: &str) -> Option<&str>
The string value of key. None for a missing key and for a key
whose stored value is not a string: use State::has_invalid_value
to tell them apart before calling State::set.
Sourcepub fn has_invalid_value(&self, key: &str) -> bool
pub fn has_invalid_value(&self, key: &str) -> bool
True when the file holds a non-string value for key that has not
been replaced or removed. A caller that must not overwrite unreadable
data skips State::set while this is true; calling State::set or
State::remove is the explicit reset.
Sourcepub fn set(&mut self, key: impl Into<String>, value: impl Into<String>)
pub fn set(&mut self, key: impl Into<String>, value: impl Into<String>)
Stores value, replacing any value, including an invalid one.
Sourcepub fn save(&self, file: &Path) -> Result<()>
pub fn save(&self, file: &Path) -> Result<()>
Writes the file atomically: a temporary file in the same directory,
then a rename over the old file. Invalid entries read by
State::load are written back with their exact JSON text. A current
file that is not a JSON object, or that differs from this state after
a failed load, is copied aside first (see State::load). The first
successful save after a failed load ends the check, so later saves
make no more copies.