1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
mod args;
mod character;
mod character_display;
mod clock;
mod color;
mod config;
mod position;
mod segment;
mod state;

use std::time::Duration;

use clap::Parser;

use crate::{
    args::{Args, Mode},
    clock::{clock_mode::ClockMode, counter::Counter, time::Time},
    config::Config,
    state::State,
};

pub fn run() -> Result<(), String> {
    let args = Args::parse();
    let mut config = Config::parse()?;

    let mode = args.mode.clone();
    args.overwrite(&mut config);

    let clock_mode = if let Some(mode) = mode {
        match mode {
            Mode::Clock => ClockMode::CurrentTime(Time::from_utc(config.date.utc)),
            Mode::Timer(args) => {
                if args.secs >= 360000 {
                    return Err(
                        "the timer duration cannot exceed 359,999 seconds (or 1 day - 1 second)"
                            .into(),
                    );
                }
                ClockMode::Counter(Counter::new(Some(Duration::from_secs(args.secs))))
            }
            Mode::Stopwatch => ClockMode::Counter(Counter::new(None)),
        }
    } else {
        ClockMode::CurrentTime(match config.date.utc {
            true => Time::Utc,
            false => Time::Local,
        })
    };

    State::new(config, clock_mode)
        .map_err(|err| err.to_string())?
        .run()
        .map_err(|err| err.to_string())
}