diff --git a/Cargo.lock b/Cargo.lock index 3a4d0fb6..a61001c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -865,6 +865,37 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "config" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68578f196d2a33ff61b27fae256c3164f65e36382648e30666dde05b8cc9dfdf" +dependencies = [ + "async-trait", + "convert_case", + "json5", + "nom", + "pathdiff", + "ron", + "rust-ini", + "serde", + "serde_json", + "toml", + "yaml-rust2", +] + +[[package]] +name = "confy" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45b1f4c00870f07dc34adcac82bb6a72cc5aabca8536ba1797e01df51d2ce9a0" +dependencies = [ + "directories", + "serde", + "thiserror 1.0.69", + "toml", +] + [[package]] name = "console" version = "0.15.10" @@ -2422,10 +2453,6 @@ checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" dependencies = [ "libc", "wasi", - "windows-sys 0.52.0", -] - -[[package]] name = "neuronek" version = "0.0.1-alpha.3" dependencies = [ diff --git a/src/cli/mod.rs b/src/cli/mod.rs index cb8e139b..da9e79ee 100755 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -7,6 +7,7 @@ use ingestion::IngestionCommand; use miette::IntoDiagnostic; use sea_orm::prelude::async_trait::async_trait; use substance::SubstanceCommand; +use std::path::PathBuf; use crate::core::CommandHandler; use crate::utils::AppContext; diff --git a/src/core/config.rs b/src/core/config.rs index 0de5b65d..f0e0cccb 100644 --- a/src/core/config.rs +++ b/src/core/config.rs @@ -1,42 +1,61 @@ -use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; +use std::env; use std::env::temp_dir; -use std::path::Path; use std::path::PathBuf; +use directories::ProjectDirs; +use lazy_static::lazy_static; +use crate::cli::Cli; pub const NAME: &str = env!("CARGO_PKG_NAME"); pub const VERSION: &str = env!("CARGO_PKG_VERSION"); -lazy_static::lazy_static! { - pub static ref CONFIG: Config = Config::default(); - /// Returns the path to the project's data directory. - pub static ref DATA_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).data_dir().into(); - /// Returns the path to the project's cache directory. - pub static ref CACHE_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).cache_dir().into(); - /// Returns the path to the project's config directory. - pub static ref CONFIG_DIR: Box = directories::ProjectDirs::from("com", "keinsell", NAME).unwrap_or_else(|| panic!("project data directory not found")).config_dir().into(); +lazy_static! { + pub static ref DATA_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project data directory not found") + .data_dir() + .to_path_buf() + }; + pub static ref CACHE_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project cache directory not found") + .cache_dir() + .to_path_buf() + }; + pub static ref CONFIG_DIR: PathBuf = { + ProjectDirs::from("com", "keinsell", NAME) + .expect("project config directory not found") + .config_dir() + .to_path_buf() + }; } - -// TODO(NEU-1): Implement -#[derive(Debug, Clone)] -pub struct Config -{ +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Config { pub sqlite_path: PathBuf, + pub version: Option, } -impl Default for Config -{ - fn default() -> Self - { - let mut journal_path = DATA_DIR.join("journal.db").clone(); - - if cfg!(test) || cfg!(debug_assertions) - { - journal_path = temp_dir().join("neuronek.sqlite"); - } - +impl Default for Config { + fn default() -> Self { + let default_db = if cfg!(test) || cfg!(debug_assertions) { + temp_dir().join("neuronek.sqlite") + } else { + DATA_DIR.join("journal.db") + }; Config { - sqlite_path: journal_path, + sqlite_path: default_db, + version: Some(1), } } } + +impl Config { + pub fn load() -> Self { + confy::load(NAME, None).unwrap_or_default() + } +} + +lazy_static! { + pub static ref CONFIG: Config = Config::load(); +} diff --git a/src/utils.rs b/src/utils.rs index 5d82b49b..d7e253c6 100755 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,5 @@ use crate::cli::OutputFormat; -use crate::core::config::CONFIG; +use crate::core::config::{Config, CONFIG}; use crate::database::Migrator; use async_std::task::block_on; use atty::Stream;