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
use anyhow::Result;
use clap::ValueEnum;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
pub struct Output {
    temperature_in: crate::temperature::Temperature,
    temperature_out: crate::temperature::Temperature,
    value_in: f64,
    value_out: f64,
}

impl Output {
    /// Creates a new [`Output`].
    pub fn new(
        temperature_in: crate::temperature::Temperature,
        value_in: f64,
        temperature_out: crate::temperature::Temperature,
        value_out: f64,
    ) -> Self {
        Self {
            temperature_in,
            value_in,
            temperature_out,
            value_out,
        }
    }

    #[cfg(feature = "json")]
    pub fn to_json(self) -> Result<String> {
        use anyhow::Context;

        serde_json::to_string_pretty(&self).context("Failed to parse Output to Json-Format")
    }

    #[cfg(feature = "yaml")]
    pub fn to_yaml(self) -> Result<String> {
        use anyhow::Context;

        serde_yaml::to_string(&self).context("Failed to parse Output to Yaml-Format")
    }

    #[cfg(feature = "toml")]
    pub fn to_toml(self) -> Result<String> {
        use anyhow::Context;

        toml::to_string_pretty(&self).context("Failed to parse Output to Toml-Format")
    }
}

#[derive(Debug, Serialize, Deserialize, Copy, Clone, ValueEnum)]
pub enum Format {
    #[cfg(feature = "json")]
    Json,
    #[cfg(feature = "yaml")]
    Yaml,
    #[cfg(feature = "toml")]
    Toml,
}