1use std::{path::PathBuf, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, ManifestFormat, State};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 #[clap(long, short = 'f', default_value = "json", possible_values = &["json", "yaml"])]32 format: ManifestFormatName,33 34 35 #[clap(long, short = 'S', conflicts_with = "format")]36 string: bool,37 38 #[clap(long, short = 'y')]39 yaml_stream: bool,40 41 42 #[clap(long)]43 line_padding: Option<usize>,44 45 #[cfg(feature = "exp-preserve-order")]46 #[clap(long)]47 exp_preserve_order: bool,48}49impl ConfigureState for ManifestOpts {50 type Guards = ();51 fn configure(&self, s: &State) -> Result<()> {52 if self.string {53 s.set_manifest_format(ManifestFormat::String);54 } else {55 #[cfg(feature = "exp-preserve-order")]56 let preserve_order = self.exp_preserve_order;57 match self.format {58 ManifestFormatName::String => s.set_manifest_format(ManifestFormat::String),59 ManifestFormatName::Json => s.set_manifest_format(ManifestFormat::Json {60 padding: self.line_padding.unwrap_or(3),61 #[cfg(feature = "exp-preserve-order")]62 preserve_order,63 }),64 ManifestFormatName::Yaml => s.set_manifest_format(ManifestFormat::Yaml {65 padding: self.line_padding.unwrap_or(2),66 #[cfg(feature = "exp-preserve-order")]67 preserve_order,68 }),69 }70 }71 if self.yaml_stream {72 s.set_manifest_format(ManifestFormat::YamlStream(Box::new(s.manifest_format())))73 }74 Ok(())75 }76}7778#[derive(Parser)]79pub struct OutputOpts {80 81 #[clap(long, short = 'o')]82 pub output_file: Option<PathBuf>,83 84 #[clap(long, short = 'c')]85 pub create_output_dirs: bool,86 87 #[clap(long, short = 'm')]88 pub multi: Option<PathBuf>,89}