Skip to main content

save_new

Function save_new 

Source
pub fn save_new<T: Serialize>(
    value: &T,
    path: impl AsRef<Path>,
    format: Format,
    key: &str,
) -> Result<(), Error>
Expand description

As save, but refuses if path already exists.

Viper calls this SafeWriteConfig. The case is a setup wizard or a --init subcommand: writing a configuration somebody already wrote by hand, silently, is the one failure mode those have.

Written directly rather than through a temporary file and a rename. The rename is what makes save atomic against an existing file, and there is no existing file here by definition — so create_new on the target itself is both simpler and free of the race a check-then-write would have.

§Errors

If the file exists, if value cannot be serialized, if the format’s feature is off, or if the file cannot be written.