Native configuration persistence
Path coverage: src-tauri/…/conf.rs
Overview
Native app configuration is represented by AppConf in src-tauri/…/conf.rs. The persisted schema is a JSON object with four frontend-facing settings: theme, stay_on_top, ask_mode, and mac_titlebar_hidden. Defaults come from AppConf::new(): theme is "system", stay_on_top and ask_mode are false, and mac_titlebar_hidden defaults to true only on macOS and false elsewhere via cfg(target_os = "macos").
AppConf::get_conf_path() stores the file at the Tauri config directory under com.nofwl.chatgpt/config.json; get_scripts_path() uses the same app-specific config root and appends scripts. AppConf::load_script() reads a named file from that scripts directory and returns an empty string if the file is missing or unreadable, so script storage failures are silent to callers.
AppConf::load() is the main loading path. If config.json does not exist, it creates a default AppConf, persists it through save(), and returns it. Existing files are read as strings and deserialized with Serde. If deserialization into AppConf fails, the loader logs the error, starts from current defaults, merges in the existing JSON object via amend(), saves the amended result back to disk, and returns it. This gives older config files a migration path when fields are added, as long as the existing file is still valid JSON and the merged values can deserialize into the current AppConf schema.
AppConf::save() creates the parent config directory with fs::create_dir_all, serializes the whole struct with serde_json::to_string_pretty, and overwrites config.json with File::create. There is no partial write or atomic rename; the file on disk is always the full current schema after a successful save.
AppConf::get_theme() is the native theme resolver used by callers that need a Tauri Theme. It reloads persisted config, then maps "system" through dark_light::detect() (Default falls back to Theme::Light), maps "dark" to Theme::Dark, and treats any other theme string as light.
Coupling
- If a new persisted setting is added to
AppConfin src-tauri/…/conf.rs, also updateAppConf::new()and verifyamend()can migrate old config.json files.load()only recovers from schema drift by merging an existing JSON object into defaults; missing defaults or incompatible field types will keep deserialization failing. - If frontend setting names or accepted theme values change, keep them aligned with the serialized field names and
get_theme()matching in src-tauri/…/conf.rs. Unknownthemestrings do not error; they silently resolve toTheme::Light. - If the app identifier or config storage location changes in
get_conf_path(), updateget_scripts_path()consistently. Both persist underconfig_dir()/com.nofwl.chatgpt, so changing only one path would split settings and scripts across different config roots.
Risks
- src-tauri/…/conf.rs:
load()callsserde_json::from_str(&contents)?again inside the error-recovery branch beforeamend(). If the file is malformed JSON rather than merely missing fields, recovery returns an error after logging and does not rewrite defaults. - src-tauri/…/conf.rs:
save()overwrites config.json directly withFile::create; if the process stops between truncation andwrite_all, the nextload()may see malformed JSON and fail rather than restore defaults.