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

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.7 KiBsourcehistory
1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5	JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{TomlFormat, 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}1718#[derive(Parser)]19#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]20pub struct ManifestOpts {21	/// Output format, wraps resulting value to corresponding std.manifest call22	///23	/// [default: json, yaml when -y is used]24	#[clap(long, short = 'f')]25	format: Option<ManifestFormatName>,26	/// Expect plain string as output.27	/// Mutually exclusive with `--format`28	#[clap(long, short = 'S', conflicts_with = "format")]29	string: bool,30	/// Write output as YAML stream, can be used with --format json/yaml31	#[clap(long, short = 'y', conflicts_with = "string")]32	yaml_stream: bool,33	/// Number of spaces to pad output manifest with.34	/// `0` for hard tabs, `-1` for single line output35	///36	/// [default: 3 for json, 2 for yaml/toml]37	#[clap(long)]38	line_padding: Option<usize>,39	/// Preserve order in object manifestification40	#[cfg(feature = "exp-preserve-order")]41	#[clap(long)]42	pub preserve_order: bool,43}44impl ManifestOpts {45	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {46		let format: Box<dyn ManifestFormat> = if self.string {47			Box::new(StringFormat)48		} else {49			#[cfg(feature = "exp-preserve-order")]50			let preserve_order = self.preserve_order;51			let format = match self.format {52				Some(v) => v,53				None if self.yaml_stream => ManifestFormatName::Yaml,54				None => ManifestFormatName::Json,55			};56			match format {57				ManifestFormatName::String => Box::new(ToStringFormat),58				ManifestFormatName::Json => Box::new(JsonFormat::cli(59					self.line_padding.unwrap_or(3),60					#[cfg(feature = "exp-preserve-order")]61					preserve_order,62				)),63				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(64					self.line_padding.unwrap_or(2),65					#[cfg(feature = "exp-preserve-order")]66					preserve_order,67				)),68				ManifestFormatName::Toml => Box::new(TomlFormat::cli(69					self.line_padding.unwrap_or(2),70					#[cfg(feature = "exp-preserve-order")]71					preserve_order,72				)),73			}74		};75		if self.yaml_stream {76			Box::new(YamlStreamFormat(format))77		} else {78			format79		}80	}81}8283#[derive(Parser)]84pub struct OutputOpts {85	/// Write to the output file rather than stdout86	#[clap(long, short = 'o')]87	pub output_file: Option<PathBuf>,88	/// Automatically creates all parent directories for files89	#[clap(long, short = 'c')]90	pub create_output_dirs: bool,91	/// Write multiple files to the directory, list files on stdout92	#[clap(long, short = 'm')]93	pub multi: Option<PathBuf>,94}