bunsen_tool/support/
output_format.rs1use bunsen::errors::{
4 BunsenError,
5 BunsenResult,
6};
7use serde::Serialize;
8
9#[derive(clap::ValueEnum, Clone, Copy, Debug, PartialEq, Eq)]
11pub enum Format {
12 #[value(alias = "yaml")]
14 Yml,
15
16 Json,
18}
19
20impl Format {
21 pub fn render<T: Serialize>(
28 self,
29 value: &T,
30 what: &str,
31 ) -> BunsenResult<String> {
32 match self {
33 Format::Yml => serde_saphyr::to_string(value)
34 .map_err(|e| BunsenError::internal(format!("render {what} as YAML: {e}"))),
35 Format::Json => serde_json::to_string(value)
36 .map(|json| json + "\n")
37 .map_err(|e| BunsenError::internal(format!("render {what} as JSON: {e}"))),
38 }
39 }
40}