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