chest_rs/
lib.rs

1mod command;
2mod dirs;
3
4use std::fs::File;
5
6use anyhow::{Context, Result};
7use clap::Parser;
8use command::ChestCommand;
9#[cfg(not(debug_assertions))]
10use dirs::data_dir;
11use dirs::log_path;
12use log::LevelFilter;
13use simplelog::{Config, WriteLogger};
14
15const VERSION: &str = env!("CARGO_PKG_VERSION");
16
17#[derive(Parser)]
18#[command(author = "Wayan-Gwie Lapointe", version = VERSION)]
19pub struct Chest {
20    #[command(subcommand)]
21    chest: ChestCommand,
22}
23
24impl Chest {
25    pub fn run(self) -> Result<()> {
26        init()?;
27        self.chest.run()
28    }
29}
30
31fn init() -> Result<()> {
32    // Initialize data directory if it's missing
33    #[cfg(not(debug_assertions))]
34    std::fs::create_dir_all(data_dir()).context("Unable to create data directory")?;
35
36    let filter = if cfg!(debug_assertions) {
37        LevelFilter::Debug
38    } else {
39        LevelFilter::Warn
40    };
41
42    WriteLogger::init(
43        filter,
44        Config::default(),
45        File::options()
46            .create(true)
47            .append(true)
48            .open(log_path())
49            .context("Unable to open log file")?,
50    )
51    .context("Unable to start logger")?;
52
53    Ok(())
54}
55
56#[test]
57fn verify_chest_cli() {
58    use clap::CommandFactory;
59    Chest::command().debug_assert()
60}