1use std::{path::PathBuf, str::FromStr};23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::{5 error::Result,6 manifest::{JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat},7 State,8};9use jrsonnet_stdlib::YamlFormat;1011use crate::ConfigureState;1213#[derive(Clone, ValueEnum)]14pub enum ManifestFormatName {15 16 String,17 Json,18 Yaml,19}2021impl FromStr for ManifestFormatName {22 type Err = &'static str;23 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {24 Ok(match s {25 "string" => ManifestFormatName::String,26 "json" => ManifestFormatName::Json,27 "yaml" => ManifestFormatName::Yaml,28 _ => return Err("no such format"),29 })30 }31}3233#[derive(Parser)]34#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]35pub struct ManifestOpts {36 37 #[clap(long, short = 'f', default_value = "json")]38 format: ManifestFormatName,39 40 41 #[clap(long, short = 'S', conflicts_with = "format")]42 string: bool,43 44 #[clap(long, short = 'y', conflicts_with = "string")]45 yaml_stream: bool,46 47 48 #[clap(long)]49 line_padding: Option<usize>,50 51 #[cfg(feature = "exp-preserve-order")]52 #[clap(long)]53 pub preserve_order: bool,54}55impl ConfigureState for ManifestOpts {56 type Guards = Box<dyn ManifestFormat>;57 fn configure(&self, _s: &State) -> Result<Self::Guards> {58 let format: Box<dyn ManifestFormat> = if self.string {59 Box::new(StringFormat)60 } else {61 #[cfg(feature = "exp-preserve-order")]62 let preserve_order = self.preserve_order;63 match self.format {64 ManifestFormatName::String => Box::new(ToStringFormat),65 ManifestFormatName::Json => Box::new(JsonFormat::cli(66 self.line_padding.unwrap_or(3),67 #[cfg(feature = "exp-preserve-order")]68 preserve_order,69 )),70 ManifestFormatName::Yaml => Box::new(YamlFormat::cli(71 self.line_padding.unwrap_or(2),72 #[cfg(feature = "exp-preserve-order")]73 preserve_order,74 )),75 }76 };77 Ok(if self.yaml_stream {78 Box::new(YamlStreamFormat(format))79 } else {80 format81 })82 }83}8485#[derive(Parser)]86pub struct OutputOpts {87 88 #[clap(long, short = 'o')]89 pub output_file: Option<PathBuf>,90 91 #[clap(long, short = 'c')]92 pub create_output_dirs: bool,93 94 #[clap(long, short = 'm')]95 pub multi: Option<PathBuf>,96}