temp_conv/
formatted_output.rs

1//! Data Structure for helping output the results of a conversion
2
3use anyhow::Result;
4use clap::ValueEnum;
5use serde::{Deserialize, Serialize};
6
7#[derive(Serialize, Deserialize, Debug, Copy, Clone)]
8pub struct Output {
9    temperature_in: crate::temperature::Temperature,
10    temperature_out: crate::temperature::Temperature,
11    value_in: f64,
12    value_out: f64,
13}
14
15impl Output {
16    /// Creates a new [`Output`].
17    #[inline]
18    pub const fn new(
19        temperature_in: crate::temperature::Temperature,
20        value_in: f64,
21        temperature_out: crate::temperature::Temperature,
22        value_out: f64,
23    ) -> Self {
24        Self {
25            temperature_in,
26            value_in,
27            temperature_out,
28            value_out,
29        }
30    }
31
32    #[cfg(feature = "json")]
33    #[inline]
34    pub fn to_json(self) -> Result<String> {
35        use anyhow::Context;
36
37        serde_json::to_string_pretty(&self).context("Failed to parse Output to Json-Format")
38    }
39
40    #[cfg(feature = "yaml")]
41    #[inline]
42    pub fn to_yaml(self) -> Result<String> {
43        use anyhow::Context;
44
45        serde_yaml::to_string(&self).context("Failed to parse Output to Yaml-Format")
46    }
47
48    #[cfg(feature = "toml")]
49    #[inline]
50    pub fn to_toml(self) -> Result<String> {
51        use anyhow::Context;
52
53        toml::to_string_pretty(&self).context("Failed to parse Output to Toml-Format")
54    }
55}
56
57#[derive(Debug, Serialize, Deserialize, Copy, Clone, ValueEnum)]
58pub enum Format {
59    #[cfg(feature = "json")]
60    Json,
61    #[cfg(feature = "yaml")]
62    Yaml,
63    #[cfg(feature = "toml")]
64    Toml,
65}