From 835a366caa7dc3e4a2fbdcaffbd391b5611aa3a6 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 22 Jan 2023 13:27:43 +0100 Subject: [PATCH] Change config format --- src/error.rs | 6 +++--- src/mapper/mod.rs | 2 +- src/repository/config.rs | 36 +++++++++++++++++++++++++++++------- src/repository/mod.rs | 2 +- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/error.rs b/src/error.rs index 19fa1da..704c7d7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -79,8 +79,8 @@ pub struct SerializeJsonError { #[derive(Debug, Error, Diagnostic)] #[diagnostic(code(nenv::toml::deserialize))] -#[error("Failed to parse the toml file.")] -pub struct ParseTomlError { +#[error("The config file could not parsed.")] +pub struct ParseConfigError { #[source_code] src: NamedSource, @@ -91,7 +91,7 @@ pub struct ParseTomlError { caused_by: toml::de::Error, } -impl ParseTomlError { +impl ParseConfigError { pub fn new(file_name: &str, src: String, caused_by: toml::de::Error) -> Self { let abs_pos = caused_by .line_col() diff --git a/src/mapper/mod.rs b/src/mapper/mod.rs index e74d4f9..89040a3 100644 --- a/src/mapper/mod.rs +++ b/src/mapper/mod.rs @@ -26,7 +26,7 @@ impl Mapper { pub async fn load(repository: Repository) -> Self { let version = Self::get_version() .await - .unwrap_or_else(|| repository.config.default_version.to_owned()); + .unwrap_or_else(|| repository.config.node.default_version.to_owned()); Self { repo: repository, active_version: version, diff --git a/src/repository/config.rs b/src/repository/config.rs index b39e668..e638984 100644 --- a/src/repository/config.rs +++ b/src/repository/config.rs @@ -6,27 +6,49 @@ use tokio::fs; use crate::error::SerializeTomlError; use crate::{ consts::{CFG_DIR, CFG_FILE_PATH, NODE_DIST_URL}, - error::ParseTomlError, + error::ParseConfigError, }; use super::NodeVersion; -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Default, Serialize, Deserialize, Clone, Debug)] pub struct Config { - pub dist_base_url: String, + /// Node execution related config + pub node: NodeConfig, + + /// Configuration for how to download node versions + pub download: DownloadConfig, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct NodeConfig { + /// The default version if no version is specified + /// in the `package.json` file or `NODE_VERSION` environment variable #[serde(with = "NodeVersion")] pub default_version: NodeVersion, } -impl Default for Config { +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct DownloadConfig { + pub dist_base_url: String, +} + +impl Default for NodeConfig { fn default() -> Self { Self { - dist_base_url: String::from(NODE_DIST_URL), default_version: NodeVersion::LatestLts, } } } +impl Default for DownloadConfig { + fn default() -> Self { + Self { + dist_base_url: String::from(NODE_DIST_URL), + } + } +} + impl Config { /// Loads the config file from the default config path pub async fn load() -> Result { @@ -48,7 +70,7 @@ impl Config { .context("reading config file")?; let cfg = toml::from_str(&cfg_string) - .map_err(|e| ParseTomlError::new("config.toml", cfg_string, e))?; + .map_err(|e| ParseConfigError::new("config.toml", cfg_string, e))?; Ok(cfg) } @@ -67,7 +89,7 @@ impl Config { } pub async fn set_default_version(&mut self, default_version: NodeVersion) -> Result<()> { - self.default_version = default_version; + self.node.default_version = default_version; self.save().await } } diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 909aff2..e20e356 100644 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -91,7 +91,7 @@ impl Repository { /// Initializes a new repository with the given confi pub async fn init(config: Config) -> Result { Self::create_folders().await?; - let web_api = WebApi::new(&config.dist_base_url); + let web_api = WebApi::new(&config.download.dist_base_url); let versions = load_versions(&web_api).await?; Ok(Self {