diff options
| author | lukeflo | 2025-02-17 19:50:55 +0100 |
|---|---|---|
| committer | lukeflo | 2025-02-17 19:50:55 +0100 |
| commit | 059591a1be6b887eaca9b114fdb5b350a65bae43 (patch) | |
| tree | 40bbb1016606efa3111ea0dec051a2ef2c128ad4 | |
| parent | dd8dd9611771491e723a49b41cf27b1e9090664d (diff) | |
| download | bibiman-059591a1be6b887eaca9b114fdb5b350a65bae43.tar.gz bibiman-059591a1be6b887eaca9b114fdb5b350a65bae43.zip | |
implement basic config struct and example file
| -rw-r--r-- | Cargo.lock | 106 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | files/bibiman.toml | 3 | ||||
| -rw-r--r-- | src/cliargs.rs | 12 | ||||
| -rw-r--r-- | src/config.rs | 46 | ||||
| -rw-r--r-- | src/main.rs | 6 |
6 files changed, 168 insertions, 7 deletions
@@ -58,6 +58,17 @@ dependencies = [ ] [[package]] +name = "async-trait" +version = "0.1.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "644dd749086bf3771a2fbc5f256fdb982d53f011c7d5d560304eafeecebce79d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -91,6 +102,7 @@ dependencies = [ "arboard", "biblatex", "color-eyre", + "config", "crossterm", "dirs", "editor-command", @@ -101,6 +113,7 @@ dependencies = [ "rand", "ratatui", "regex", + "serde", "signal-hook", "tokio", "tokio-util", @@ -254,6 +267,29 @@ dependencies = [ ] [[package]] +name = "config" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf9dc8d4ef88e27a8cb23e85cb116403dedd57f7971964dc4b18ccead548901" +dependencies = [ + "async-trait", + "convert_case", + "pathdiff", + "serde", + "toml", + "winnow", +] + +[[package]] +name = "convert_case" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca" +dependencies = [ + "unicode-segmentation", +] + +[[package]] name = "core-foundation" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1197,6 +1233,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" [[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + +[[package]] name = "percent-encoding" version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1480,18 +1522,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.210" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.217" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" dependencies = [ "proc-macro2", "quote", @@ -1499,6 +1541,15 @@ dependencies = [ ] [[package]] +name = "serde_spanned" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" +dependencies = [ + "serde", +] + +[[package]] name = "sharded-slab" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1628,9 +1679,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.79" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -1770,6 +1821,40 @@ dependencies = [ ] [[package]] +name = "toml" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd87a5cdd6ffab733b2f74bc4fd7ee5fff6634124999ac278c35fc78c6120148" +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.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow", +] + +[[package]] name = "tracing" version = "0.1.40" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2219,6 +2304,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] +name = "winnow" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" +dependencies = [ + "memchr", +] + +[[package]] name = "wl-clipboard-rs" version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -35,3 +35,5 @@ tui-input = "0.11.0" walkdir = "2.5.0" regex = "1.11.1" ureq = "2.12.1" +config = { version = "0.15.8", default-features = false, features = ["async", "async-trait", "convert-case", "convert_case", "toml"] } +serde = { version = "1.0.217", features = ["serde_derive"] } diff --git a/files/bibiman.toml b/files/bibiman.toml new file mode 100644 index 0000000..e24b597 --- /dev/null +++ b/files/bibiman.toml @@ -0,0 +1,3 @@ +[general] +bibfiles = ["../tests/biblatex-test.bib"] +editor = "vi" diff --git a/src/cliargs.rs b/src/cliargs.rs index 71ba38c..ecec93e 100644 --- a/src/cliargs.rs +++ b/src/cliargs.rs @@ -17,6 +17,7 @@ use color_eyre::eyre::Result; use color_eyre::owo_colors::OwoColorize; +use dirs::{config_dir, home_dir}; use lexopt::prelude::*; use std::env; use std::path::PathBuf; @@ -31,6 +32,7 @@ pub struct CLIArgs { pub versionarg: bool, pub pos_args: Vec<PathBuf>, pub files: Vec<PathBuf>, + pub cfg_path: PathBuf, // INFO: AppColors struct later should be moved to config/app struct // when config file is implemented pub colors: AppColors, @@ -41,10 +43,18 @@ impl CLIArgs { let mut args = CLIArgs::default(); let mut parser = lexopt::Parser::from_env(); + // Default config + args.cfg_path = if config_dir().is_some() { + config_dir().unwrap().join("bibiman/bibiman.toml") + } else { + home_dir().unwrap().join(".config/bibiman/bibiman.toml") + }; + while let Some(arg) = parser.next()? { match arg { Short('h') | Long("help") => args.helparg = true, Short('v') | Long("version") => args.versionarg = true, + Short('c') | Long("config-file") => args.cfg_path = parser.value()?.parse()?, Long("light-terminal") => { args.colors.light_colors(); args.colors.toggle_color_scheme() @@ -114,6 +124,8 @@ POSITIONAL ARGS: FLAGS: -h, --help Show this help and exit -v, --version Show the version and exit + -c, --config-file Path to config file used for current session. + Takes precedence over standard config file. --light-terminal Enable color mode for light terminal background", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..2ef296a --- /dev/null +++ b/src/config.rs @@ -0,0 +1,46 @@ +// bibiman - a TUI for managing BibLaTeX databases +// Copyright (C) 2024 lukeflo +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <https://www.gnu.org/licenses/>. +///// + +use std::path::PathBuf; + +use config::{ConfigError, FileFormat}; +use serde::Deserialize; + +use crate::cliargs::CLIArgs; + +#[derive(Debug, Clone, Deserialize)] +pub struct BibiConfig { + pub general: General, +} + +#[derive(Debug, Clone, Deserialize)] +pub struct General { + pub bibfiles: Vec<PathBuf>, + pub editor: String, +} + +impl BibiConfig { + pub fn new(args: &mut CLIArgs) -> Result<Self, ConfigError> { + let mut cfg = config::Config::builder(); + cfg = cfg.add_source( + config::File::from(args.cfg_path.clone()) + .format(FileFormat::Toml) + .required(true), + ); + cfg.build()?.try_deserialize() + } +} diff --git a/src/main.rs b/src/main.rs index 78c5075..a0f69d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,18 +18,20 @@ use app::App; use cliargs::CLIArgs; use color_eyre::eyre::Result; +use config::BibiConfig; use errorsetup::init_error_hooks; pub mod app; pub mod bibiman; pub mod cliargs; +pub mod config; pub mod errorsetup; pub mod tui; #[tokio::main] async fn main() -> Result<()> { // Parse CLI arguments - let mut parsed_args = CLIArgs::parse_args().unwrap(); + let mut parsed_args = CLIArgs::parse_args()?; // Print help if -h/--help flag is passed and exit if parsed_args.helparg { @@ -43,6 +45,8 @@ async fn main() -> Result<()> { std::process::exit(0); } + let cfg = BibiConfig::new(&mut parsed_args)?; + init_error_hooks()?; // Create an application. |
