Function cli_config::init

source ·
pub fn init<T>(config: T, prefix: &str, filename: &str) -> Result<PathBuf>where
    T: Serialize + Default + File,
Expand description

Initialize the configuration file for the specified type.

This function returns the path to the configuration file for the specified type. If the file does not exist, it will be created.

Arguments

  • config - The configuration object to initialize the file with.
  • prefix - The name of the folder that will contain the configuration file.
  • filename - The name of the configuration file.

Examples

use cli_config::fs::JSONFile;

#[derive(serde::Serialize, serde::Deserialize, Default)]
struct MyConfig {
    pub is_first_run: bool,
}

impl JSONFile for MyConfig {}

let config = MyConfig::default();
let prefix = "my-app";
let filename = "config.json";

// Initialize the configuration file
let config_path = cli_config::init(config, prefix, filename).unwrap();

// Use the configuration file
let loaded_config = MyConfig::load(&config_path).unwrap();
println!("Is my first run? {}", loaded_config.is_first_run);