saja 0.1.0

Zero-configuration C build system
/*
 * Configuration.
 *
 * Copyright (C) 2026  Madeleine Choi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::{
    fs::OpenOptions,
    io::{Read, Write},
    path::Path,
};

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
    pub name: String,
    pub author: String,
    pub standard: String,
    pub version: String,
    pub license: String,
}

impl Config {
    pub fn load(target: &Path) -> anyhow::Result<Self> {
        let mut file = OpenOptions::new()
            .read(true)
            .open(target.join("saja.toml"))?;

        let mut buf = String::new();

        file.read_to_string(&mut buf)?;

        Ok(toml::from_str(&buf)?)
    }

    pub fn store(&self, target: &Path) -> anyhow::Result<()> {
        let mut file = OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(target.join("saja.toml"))?;

        let config = toml::to_string_pretty(self)?;

        write!(file, "{config}")?;

        Ok(())
    }
}