git.delta.rocks / jrsonnet / refs/commits / 446308e1b3b1

difftreelog

fix mark -S and --format as mutually exclusive

Yaroslav Bolyukin2022-10-17parent: #5e6d5ee.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
before · crates/jrsonnet-cli/src/manifest.rs
1use std::{path::PathBuf, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, ManifestFormat, State};56use crate::ConfigureState;78pub enum ManifestFormatName {9	/// Expect string as output, and write them directly10	String,11	Json,12	Yaml,13}1415impl FromStr for ManifestFormatName {16	type Err = &'static str;17	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {18		Ok(match s {19			"string" => ManifestFormatName::String,20			"json" => ManifestFormatName::Json,21			"yaml" => ManifestFormatName::Yaml,22			_ => return Err("no such format"),23		})24	}25}2627#[derive(Parser)]28#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]29pub struct ManifestOpts {30	/// Output format, wraps resulting value to corresponding std.manifest call.31	/// If set to `string` then plain string value is expected to be returned,32	/// otherwise output will be serialized to the specified format.33	#[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"])]34	format: ManifestFormatName,35	/// Expect plain string as output.36	/// Shortcut for `--format=string` thus this option is mutually exclusive with `format` option.37	#[clap(long, short = 'S')]38	string: bool,39	/// Write output as YAML stream, can be used with --format json/yaml40	#[clap(long, short = 'y')]41	yaml_stream: bool,42	/// Number of spaces to pad output manifest with.43	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]44	#[clap(long)]45	line_padding: Option<usize>,46	/// Preserve order in object manifestification47	#[cfg(feature = "exp-preserve-order")]48	#[clap(long)]49	exp_preserve_order: bool,50}51impl ConfigureState for ManifestOpts {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}
after · crates/jrsonnet-cli/src/manifest.rs
1use std::{path::PathBuf, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, ManifestFormat, State};56use crate::ConfigureState;78pub enum ManifestFormatName {9	/// Expect string as output, and write them directly10	String,11	Json,12	Yaml,13}1415impl FromStr for ManifestFormatName {16	type Err = &'static str;17	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {18		Ok(match s {19			"string" => ManifestFormatName::String,20			"json" => ManifestFormatName::Json,21			"yaml" => ManifestFormatName::Yaml,22			_ => return Err("no such format"),23		})24	}25}2627#[derive(Parser)]28#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]29pub struct ManifestOpts {30	/// Output format, wraps resulting value to corresponding std.manifest call.31	#[clap(long, short = 'f', default_value = "json", possible_values = &["json", "yaml"])]32	format: ManifestFormatName,33	/// Expect plain string as output.34	/// Mutually exclusive with `--format`35	#[clap(long, short = 'S', conflicts_with = "format")]36	string: bool,37	/// Write output as YAML stream, can be used with --format json/yaml38	#[clap(long, short = 'y')]39	yaml_stream: bool,40	/// Number of spaces to pad output manifest with.41	/// `0` for hard tabs, `-1` for single line output [default: 3 for json, 2 for yaml]42	#[clap(long)]43	line_padding: Option<usize>,44	/// Preserve order in object manifestification45	#[cfg(feature = "exp-preserve-order")]46	#[clap(long)]47	exp_preserve_order: bool,48}49impl ConfigureState for ManifestOpts {50	fn configure(&self, s: &State) -> Result<()> {51		if self.string {52			s.set_manifest_format(ManifestFormat::String);53		} else {54			#[cfg(feature = "exp-preserve-order")]55			let preserve_order = self.exp_preserve_order;56			match self.format {57				ManifestFormatName::String => s.set_manifest_format(ManifestFormat::String),58				ManifestFormatName::Json => s.set_manifest_format(ManifestFormat::Json {59					padding: self.line_padding.unwrap_or(3),60					#[cfg(feature = "exp-preserve-order")]61					preserve_order,62				}),63				ManifestFormatName::Yaml => s.set_manifest_format(ManifestFormat::Yaml {64					padding: self.line_padding.unwrap_or(2),65					#[cfg(feature = "exp-preserve-order")]66					preserve_order,67				}),68			}69		}70		if self.yaml_stream {71			s.set_manifest_format(ManifestFormat::YamlStream(Box::new(s.manifest_format())))72		}73		Ok(())74	}75}7677#[derive(Parser)]78pub struct OutputOpts {79	/// Write to the output file rather than stdout80	#[clap(long, short = 'o')]81	pub output_file: Option<PathBuf>,82	/// Automatically creates all parent directories for files83	#[clap(long, short = 'c')]84	pub create_output_dirs: bool,85	/// Write multiple files to the directory, list files on stdout86	#[clap(long, short = 'm')]87	pub multi: Option<PathBuf>,88}