git.delta.rocks / jrsonnet / refs/commits / 1925b3a76ba9

difftreelog

source

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