temp_conv/
cli.rs

1//! Struct and helper function for the cli tool
2
3use std::path::PathBuf;
4
5use anyhow::Context;
6use clap::Parser;
7
8#[derive(Parser, Debug)]
9#[command(version, about, long_about = None)]
10pub struct Cli {
11    #[cfg(any(feature = "json", feature = "yaml", feature = "toml"))]
12    #[arg(value_enum, short, long)]
13    format: Option<crate::formatted_output::Format>,
14    #[arg(long)]
15    only_value: bool,
16    #[arg(short, long)]
17    output: Option<PathBuf>,
18    #[arg(value_enum)]
19    temperature_in: crate::temperature::Temperature,
20    #[arg(value_enum)]
21    temperature_out: crate::temperature::Temperature,
22    value: Option<f64>,
23    #[arg(short, long)]
24    verbose: bool,
25}
26
27impl Cli {
28    /// Returns the format of this [`Cli`].
29    ///
30    /// If none of the features json, yaml or toml is set this function will always return None.
31    #[cfg(any(feature = "json", feature = "yaml", feature = "toml"))]
32    #[inline]
33    pub const fn format(&self) -> Option<crate::formatted_output::Format> {
34        self.format
35    }
36
37    /// Returns the format of this [`Cli`].
38    ///
39    /// If none of the features json, yaml or toml is set this function will always return None.
40    #[cfg(not(any(feature = "json", feature = "yaml", feature = "toml")))]
41    #[inline]
42    pub const fn format(&self) -> Option<crate::formatted_output::Format> {
43        None
44    }
45
46    /// Returns the only value of this [`Cli`].
47    #[inline]
48    pub const fn only_value(&self) -> bool {
49        self.only_value
50    }
51
52    /// Returns the output of this [`Cli`].
53    #[inline]
54    pub fn output(&self) -> Option<PathBuf> {
55        self.output.clone()
56    }
57
58    /// Returns the value of this [`Cli`].
59    ///
60    /// # Panics
61    ///
62    /// Panics if value is none.
63    #[inline]
64    pub fn value(&self) -> f64 {
65        self.value.expect("Value missing")
66    }
67
68    /// Returns the verbose of this [`Cli`].
69    #[inline]
70    pub const fn verbose(&self) -> bool {
71        self.verbose
72    }
73
74    /// Returns the temperature in of this [`Cli`].
75    #[inline]
76    pub const fn temperature_in(&self) -> crate::temperature::Temperature {
77        self.temperature_in
78    }
79
80    /// Returns the temperature out of this [`Cli`].
81    #[inline]
82    pub const fn temperature_out(&self) -> crate::temperature::Temperature {
83        self.temperature_out
84    }
85
86    /// Returns the convert of this [`Cli`].
87    ///
88    /// # Errors
89    ///
90    /// This function will return an error if the field value of this struct is none.
91    #[inline]
92    pub fn convert(&self) -> anyhow::Result<f64> {
93        let temp = self
94            .value
95            .context("Failed to convert the temperature because there was no temperature given.")?;
96
97        if self.temperature_in == self.temperature_out {
98            return Ok(temp);
99        };
100
101        Ok(self.temperature_in.convert(self.temperature_out, temp))
102    }
103}