Skip to main content

write_user_preset

Function write_user_preset 

Source
pub fn write_user_preset(preset: &ProviderPreset) -> Result<WrittenPreset>
Expand description

Serialize preset to TOML and write it to <user_data_dir>/providers.d/<id>.toml. Creates the directory if missing. Used by recursive init’s “save as reusable preset?” path (init.rs) and by recursive providers add so the persistence surface lives in one place rather than being reimplemented at each caller.

On Linux/macOS the file is created with mode 0644 (minus umask), the same as std::fs::write. The file is not a secret (the API key is stored separately under <user_data_dir>/secrets.env), so 0644 is acceptable here.

Errors are mapped to the existing crate::error::Error variants so call sites that already thread Result<_> through don’t need a separate io::Error plumbing.

The on-disk shape matches the PresetsFile { providers: Vec<_> } envelope that additional_presets() parses via toml::from_str. Hand-concatenating [[providers]] in front of a serialised preset is brittle (and indeed tripped the parser on first round), so we emit it through a tiny wrapper struct.