Expand description
day-part-preferences: a headless cross-platform persistent key/value store. No UI; any Rust code can
depend on this crate and call set / get / remove / contains to persist small
strings across launches through the platform’s native preferences facility.
day_part_preferences::set("greeting", "hello");
assert_eq!(day_part_preferences::get("greeting").as_deref(), Some("hello"));
assert!(day_part_preferences::contains("greeting"));
day_part_preferences::remove("greeting");Platform selection is purely #[cfg(target_os)]/#[cfg(target_env)] (persistence is an OS
concern, not a widget-toolkit one): macOS and iOS share one NSUserDefaults file, Android uses
SharedPreferences (via a Java shim staged by day build), and Linux / Windows / HarmonyOS
share a file-backed store under the per-user config directory. Values persist until removed and
survive process restarts. Platforms without any store fall back to a no-op that always reports
failure/absence.
This is a small string store for user settings and lightweight app state, not a database. Keep values modest; large blobs belong in a file. See docs/prefs.md for the per-platform matrix.
Macros§
- pref_
enum - Declare an enum that round-trips through the store under short stable tags:
Structs§
- Memory
Store - An in-process store that forgets everything at exit: tests, and deterministic script runs.
- Native
Store - The platform store through day-part-preferences (docs/prefs.md):
NSUserDefaults,SharedPreferences, a per-app config file, localStorage. - Pref
- A typed, persisted, reactive setting (docs/settings.md), declared once as a static:
Traits§
- Pref
Value - A value a
Prefcan hold: encoded to one string in the store and back. Implemented for the plain scalars and strings; enums get it frompref_enum!. - Store
- Where values live (docs/settings.md). The default,
NativeStore, is day-part-preferences: the platform’s own preferences facility. An app installs another withinstall_storebefore building the settings UI; tests useMemoryStore.
Functions§
- bind
- Two-way-bind a signal to a stored preference: seed the signal from the store now (when a
stored value exists and parses as
T), then persist every later change. Call it right after creating the signal, before anything reads it. The write-back lives in the current reactive scope, so it stops with the page that created the signal. - contains
- Whether a value is currently stored under
key. - get
- Read the string stored under
key, orNoneif it is absent (or no store is available on the platform). A stored empty string isSome(""), distinct from an absent key. - install_
store - Replace the store every
Prefreads and writes. Call before any pref is read (before building the settings UI); signals already seeded keep their values. - persistence_
failures - The keys whose last write the store refused (
Store::set/removereturned false), in key order; empty while every write has landed. A failed write is also logged. The in-memory value stays what the user chose for this run; it will not survive a relaunch, which is what an app watching this signal can say. A key leaves the list when its own next write lands; another key’s success never clears it. - remove
- Delete the value stored under
key. Returnstrueif a value existed and was removed,falseif the key was already absent (or the delete could not be committed). - set
- Persist
valueunderkey, overwriting any previous value. Returnstruewhen the write was committed. On Apple platforms this always succeeds; on Android it reflectsSharedPreferences.Editor.commit(); on the file-backed platforms it reflects whether the store file could be written (a missing config directory or a read-only home yieldsfalse).