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

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.6 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};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	Toml,20}2122#[derive(Parser)]23#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]24pub struct ManifestOpts {25	/// Output format, wraps resulting value to corresponding std.manifest call.26	#[clap(long, short = 'f', default_value = "json")]27	format: ManifestFormatName,28	/// Expect plain string as output.29	/// Mutually exclusive with `--format`30	#[clap(long, short = 'S', conflicts_with = "format")]31	string: bool,32	/// Write output as YAML stream, can be used with --format json/yaml33	#[clap(long, short = 'y', conflicts_with = "string")]34	yaml_stream: bool,35	/// Number of spaces to pad output manifest with.36	/// `0` for hard tabs, `-1` for single line output [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 ConfigureState for ManifestOpts {45	type Guards = Box<dyn ManifestFormat>;46	fn configure(&self, _s: &State) -> Result<Self::Guards> {47		let format: Box<dyn ManifestFormat> = if self.string {48			Box::new(StringFormat)49		} else {50			#[cfg(feature = "exp-preserve-order")]51			let preserve_order = self.preserve_order;52			match self.format {53				ManifestFormatName::String => Box::new(ToStringFormat),54				ManifestFormatName::Json => Box::new(JsonFormat::cli(55					self.line_padding.unwrap_or(3),56					#[cfg(feature = "exp-preserve-order")]57					preserve_order,58				)),59				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(60					self.line_padding.unwrap_or(2),61					#[cfg(feature = "exp-preserve-order")]62					preserve_order,63				)),64				ManifestFormatName::Toml => Box::new(TomlFormat::cli(65					self.line_padding.unwrap_or(2),66					#[cfg(feature = "exp-preserve-order")]67					preserve_order,68				)),69			}70		};71		Ok(if self.yaml_stream {72			Box::new(YamlStreamFormat(format))73		} else {74			format75		})76	}77}7879#[derive(Parser)]80pub struct OutputOpts {81	/// Write to the output file rather than stdout82	#[clap(long, short = 'o')]83	pub output_file: Option<PathBuf>,84	/// Automatically creates all parent directories for files85	#[clap(long, short = 'c')]86	pub create_output_dirs: bool,87	/// Write multiple files to the directory, list files on stdout88	#[clap(long, short = 'm')]89	pub multi: Option<PathBuf>,90}