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

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	stdlib::manifest::{JsonFormat, StringFormat, ToStringFormat, YamlFormat, YamlStreamFormat},7	ManifestFormat, State,8};910use crate::ConfigureState;1112#[derive(Clone, ValueEnum)]13pub enum ManifestFormatName {14	/// Expect string as output, and write them directly15	String,16	Json,17	Yaml,18}1920impl FromStr for ManifestFormatName {21	type Err = &'static str;22	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {23		Ok(match s {24			"string" => ManifestFormatName::String,25			"json" => ManifestFormatName::Json,26			"yaml" => ManifestFormatName::Yaml,27			_ => return Err("no such format"),28		})29	}30}3132#[derive(Parser)]33#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]34pub struct ManifestOpts {35	/// Output format, wraps resulting value to corresponding std.manifest call.36	#[clap(long, short = 'f', default_value = "json")]37	format: ManifestFormatName,38	/// Expect plain string as output.39	/// Mutually exclusive with `--format`40	#[clap(long, short = 'S', conflicts_with = "format")]41	string: bool,42	/// Write output as YAML stream, can be used with --format json/yaml43	#[clap(long, short = 'y', conflicts_with = "string")]44	yaml_stream: bool,45	/// Number of spaces to pad output manifest with.46	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]47	#[clap(long)]48	line_padding: Option<usize>,49	/// Preserve order in object manifestification50	#[cfg(feature = "exp-preserve-order")]51	#[clap(long)]52	preserve_order: bool,53}54impl ConfigureState for ManifestOpts {55	type Guards = Box<dyn ManifestFormat>;56	fn configure(&self, _s: &State) -> Result<Self::Guards> {57		let format: Box<dyn ManifestFormat> = if self.string {58			Box::new(StringFormat)59		} else {60			#[cfg(feature = "exp-preserve-order")]61			let preserve_order = self.preserve_order;62			match self.format {63				ManifestFormatName::String => Box::new(ToStringFormat),64				ManifestFormatName::Json => Box::new(JsonFormat::cli(65					self.line_padding.unwrap_or(3),66					#[cfg(feature = "exp-preserve-order")]67					preserve_order,68				)),69				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(70					self.line_padding.unwrap_or(2),71					#[cfg(feature = "exp-preserve-order")]72					preserve_order,73				)),74			}75		};76		Ok(if self.yaml_stream {77			Box::new(YamlStreamFormat(format))78		} else {79			format80		})81	}82}8384#[derive(Parser)]85pub struct OutputOpts {86	/// Write to the output file rather than stdout87	#[clap(long, short = 'o')]88	pub output_file: Option<PathBuf>,89	/// Automatically creates all parent directories for files90	#[clap(long, short = 'c')]91	pub create_output_dirs: bool,92	/// Write multiple files to the directory, list files on stdout93	#[clap(long, short = 'm')]94	pub multi: Option<PathBuf>,95}