Skip to main content

sdkt_core/
format.rs

1use std::str::FromStr;
2
3/// Defines the output formatting style for CLI and RPC responses.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub enum OutputFormat {
6    /// Compact JSON format.
7    #[default]
8    Json,
9    /// Human-readable, pretty-printed JSON.
10    Pretty,
11}
12
13impl FromStr for OutputFormat {
14    type Err = String;
15
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        match s.to_lowercase().as_str() {
18            "json" => Ok(OutputFormat::Json),
19            "pretty" => Ok(OutputFormat::Pretty),
20            _ => Err(format!(
21                "Invalid format '{}'. Expected 'json' or 'pretty'.",
22                s
23            )),
24        }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_output_format_from_str() {
34        assert_eq!("json".parse::<OutputFormat>(), Ok(OutputFormat::Json));
35        assert_eq!("JSON".parse::<OutputFormat>(), Ok(OutputFormat::Json));
36        assert_eq!("pretty".parse::<OutputFormat>(), Ok(OutputFormat::Pretty));
37        assert!("invalid".parse::<OutputFormat>().is_err());
38    }
39}