quickpeep/quickpeep_raker/src/config.rs

38 lines
1.2 KiB
Rust

use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Config for a raker. All paths are relative to the config file if needed, but will be resolved
/// when loading.
pub struct RakerConfig {
/// Path to data files
pub data_dir: PathBuf,
/// Path to seeds
pub seed_dir: PathBuf,
/// Path to the raker's workbench (queue etc)
pub workbench_dir: PathBuf,
/// Directory where new rake packs will be emitted
pub emit_dir: PathBuf,
}
impl RakerConfig {
/// Loads a config at the specified path.
/// Will resolve all the paths in the RakerConfig for you.
pub fn load(path: &Path) -> anyhow::Result<RakerConfig> {
let config_dir = path.parent().context("Can't get parent of config file.")?;
let bytes = std::fs::read(path)?;
let mut raker_config: RakerConfig = toml::from_slice(&bytes)?;
raker_config.data_dir = config_dir.join(raker_config.data_dir);
raker_config.seed_dir = config_dir.join(raker_config.seed_dir);
raker_config.workbench_dir = config_dir.join(raker_config.workbench_dir);
raker_config.emit_dir = config_dir.join(raker_config.emit_dir);
Ok(raker_config)
}
}