use std::collections::BTreeMap;
#[cfg(feature = "trust-manifest")]
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::{JaoResult, storage};
#[cfg(feature = "trust-manifest")]
const DEFAULT_TRUSTFILE_NAME: &str = "jaotrusted.toml";
const CONFIG_FILE_LOCATION: &str = "config.toml";
const CURRENT_CONFIG_VERSION: u32 = 1;
pub(crate) fn load_or_init() -> JaoResult<JaoConfig> {
let mut config_file = match storage::load_from_storage(CONFIG_FILE_LOCATION)? {
Some(config) => config,
None => {
let default_config = JaoConfigFile::default();
storage::write_to_storage(CONFIG_FILE_LOCATION, &default_config)?;
default_config
}
};
if config_file.version != CURRENT_CONFIG_VERSION {
config_file.version = CURRENT_CONFIG_VERSION;
storage::write_to_storage(CONFIG_FILE_LOCATION, &config_file)?;
}
Ok(JaoConfig::from(config_file))
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct JaoConfig {
#[cfg(feature = "trust-manifest")]
pub(crate) trustfile: PathBuf,
}
impl From<JaoConfigFile> for JaoConfig {
#[allow(unused_variables)]
fn from(config: JaoConfigFile) -> Self {
Self {
#[cfg(feature = "trust-manifest")]
trustfile: config.trustfile,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct JaoConfigFile {
#[serde(default = "default_config_version")]
pub(crate) version: u32,
#[cfg(feature = "trust-manifest")]
#[serde(default = "default_trustfile")]
pub(crate) trustfile: PathBuf,
#[serde(flatten)]
pub(crate) extra: BTreeMap<String, toml::Value>,
}
impl Default for JaoConfigFile {
fn default() -> Self {
Self {
version: CURRENT_CONFIG_VERSION,
#[cfg(feature = "trust-manifest")]
trustfile: default_trustfile(),
extra: BTreeMap::new(),
}
}
}
#[cfg(feature = "trust-manifest")]
fn default_trustfile() -> PathBuf {
PathBuf::from(DEFAULT_TRUSTFILE_NAME)
}
fn default_config_version() -> u32 {
CURRENT_CONFIG_VERSION
}