Change config format

feature/lookup-installed
trivernis 2 years 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)]
#[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()

@ -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,

@ -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<Self> {
@ -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
}
}

@ -91,7 +91,7 @@ impl Repository {
/// Initializes a new repository with the given confi
pub async fn init(config: Config) -> Result<Self> {
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 {

Loading…
Cancel
Save