diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b81f7a..f2b12cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ Procedure when bumping the version number: ## Unreleased +### Added +- Config file + ## v0.3.0 - 2022-08-22 ### Added diff --git a/Cargo.lock b/Cargo.lock index 4a7be31..33ea96f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,11 +187,13 @@ dependencies = [ "log", "parking_lot", "rusqlite", + "serde", "serde_json", "thiserror", "time", "tokio", "tokio-tungstenite", + "toml", "toss", "unicode-segmentation", "unicode-width", @@ -1173,6 +1175,15 @@ dependencies = [ "webpki", ] +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + [[package]] name = "toss" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index aebfd23..4347a44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,11 @@ edit = "0.1.4" log = { version = "0.4.17", features = ["std"] } parking_lot = "0.12.1" rusqlite = { version = "0.28.0", features = ["bundled", "time"] } +serde = { version = "1.0.144", features = ["derive"] } serde_json = "1.0.85" thiserror = "1.0.32" tokio = { version = "1.20.1", features = ["full"] } +toml = "0.5.9" unicode-segmentation = "1.9.0" unicode-width = "0.1.9" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..c973a3c --- /dev/null +++ b/src/config.rs @@ -0,0 +1,16 @@ +use std::fs; +use std::path::Path; + +use serde::Deserialize; + +use crate::macros::ok_or_return; + +#[derive(Debug, Default, Deserialize)] +pub struct Config {} + +impl Config { + pub fn load(path: &Path) -> Self { + let content = ok_or_return!(fs::read_to_string(path), Self::default()); + ok_or_return!(toml::from_str(&content), Self::default()) + } +} diff --git a/src/main.rs b/src/main.rs index 0d8115a..2f341dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ // TODO Enable warn(unreachable_pub)? // TODO Clean up use and manipulation of toss Pos and Size +mod config; mod euph; mod export; mod logger; @@ -35,6 +36,7 @@ use toss::terminal::Terminal; use ui::Ui; use vault::Vault; +use crate::config::Config; use crate::logger::Logger; #[derive(Debug, clap::Subcommand)] @@ -58,6 +60,10 @@ impl Default for Command { #[derive(Debug, clap::Parser)] #[clap(version)] struct Args { + /// Path to the config file. + #[clap(long, short)] + config: Option, + /// Path to a directory for cove to store its data in. #[clap(long, short)] data_dir: Option, @@ -75,25 +81,24 @@ struct Args { command: Option, } -fn data_dir(args_data_dir: Option) -> PathBuf { - if let Some(data_dir) = args_data_dir { - data_dir - } else { - let dirs = - ProjectDirs::from("de", "plugh", "cove").expect("unable to determine directories"); - dirs.data_dir().to_path_buf() - } -} - #[tokio::main] async fn main() -> anyhow::Result<()> { let args = Args::parse(); + let dirs = ProjectDirs::from("de", "plugh", "cove").expect("unable to determine directories"); + + let config_path = args + .config + .unwrap_or_else(|| dirs.config_dir().join("config.toml")); + println!("Config file: {}", config_path.to_string_lossy()); + let config = Config::load(&config_path); let vault = if args.ephemeral { vault::launch_in_memory()? } else { - let data_dir = data_dir(args.data_dir); - println!("Data dir: {}", data_dir.to_string_lossy()); + let data_dir = args + .data_dir + .unwrap_or_else(|| dirs.data_dir().to_path_buf()); + println!("Data dir: {}", data_dir.to_string_lossy()); vault::launch(&data_dir.join("vault.db"))? };