opentitanlib/app/config/
mod.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use anyhow::Result;
6use std::path::{Path, PathBuf};
7use thiserror::Error;
8
9use crate::app::TransportWrapperBuilder;
10use crate::util::fs::read_to_string;
11
12mod structs;
13pub use structs::*;
14
15#[derive(Error, Debug)]
16pub enum Error {
17    #[error("No such configuration: `{0}`")]
18    ConfigNotFound(PathBuf),
19    #[error("Loading configuration file `{0}`: {1}")]
20    ConfigReadError(PathBuf, anyhow::Error),
21    #[error("Parsing configuration file `{0}`: {1}")]
22    ConfigParseError(PathBuf, anyhow::Error),
23}
24
25pub fn process_config_file(env: &mut TransportWrapperBuilder, conf_file: &Path) -> Result<()> {
26    log::debug!("Reading config file {:?}", conf_file);
27    let conf_data = match read_to_string(conf_file) {
28        Ok(v) => v,
29        Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
30            Err(Error::ConfigNotFound(conf_file.to_owned()))?
31        }
32        Err(e) => Err(e)?,
33    };
34    let res: ConfigurationFile = serde_annotate::from_str(&conf_data)
35        .map_err(|e| Error::ConfigParseError(conf_file.to_path_buf(), e.into()))?;
36
37    let subdir = conf_file.parent().unwrap_or_else(|| Path::new(""));
38    for included_conf_file in &res.includes {
39        let path = subdir.join(included_conf_file);
40        process_config_file(env, &path)?
41    }
42    env.add_configuration_file(res)
43}