Skip to main content

hotpath_meta/
shared.rs

1use std::str::FromStr;
2
3/// Output format for profiling reports.
4///
5/// This enum specifies how profiling results should be displayed when the program exits.
6///
7/// # Variants
8///
9/// * `Table` - Human-readable table format (default)
10/// * `Json` - JSON format
11/// * `JsonPretty` - Pretty-printed JSON format
12/// * `None` - Suppress all profiling output (metrics server and MCP server still function)
13///
14/// # Parsing
15///
16/// Can be parsed from strings via `HOTPATH_META_OUTPUT_FORMAT` environment variable:
17/// - `"table"` → `Format::Table`
18/// - `"json"` → `Format::Json`
19/// - `"json-pretty"` → `Format::JsonPretty`
20/// - `"none"` → `Format::None`
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
22pub enum Format {
23    #[default]
24    Table,
25    Json,
26    JsonPretty,
27    None,
28}
29
30impl FromStr for Format {
31    type Err = String;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        match s.to_lowercase().as_str() {
35            "table" => Ok(Format::Table),
36            "json" => Ok(Format::Json),
37            "json-pretty" | "jsonpretty" => Ok(Format::JsonPretty),
38            "none" => Ok(Format::None),
39            _ => Err(format!(
40                "unknown format '{}', expected: table, json, json-pretty, none",
41                s
42            )),
43        }
44    }
45}
46
47impl Format {
48    /// Returns the format from `HOTPATH_META_OUTPUT_FORMAT` env var, or default if not set.
49    /// Panics if the env var contains an invalid value.
50    pub fn from_env() -> Self {
51        match std::env::var("HOTPATH_META_OUTPUT_FORMAT") {
52            Ok(v) => v
53                .parse()
54                .unwrap_or_else(|e| panic!("HOTPATH_META_OUTPUT_FORMAT: {}", e)),
55            Err(_) => Format::default(),
56        }
57    }
58}
59
60pub trait IntoF64 {
61    fn into_f64(self) -> f64;
62}
63
64impl IntoF64 for f64 {
65    fn into_f64(self) -> f64 {
66        self
67    }
68}
69
70impl IntoF64 for f32 {
71    fn into_f64(self) -> f64 {
72        self as f64
73    }
74}
75
76impl IntoF64 for i8 {
77    fn into_f64(self) -> f64 {
78        self as f64
79    }
80}
81
82impl IntoF64 for i16 {
83    fn into_f64(self) -> f64 {
84        self as f64
85    }
86}
87
88impl IntoF64 for i32 {
89    fn into_f64(self) -> f64 {
90        self as f64
91    }
92}
93
94impl IntoF64 for i64 {
95    fn into_f64(self) -> f64 {
96        self as f64
97    }
98}
99
100impl IntoF64 for u8 {
101    fn into_f64(self) -> f64 {
102        self as f64
103    }
104}
105
106impl IntoF64 for u16 {
107    fn into_f64(self) -> f64 {
108        self as f64
109    }
110}
111
112impl IntoF64 for u32 {
113    fn into_f64(self) -> f64 {
114        self as f64
115    }
116}
117
118impl IntoF64 for u64 {
119    fn into_f64(self) -> f64 {
120        self as f64
121    }
122}
123
124impl IntoF64 for isize {
125    fn into_f64(self) -> f64 {
126        self as f64
127    }
128}
129
130impl IntoF64 for usize {
131    fn into_f64(self) -> f64 {
132        self as f64
133    }
134}