Expand description
Widget state persistence for save/restore across sessions.
This module provides the StateRegistry and StorageBackend infrastructure
for persisting widget state. It works with the Stateful trait from ftui-widgets.
§Architecture
┌──────────────────────────────────────────────────────────────┐
│ StateRegistry │
│ - In-memory cache of widget states │
│ - Delegates to StorageBackend for persistence │
│ - Provides load/save/clear operations │
└──────────────────────────────────────────────────────────────┘
│
▼
┌──────────────────────────────────────────────────────────────┐
│ StorageBackend │
│ - MemoryStorage: in-memory (testing, ephemeral) │
│ - FileStorage: JSON file (requires state-persistence) │
└──────────────────────────────────────────────────────────────┘§Design Invariants
- Graceful degradation: Storage failures never panic; operations return
Result. - Atomic writes: File storage uses write-rename pattern to prevent corruption.
- Partial load tolerance: Missing or corrupt entries use
Default::default(). - Type safety: Registry is type-erased internally but type-safe at boundaries.
§Failure Modes
| Failure | Cause | Behavior |
|---|---|---|
StorageError::Io | File I/O failure | Returns error, cache unaffected |
StorageError::Serialization | JSON encode/decode | Entry skipped, logged |
StorageError::Corruption | Invalid file format | Load returns partial data |
| Missing entry | First run, key changed | Default::default() used |
§Feature Gates
state-persistence: EnablesFileStoragewith JSON serialization. Without this feature, onlyMemoryStorageis available.
Structs§
- Memory
Storage - In-memory storage backend for testing and ephemeral state.
- Registry
Stats - Statistics about the state registry.
- State
Registry - Central registry for widget state persistence.
- Stored
Entry - A serialized state entry with version metadata.
Enums§
- Storage
Error - Errors that can occur during state storage operations.
Traits§
- Storage
Backend - Trait for pluggable state storage backends.
Type Aliases§
- Storage
Result - Result type for storage operations.