Skip to main content

Crate day_part_preferences

Crate day_part_preferences 

Source
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§

MemoryStore
An in-process store that forgets everything at exit: tests, and deterministic script runs.
NativeStore
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§

PrefValue
A value a Pref can hold: encoded to one string in the store and back. Implemented for the plain scalars and strings; enums get it from pref_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 with install_store before building the settings UI; tests use MemoryStore.

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, or None if it is absent (or no store is available on the platform). A stored empty string is Some(""), distinct from an absent key.
install_store
Replace the store every Pref reads 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/remove returned 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. Returns true if a value existed and was removed, false if the key was already absent (or the delete could not be committed).
set
Persist value under key, overwriting any previous value. Returns true when the write was committed. On Apple platforms this always succeeds; on Android it reflects SharedPreferences.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 yields false).