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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use std::io::Write; use std::str::FromStr; use crate::output::console::ConsoleOutput; use crate::output::json::JsonOutput; use crate::output::yaml::YamlOutput; use anyhow::Result; use serde::{Deserialize, Serialize}; pub mod console; pub mod json; pub mod yaml; #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub enum OutputFormat { Console, Yaml, Json, } pub trait OutputTrait { fn display<'a, T: Deserialize<'a> + Serialize>( &self, writer: impl Write, obj: &T, include_keys: Option<Vec<&str>>, exclude_keys: Option<Vec<&str>>, ) -> Result<()>; } impl FromStr for OutputFormat { type Err = String; fn from_str(s: &str) -> Result<Self, Self::Err> { match s.to_lowercase().as_str() { "console" => Ok(OutputFormat::Console), "json" => Ok(OutputFormat::Json), "yaml" => Ok(OutputFormat::Yaml), _ => Err(format!("{} not supported format", s)), } } } pub struct OutputFactory { pub output: OutputFormat, } impl OutputFactory { pub fn new(output: OutputFormat) -> Self { Self { output } } } impl OutputTrait for OutputFactory { fn display<'a, T: Deserialize<'a> + Serialize>( &self, writer: impl Write, obj: &T, include_keys: Option<Vec<&str>>, exclude_keys: Option<Vec<&str>>, ) -> Result<()> { match self.output { OutputFormat::Console => { ConsoleOutput::new().display(writer, obj, include_keys, exclude_keys) } OutputFormat::Yaml => { YamlOutput::new().display(writer, obj, include_keys, exclude_keys) } OutputFormat::Json => { JsonOutput::new().display(writer, obj, include_keys, exclude_keys) } } } }