Change config format

feature/lookup-installed
trivernis 1 year ago
parent 3c09130d91
commit 835a366caa
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG Key ID: DFFFCC2C7A02DB45

@ -79,8 +79,8 @@ pub struct SerializeJsonError {
#[derive(Debug, Error, Diagnostic)] #[derive(Debug, Error, Diagnostic)]
#[diagnostic(code(nenv::toml::deserialize))] #[diagnostic(code(nenv::toml::deserialize))]
#[error("Failed to parse the toml file.")] #[error("The config file could not parsed.")]
pub struct ParseTomlError { pub struct ParseConfigError {
#[source_code] #[source_code]
src: NamedSource, src: NamedSource,
@ -91,7 +91,7 @@ pub struct ParseTomlError {
caused_by: toml::de::Error, caused_by: toml::de::Error,
} }
impl ParseTomlError { impl ParseConfigError {
pub fn new(file_name: &str, src: String, caused_by: toml::de::Error) -> Self { pub fn new(file_name: &str, src: String, caused_by: toml::de::Error) -> Self {
let abs_pos = caused_by let abs_pos = caused_by
.line_col() .line_col()

@ -26,7 +26,7 @@ impl Mapper {
pub async fn load(repository: Repository) -> Self { pub async fn load(repository: Repository) -> Self {
let version = Self::get_version() let version = Self::get_version()
.await .await
.unwrap_or_else(|| repository.config.default_version.to_owned()); .unwrap_or_else(|| repository.config.node.default_version.to_owned());
Self { Self {
repo: repository, repo: repository,
active_version: version, active_version: version,

@ -6,27 +6,49 @@ use tokio::fs;
use crate::error::SerializeTomlError; use crate::error::SerializeTomlError;
use crate::{ use crate::{
consts::{CFG_DIR, CFG_FILE_PATH, NODE_DIST_URL}, consts::{CFG_DIR, CFG_FILE_PATH, NODE_DIST_URL},
error::ParseTomlError, error::ParseConfigError,
}; };
use super::NodeVersion; use super::NodeVersion;
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Default, Serialize, Deserialize, Clone, Debug)]
pub struct Config { 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")] #[serde(with = "NodeVersion")]
pub default_version: 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 { fn default() -> Self {
Self { Self {
dist_base_url: String::from(NODE_DIST_URL),
default_version: NodeVersion::LatestLts, default_version: NodeVersion::LatestLts,
} }
} }
} }
impl Default for DownloadConfig {
fn default() -> Self {
Self {
dist_base_url: String::from(NODE_DIST_URL),
}
}
}
impl Config { impl Config {
/// Loads the config file from the default config path /// Loads the config file from the default config path
pub async fn load() -> Result<Self> { pub async fn load() -> Result<Self> {
@ -48,7 +70,7 @@ impl Config {
.context("reading config file")?; .context("reading config file")?;
let cfg = toml::from_str(&cfg_string) 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) Ok(cfg)
} }
@ -67,7 +89,7 @@ impl Config {
} }
pub async fn set_default_version(&mut self, default_version: NodeVersion) -> Result<()> { 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 self.save().await
} }
} }

@ -91,7 +91,7 @@ impl Repository {
/// Initializes a new repository with the given confi /// Initializes a new repository with the given confi
pub async fn init(config: Config) -> Result<Self> { pub async fn init(config: Config) -> Result<Self> {
Self::create_folders().await?; 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?; let versions = load_versions(&web_api).await?;
Ok(Self { Ok(Self {

Loading…
Cancel
Save