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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::path::PathBuf;

use anyhow::Context;
use clap::Parser;

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Cli {
    #[cfg(any(feature = "json", feature = "yaml", feature = "toml"))]
    #[arg(value_enum, short, long)]
    format: Option<crate::formatted_output::Format>,
    #[arg(long)]
    only_value: bool,
    #[arg(short, long)]
    output: Option<PathBuf>,
    #[arg(value_enum)]
    temperature_in: crate::temperature::Temperature,
    #[arg(value_enum)]
    temperature_out: crate::temperature::Temperature,
    value: Option<f64>,
    #[arg(short, long)]
    verbose: bool,
}

impl Cli {
    #[cfg(any(feature = "json", feature = "yaml", feature = "toml"))]
    pub fn format(&self) -> Option<crate::formatted_output::Format> {
        self.format
    }

    #[cfg(not(any(feature = "json", feature = "yaml", feature = "toml")))]
    pub fn format(&self) -> Option<crate::formatted_output::Format> {
        None
    }

    pub fn only_value(&self) -> bool {
        self.only_value
    }

    pub fn output(&self) -> Option<PathBuf> {
        self.output.clone()
    }

    pub fn value(&self) -> f64 {
        self.value.expect("Value missing")
    }

    pub fn verbose(&self) -> bool {
        self.verbose
    }

    pub fn temperature_in(&self) -> crate::temperature::Temperature {
        self.temperature_in
    }

    pub fn temperature_out(&self) -> crate::temperature::Temperature {
        self.temperature_out
    }

    pub fn convert(&self) -> anyhow::Result<f64> {
        let temp = self
            .value
            .context("Failed to convert the temperature because there was no temperature given.")?;

        if self.temperature_in == self.temperature_out {
            return Ok(temp);
        };

        Ok(self.temperature_in.convert(self.temperature_out, temp))
    }
}