git.delta.rocks / jrsonnet / refs/commits / 80f37a416bf7

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.5 KiBsourcehistory
1use std::{path::PathBuf, str::FromStr};23use clap::Parser;4use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};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}47impl ConfigureState for ManifestOpts {48	fn configure(&self, state: &EvaluationState) -> Result<()> {49		if self.string {50			state.set_manifest_format(ManifestFormat::String);51		} else {52			match self.format {53				ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),54				ManifestFormatName::Json => {55					state.set_manifest_format(ManifestFormat::Json(self.line_padding.unwrap_or(3)))56				}57				ManifestFormatName::Yaml => {58					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding.unwrap_or(2)))59				}60			}61		}62		if self.yaml_stream {63			state.set_manifest_format(ManifestFormat::YamlStream(Box::new(64				state.manifest_format(),65			)))66		}67		Ok(())68	}69}7071#[derive(Parser)]72pub struct OutputOpts {73	/// Write to the output file rather than stdout74	#[clap(long, short = 'o')]75	pub output_file: Option<PathBuf>,76	/// Automatically creates all parent directories for files77	#[clap(long, short = 'c')]78	pub create_output_dirs: bool,79	/// Write multiple files to the directory, list files on stdout80	#[clap(long, short = 'm')]81	pub multi: Option<PathBuf>,82}