diff --git a/.gitignore b/.gitignore index 2f7896d..401267f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ target/ +subtails.toml diff --git a/Cargo.lock b/Cargo.lock index d73e400..6512393 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1161,6 +1161,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -1287,6 +1296,8 @@ dependencies = [ "crossterm", "ratatui", "reqwest", + "serde", + "toml", ] [[package]] @@ -1419,6 +1430,40 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" +dependencies = [ + "serde", +] + +[[package]] +name = "toml_edit" +version = "0.22.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + [[package]] name = "tower-service" version = "0.3.3" @@ -1746,6 +1791,15 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.6.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" +dependencies = [ + "memchr", +] + [[package]] name = "write16" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index da77b96..035daa2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" crossterm = "0.28.1" ratatui = "0.29.0" reqwest = "0.12.9" +serde = { version = "1.0.215", features = ["derive"] } +toml = "0.8.19" diff --git a/src/config/errors.rs b/src/config/errors.rs new file mode 100644 index 0000000..c2f1f59 --- /dev/null +++ b/src/config/errors.rs @@ -0,0 +1,21 @@ +use std::fmt::Display; + +#[derive(Debug)] +pub enum ConfigError { + ConfigNotFound, + MissingConfigValue(&'static str), +} + +impl std::error::Error for ConfigError {} + +impl Display for ConfigError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::ConfigNotFound => write!( + f, + "Config File was not found in either the current directory or ~/.config." + ), + Self::MissingConfigValue(var_name) => write!(f, "Neither the Env variable \"{}\" was set, nor the equivalent field in the config file was found.", var_name) + } + } +} diff --git a/src/config/file.rs b/src/config/file.rs new file mode 100644 index 0000000..54fdf9e --- /dev/null +++ b/src/config/file.rs @@ -0,0 +1,35 @@ +use std::fs::{exists, read_to_string}; + +use toml::{de::from_str, Table}; + +use crate::{config::errors::ConfigError, utils::Error}; + +use super::{validate::validate_config, Settings}; + +const CONFIG_PATHS: [&str; 2] = ["./", "~/.config/"]; +const CONFIG_FILENAME: &str = "subtails.toml"; + +fn read_config() -> Result