pikadick/
setup.rs

1use crate::config::{
2    Config,
3    Severity,
4};
5use anyhow::{
6    ensure,
7    Context,
8};
9use camino::Utf8Path;
10
11/// Load a config.
12///
13/// This prints to the stderr directly.
14/// It is intended to be called BEFORE the loggers are set up.
15pub(crate) fn load_config(path: &Utf8Path) -> anyhow::Result<Config> {
16    eprintln!("loading `{}`...", path);
17    let mut config =
18        Config::load_from_path(path).with_context(|| format!("failed to load `{}`", path))?;
19
20    eprintln!("validating config...");
21    let errors = config.validate();
22    let mut error_count = 0;
23    for e in errors {
24        match e.severity() {
25            Severity::Warn => {
26                eprintln!("validation warning: {}", e.error());
27            }
28            Severity::Error => {
29                eprintln!("validation error: {}", e.error());
30                error_count += 1;
31            }
32        }
33    }
34
35    ensure!(
36        error_count == 0,
37        "validation failed with {error_count} errors."
38    );
39
40    Ok(config)
41}