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

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.5 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, 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 call.22	#[clap(long, short = 'f', default_value = "json")]23	format: ManifestFormatName,24	/// Expect plain string as output.25	/// Mutually exclusive with `--format`26	#[clap(long, short = 'S', conflicts_with = "format")]27	string: bool,28	/// Write output as YAML stream, can be used with --format json/yaml29	#[clap(long, short = 'y', conflicts_with = "string")]30	yaml_stream: bool,31	/// Number of spaces to pad output manifest with.32	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml/toml]33	#[clap(long)]34	line_padding: Option<usize>,35	/// Preserve order in object manifestification36	#[cfg(feature = "exp-preserve-order")]37	#[clap(long)]38	pub preserve_order: bool,39}40impl ManifestOpts {41	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {42		let format: Box<dyn ManifestFormat> = if self.string {43			Box::new(StringFormat)44		} else {45			#[cfg(feature = "exp-preserve-order")]46			let preserve_order = self.preserve_order;47			match self.format {48				ManifestFormatName::String => Box::new(ToStringFormat),49				ManifestFormatName::Json => Box::new(JsonFormat::cli(50					self.line_padding.unwrap_or(3),51					#[cfg(feature = "exp-preserve-order")]52					preserve_order,53				)),54				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(55					self.line_padding.unwrap_or(2),56					#[cfg(feature = "exp-preserve-order")]57					preserve_order,58				)),59				ManifestFormatName::Toml => Box::new(TomlFormat::cli(60					self.line_padding.unwrap_or(2),61					#[cfg(feature = "exp-preserve-order")]62					preserve_order,63				)),64			}65		};66		if self.yaml_stream {67			Box::new(YamlStreamFormat(format))68		} else {69			format70		}71	}72}7374#[derive(Parser)]75pub struct OutputOpts {76	/// Write to the output file rather than stdout77	#[clap(long, short = 'o')]78	pub output_file: Option<PathBuf>,79	/// Automatically creates all parent directories for files80	#[clap(long, short = 'c')]81	pub create_output_dirs: bool,82	/// Write multiple files to the directory, list files on stdout83	#[clap(long, short = 'm')]84	pub multi: Option<PathBuf>,85}