Skip to main content

bookyard_core/
config.rs

1use std::{fs, path::Path};
2
3use crate::{BookyardConfig, Result, error::io_error};
4
5pub const CONFIG_FILE: &str = "bookyard.toml";
6
7pub fn load_config(path: &Path) -> Result<BookyardConfig> {
8    let raw = fs::read_to_string(path).map_err(|source| io_error(path, source))?;
9    toml::from_str(&raw).map_err(|source| crate::BookyardError::TomlParse {
10        path: path.to_path_buf(),
11        source,
12    })
13}
14
15pub fn save_config(path: &Path, config: &BookyardConfig) -> Result<()> {
16    let raw = toml::to_string_pretty(config).map_err(crate::BookyardError::TomlWrite)?;
17    if let Some(parent) = path.parent() {
18        fs::create_dir_all(parent).map_err(|source| io_error(parent, source))?;
19    }
20    fs::write(path, raw).map_err(|source| io_error(path, source))
21}