devist 0.17.2

Project bootstrap CLI for AI-assisted development. Spin up new projects from templates, manage backends, and keep your codebase comprehensible.
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;

use crate::paths;

#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
    pub default_template: Option<String>,
    pub projects_dir: Option<String>,
    pub templates_repo: Option<String>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            default_template: Some("react-supabase".to_string()),
            projects_dir: None,
            templates_repo: Some("https://github.com/WebchemistCorp/devist-templates".to_string()),
        }
    }
}

impl Config {
    #[allow(dead_code)]
    pub fn load() -> Result<Self> {
        let path = paths::config_file()?;
        if !path.exists() {
            let cfg = Config::default();
            cfg.save()?;
            return Ok(cfg);
        }

        let text = fs::read_to_string(&path)
            .with_context(|| format!("Failed to read {}", path.display()))?;
        let cfg: Config =
            toml::from_str(&text).with_context(|| format!("Failed to parse {}", path.display()))?;
        Ok(cfg)
    }

    pub fn save(&self) -> Result<()> {
        let path = paths::config_file()?;
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        let text = toml::to_string_pretty(self)?;
        fs::write(&path, text)?;
        Ok(())
    }
}