git.delta.rocks / jrsonnet / refs/commits / 85c2a4616ecc

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.8 KiBsourcehistory
1use std::{path::PathBuf, str::FromStr};23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::{5	error::Result,6	manifest::{JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat},7	State,8};9use jrsonnet_stdlib::YamlFormat;1011use crate::ConfigureState;1213#[derive(Clone, ValueEnum)]14pub enum ManifestFormatName {15	/// Expect string as output, and write them directly16	String,17	Json,18	Yaml,19}2021impl FromStr for ManifestFormatName {22	type Err = &'static str;23	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {24		Ok(match s {25			"string" => ManifestFormatName::String,26			"json" => ManifestFormatName::Json,27			"yaml" => ManifestFormatName::Yaml,28			_ => return Err("no such format"),29		})30	}31}3233#[derive(Parser)]34#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]35pub struct ManifestOpts {36	/// Output format, wraps resulting value to corresponding std.manifest call.37	#[clap(long, short = 'f', default_value = "json")]38	format: ManifestFormatName,39	/// Expect plain string as output.40	/// Mutually exclusive with `--format`41	#[clap(long, short = 'S', conflicts_with = "format")]42	string: bool,43	/// Write output as YAML stream, can be used with --format json/yaml44	#[clap(long, short = 'y', conflicts_with = "string")]45	yaml_stream: bool,46	/// Number of spaces to pad output manifest with.47	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]48	#[clap(long)]49	line_padding: Option<usize>,50	/// Preserve order in object manifestification51	#[cfg(feature = "exp-preserve-order")]52	#[clap(long)]53	preserve_order: bool,54}55impl ConfigureState for ManifestOpts {56	type Guards = Box<dyn ManifestFormat>;57	fn configure(&self, _s: &State) -> Result<Self::Guards> {58		let format: Box<dyn ManifestFormat> = if self.string {59			Box::new(StringFormat)60		} else {61			#[cfg(feature = "exp-preserve-order")]62			let preserve_order = self.preserve_order;63			match self.format {64				ManifestFormatName::String => Box::new(ToStringFormat),65				ManifestFormatName::Json => Box::new(JsonFormat::cli(66					self.line_padding.unwrap_or(3),67					#[cfg(feature = "exp-preserve-order")]68					preserve_order,69				)),70				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(71					self.line_padding.unwrap_or(2),72					#[cfg(feature = "exp-preserve-order")]73					preserve_order,74				)),75			}76		};77		Ok(if self.yaml_stream {78			Box::new(YamlStreamFormat(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}