pub struct JsonFileAmpConfigRepository { /* private fields */ }Expand description
File-based amp-config repository backed by a single JSON document.
The repository stores one full amplifier snapshot at config_path. It is
intentionally simple: every save overwrites the entire file and every load
reads the whole document into memory.
This implementation is useful while the configuration remains relatively small and the project does not yet need querying, concurrency control, or schema migrations provided by a database.
Implementations§
Trait Implementations§
Source§impl AmpConfigPersistence for JsonFileAmpConfigRepository
impl AmpConfigPersistence for JsonFileAmpConfigRepository
Source§fn load(&self) -> Result<Option<AmpConfigDto>, String>
fn load(&self) -> Result<Option<AmpConfigDto>, String>
Loads and deserializes the persisted JSON file.
Behavior summary:
- missing file ->
Ok(None) - unreadable file ->
Err(String) - invalid JSON ->
Err(String) - valid JSON ->
Ok(Some(AmpConfigDto))
Source§fn save(&self, config: &AmpConfigDto) -> Result<(), String>
fn save(&self, config: &AmpConfigDto) -> Result<(), String>
Serializes the supplied config snapshot and writes it to disk atomically.
The write strategy is:
- Serialize the snapshot to JSON.
- Write the JSON to a sibling temporary file (same directory as the target, so the subsequent rename stays on the same filesystem/volume).
sync_allthe temporary file so the bytes are flushed to the OS.renamethe temporary file over the target path. On all major OSes this rename is atomic at the filesystem level, so a crash between steps 2-3 leaves the old file intact and a crash between steps 3-4 leaves a harmless temporary file that is cleaned up on the next successful save.
Parent directories are created automatically when necessary. The JSON is
formatted with to_string_pretty so it remains reasonably human-readable
during development and debugging.