Skip to main content

changepacks_cli/options/
format_options.rs

1use clap::ValueEnum;
2
3/// CLI output format selection.
4///
5/// Controls whether commands print human-readable output or JSON for CI integration.
6#[derive(Debug, Clone, ValueEnum)]
7pub enum FormatOptions {
8    /// JSON format for CI/CD pipelines
9    #[value(name = "json")]
10    Json,
11    /// Human-readable colored terminal output
12    #[value(name = "stdout")]
13    Stdout,
14}
15
16impl FormatOptions {
17    pub fn print(&self, stdout_msg: &str, json_msg: &str) {
18        match self {
19            Self::Stdout => println!("{stdout_msg}"),
20            Self::Json => println!("{json_msg}"),
21        }
22    }
23}
24
25#[cfg(test)]
26mod tests {
27    use super::*;
28    use clap::ValueEnum;
29
30    #[test]
31    fn test_format_options_value_enum_json() {
32        let format = FormatOptions::from_str("json", true).unwrap();
33        assert!(matches!(format, FormatOptions::Json));
34    }
35
36    #[test]
37    fn test_format_options_value_enum_stdout() {
38        let format = FormatOptions::from_str("stdout", true).unwrap();
39        assert!(matches!(format, FormatOptions::Stdout));
40    }
41
42    #[test]
43    fn test_format_options_debug() {
44        assert_eq!(format!("{:?}", FormatOptions::Json), "Json");
45        assert_eq!(format!("{:?}", FormatOptions::Stdout), "Stdout");
46    }
47
48    #[test]
49    fn test_format_options_clone() {
50        let json = FormatOptions::Json;
51        let stdout = FormatOptions::Stdout;
52
53        assert!(matches!(json.clone(), FormatOptions::Json));
54        assert!(matches!(stdout.clone(), FormatOptions::Stdout));
55    }
56}