git.delta.rocks / jrsonnet / refs/commits / 974f2c15c0a2

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.5 KiBsourcehistory
1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::{5	error::Result,6	manifest::{JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat},7	State,8};9use jrsonnet_stdlib::{TomlFormat, YamlFormat};1011#[derive(Clone, ValueEnum)]12pub enum ManifestFormatName {13	/// Expect string as output, and write them directly14	String,15	Json,16	Yaml,17	Toml,18}1920#[derive(Parser)]21#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]22pub struct ManifestOpts {23	/// Output format, wraps resulting value to corresponding std.manifest call.24	#[clap(long, short = 'f', default_value = "json")]25	format: 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 output [default: 3 for json, 2 for yaml/toml]35	#[clap(long)]36	line_padding: Option<usize>,37	/// Preserve order in object manifestification38	#[cfg(feature = "exp-preserve-order")]39	#[clap(long)]40	pub preserve_order: bool,41}42impl ManifestOpts {43	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {44		let format: Box<dyn ManifestFormat> = if self.string {45			Box::new(StringFormat)46		} else {47			#[cfg(feature = "exp-preserve-order")]48			let preserve_order = self.preserve_order;49			match self.format {50				ManifestFormatName::String => Box::new(ToStringFormat),51				ManifestFormatName::Json => Box::new(JsonFormat::cli(52					self.line_padding.unwrap_or(3),53					#[cfg(feature = "exp-preserve-order")]54					preserve_order,55				)),56				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(57					self.line_padding.unwrap_or(2),58					#[cfg(feature = "exp-preserve-order")]59					preserve_order,60				)),61				ManifestFormatName::Toml => Box::new(TomlFormat::cli(62					self.line_padding.unwrap_or(2),63					#[cfg(feature = "exp-preserve-order")]64					preserve_order,65				)),66			}67		};68		if self.yaml_stream {69			Box::new(YamlStreamFormat(format))70		} else {71			format72		}73	}74}7576#[derive(Parser)]77pub struct OutputOpts {78	/// Write to the output file rather than stdout79	#[clap(long, short = 'o')]80	pub output_file: Option<PathBuf>,81	/// Automatically creates all parent directories for files82	#[clap(long, short = 'c')]83	pub create_output_dirs: bool,84	/// Write multiple files to the directory, list files on stdout85	#[clap(long, short = 'm')]86	pub multi: Option<PathBuf>,87}