signstar_config/
config.rs

1//! Configuration file handling for a NetHSM backend.
2
3use nethsm_config::{ConfigSettings, HermeticParallelConfig};
4use signstar_common::config::{get_config_file, get_config_file_paths};
5
6/// The error that may occur when handling a configuration file for a NetHSM backend.
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9    /// The Signstar configuration is missing.
10    #[error("No configuration file found in {}.", get_config_file_paths().iter().map(|path| path.display().to_string()).collect::<Vec<String>>().join(", "))]
11    ConfigMissing,
12
13    /// An error specific to NetHsm config handling.
14    #[error("NetHSM config error:\n{0}")]
15    NetHsmConfig(#[from] nethsm_config::Error),
16}
17
18/// Loads a [`HermeticParallelConfig`].
19///
20/// Gets a configuration file from the default locations using [`get_config_file`] and returns it as
21/// [`HermeticParallelConfig`].
22///
23/// # Errors
24///
25/// Returns an error if no config file is found or if the [`HermeticParallelConfig`] can not be
26/// created.
27pub fn load_config() -> Result<HermeticParallelConfig, crate::Error> {
28    let Some(config_path) = get_config_file() else {
29        return Err(crate::Error::Config(Error::ConfigMissing));
30    };
31
32    HermeticParallelConfig::new_from_file(
33        ConfigSettings::new(
34            "signstar".to_string(),
35            nethsm_config::ConfigInteractivity::NonInteractive,
36            None,
37        ),
38        Some(&config_path),
39    )
40    .map_err(|source| crate::Error::Config(Error::NetHsmConfig(source)))
41}