Expand description
Generic app-data path resolution and atomic JSON persistence helpers.
This module owns mechanics only: given a path and a serializable value, write it atomically; given a path, read and parse a JSON value. It has no knowledge of any particular product’s settings shape or file name — the caller supplies both.
Functions§
- app_
data_ path - Resolve
file_nameinside the app’s config directory (e.g.~/.config/<app>/settings.jsonon Linux). Does not create the directory; callers that write should create parents first (seewrite_json_atomic). - atomic_
write - Write
datatopathatomically: write to a per-write unique temp file, fsync, then rename onto the target. - env_
var_ or_ none - Read an environment variable, returning
Nonefor a missing or whitespace-only value. The returned value is not trimmed — callers that need a trimmed value should trim it themselves, matching how this distinguishes “unset” from “set to something”. - parse_
json - Parse
contentsas JSON, producing a message that namespathon failure. Split out fromread_json_or_defaultso parse failures can be tested without touching the filesystem. - read_
json_ or_ default - Read and parse
pathas JSON. A missing file is not an error — it returnsT::default(), matching the common “no settings saved yet” case. - write_
json_ atomic - Serialize
valueto pretty JSON and write it topathatomically, creating parent directories as needed.