bookyard-core 0.1.0

Core configuration and catalog model for Bookyard.
Documentation
use std::{fs, path::Path};

use crate::{BookyardConfig, Result, error::io_error};

pub const CONFIG_FILE: &str = "bookyard.toml";

pub fn load_config(path: &Path) -> Result<BookyardConfig> {
    let raw = fs::read_to_string(path).map_err(|source| io_error(path, source))?;
    toml::from_str(&raw).map_err(|source| crate::BookyardError::TomlParse {
        path: path.to_path_buf(),
        source,
    })
}

pub fn save_config(path: &Path, config: &BookyardConfig) -> Result<()> {
    let raw = toml::to_string_pretty(config).map_err(crate::BookyardError::TomlWrite)?;
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|source| io_error(parent, source))?;
    }
    fs::write(path, raw).map_err(|source| io_error(path, source))
}