git.delta.rocks / jrsonnet / refs/commits / b5d51b90d7ba

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.8 KiBsourcehistory
1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5	JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{TomlFormat, XmlJsonmlFormat, YamlFormat};89#[derive(Clone, Copy, ValueEnum)]10pub enum ManifestFormatName {11	/// Expect string as output, and write them directly12	String,13	Json,14	Yaml,15	Toml,16	XmlJsonml,17}1819#[derive(Parser)]20#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]21pub struct ManifestOpts {22	/// Output format, wraps resulting value to corresponding std.manifest call23	///24	/// [default: json, yaml when -y is used]25	#[clap(long, short = 'f')]26	format: Option<ManifestFormatName>,27	/// Expect plain string as output.28	/// Mutually exclusive with `--format`29	#[clap(long, short = 'S', conflicts_with = "format")]30	string: bool,31	/// Write output as YAML stream, can be used with --format json/yaml32	#[clap(long, short = 'y', conflicts_with = "string")]33	yaml_stream: bool,34	/// Number of spaces to pad output manifest with.35	/// `0` for hard tabs, `-1` for single line output36	///37	/// [default: 3 for json, 2 for yaml/toml]38	#[clap(long)]39	line_padding: Option<usize>,40	/// Preserve order in object manifestification41	#[cfg(feature = "exp-preserve-order")]42	#[clap(long)]43	pub preserve_order: bool,44}45impl ManifestOpts {46	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {47		let format: Box<dyn ManifestFormat> = if self.string {48			Box::new(StringFormat)49		} else {50			#[cfg(feature = "exp-preserve-order")]51			let preserve_order = self.preserve_order;52			let format = match self.format {53				Some(v) => v,54				None if self.yaml_stream => ManifestFormatName::Yaml,55				None => ManifestFormatName::Json,56			};57			match format {58				ManifestFormatName::String => Box::new(ToStringFormat),59				ManifestFormatName::Json => Box::new(JsonFormat::cli(60					self.line_padding.unwrap_or(3),61					#[cfg(feature = "exp-preserve-order")]62					preserve_order,63				)),64				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(65					self.line_padding.unwrap_or(2),66					#[cfg(feature = "exp-preserve-order")]67					preserve_order,68				)),69				ManifestFormatName::Toml => Box::new(TomlFormat::cli(70					self.line_padding.unwrap_or(2),71					#[cfg(feature = "exp-preserve-order")]72					preserve_order,73				)),74				ManifestFormatName::XmlJsonml => Box::new(XmlJsonmlFormat::cli()),75			}76		};77		if self.yaml_stream {78			Box::new(YamlStreamFormat::cli(format))79		} else {80			format81		}82	}83}8485#[derive(Parser)]86pub struct OutputOpts {87	/// Write to the output file rather than stdout88	#[clap(long, short = 'o')]89	pub output_file: Option<PathBuf>,90	/// Automatically creates all parent directories for files91	#[clap(long, short = 'c')]92	pub create_output_dirs: bool,93	/// Write multiple files to the directory, list files on stdout94	#[clap(long, short = 'm')]95	pub multi: Option<PathBuf>,96}