Skip to main content

codei_config/
template.rs

1use std::fs;
2use std::path::PathBuf;
3
4use crate::error::ConfigError;
5use crate::paths::{user_config_dir, user_config_path};
6
7/// Default user config template written by `codei config init`.
8pub fn default_config_template() -> &'static str {
9    include_str!("default_config.toml")
10}
11
12/// Creates `~/.config/codei/config.toml` if it does not exist.
13/// Returns the path and whether a new file was created.
14pub fn init_user_config() -> Result<(PathBuf, bool), ConfigError> {
15    let path = user_config_path();
16    if path.exists() {
17        return Ok((path, false));
18    }
19
20    let dir = user_config_dir();
21    fs::create_dir_all(&dir).map_err(|source| ConfigError::CreateDir {
22        path: dir.clone(),
23        source,
24    })?;
25
26    fs::write(&path, default_config_template()).map_err(|source| ConfigError::Write {
27        path: path.clone(),
28        source,
29    })?;
30
31    Ok((path, true))
32}