use anyhow::{anyhow, Context, Error, Result};
use clap::ValueEnum;
use serde::Serialize;
use std::str::FromStr;
#[derive(Clone, Copy, ValueEnum)]
#[value(rename_all = "UPPERCASE")]
pub enum Format {
Ron,
Json,
Yaml,
}
impl Format {
pub fn as_string<T: Serialize>(self, data: &T) -> Result<String> {
match self {
Format::Ron => ron::ser::to_string_pretty(
data,
ron::ser::PrettyConfig::new().separate_tuple_members(true),
)
.context("failed to serialize to RON format"),
Format::Json => {
serde_json::to_string_pretty(data).context("failed to serialize to JSON format")
}
Format::Yaml => serde_yaml::to_string(data).context("failed to serialize to YAML"),
}
}
}
impl FromStr for Format {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_uppercase().as_str() {
"RON" => Ok(Format::Ron),
"JSON" => Ok(Format::Json),
"YAML" => Ok(Format::Yaml),
_ => Err(anyhow!("unsupported output format '{}'", s)),
}
}
}