1use std::{path::PathBuf, str::FromStr};23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::{error::Result, ManifestFormat, State};56use crate::ConfigureState;78#[derive(Clone, ValueEnum)]9pub enum ManifestFormatName {10 11 String,12 Json,13 Yaml,14}1516impl FromStr for ManifestFormatName {17 type Err = &'static str;18 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {19 Ok(match s {20 "string" => ManifestFormatName::String,21 "json" => ManifestFormatName::Json,22 "yaml" => ManifestFormatName::Yaml,23 _ => return Err("no such format"),24 })25 }26}2728#[derive(Parser)]29#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]30pub struct ManifestOpts {31 32 #[clap(long, short = 'f', default_value = "json")]33 format: ManifestFormatName,34 35 36 #[clap(long, short = 'S', conflicts_with = "format")]37 string: bool,38 39 #[clap(long, short = 'y')]40 yaml_stream: bool,41 42 43 #[clap(long)]44 line_padding: Option<usize>,45 46 #[cfg(feature = "exp-preserve-order")]47 #[clap(long)]48 exp_preserve_order: bool,49}50impl ConfigureState for ManifestOpts {51 type Guards = ();52 fn configure(&self, s: &State) -> Result<()> {53 if self.string {54 s.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 => s.set_manifest_format(ManifestFormat::String),60 ManifestFormatName::Json => s.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 => s.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 s.set_manifest_format(ManifestFormat::YamlStream(Box::new(s.manifest_format())))74 }75 Ok(())76 }77}7879#[derive(Parser)]80pub struct OutputOpts {81 82 #[clap(long, short = 'o')]83 pub output_file: Option<PathBuf>,84 85 #[clap(long, short = 'c')]86 pub create_output_dirs: bool,87 88 #[clap(long, short = 'm')]89 pub multi: Option<PathBuf>,90}