1use std::{path::PathBuf, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};56use crate::ConfigureState;78pub enum ManifestFormatName {9 10 String,11 Json,12 Yaml,13}1415impl FromStr for ManifestFormatName {16 type Err = &'static str;17 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {18 Ok(match s {19 "string" => ManifestFormatName::String,20 "json" => ManifestFormatName::Json,21 "yaml" => ManifestFormatName::Yaml,22 _ => return Err("no such format"),23 })24 }25}2627#[derive(Parser)]28#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]29pub struct ManifestOpts {30 31 32 33 #[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"])]34 format: ManifestFormatName,35 36 37 #[clap(long, short = 'S')]38 string: bool,39 40 #[clap(long, short = 'y')]41 yaml_stream: bool,42 43 44 #[clap(long)]45 line_padding: Option<usize>,46 47 #[cfg(feature = "exp-preserve-order")]48 #[clap(long)]49 exp_preserve_order: bool,50}51impl ConfigureState for ManifestOpts {52 fn configure(&self, state: &EvaluationState) -> Result<()> {53 if self.string {54 state.set_manifest_format(ManifestFormat::String);55 } else {56 #[cfg(feature = "exp-preserve-order")]57 let preserve_order = self.exp_preserve_order;58 match self.format {59 ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),60 ManifestFormatName::Json => state.set_manifest_format(ManifestFormat::Json {61 padding: self.line_padding.unwrap_or(3),62 #[cfg(feature = "exp-preserve-order")]63 preserve_order,64 }),65 ManifestFormatName::Yaml => state.set_manifest_format(ManifestFormat::Yaml {66 padding: self.line_padding.unwrap_or(2),67 #[cfg(feature = "exp-preserve-order")]68 preserve_order,69 }),70 }71 }72 if self.yaml_stream {73 state.set_manifest_format(ManifestFormat::YamlStream(Box::new(74 state.manifest_format(),75 )))76 }77 Ok(())78 }79}8081#[derive(Parser)]82pub struct OutputOpts {83 84 #[clap(long, short = 'o')]85 pub output_file: Option<PathBuf>,86 87 #[clap(long, short = 'c')]88 pub create_output_dirs: bool,89 90 #[clap(long, short = 'm')]91 pub multi: Option<PathBuf>,92}