git.delta.rocks / jrsonnet / refs/commits / 31f2b649c192

difftreelog

source

crates/jrsonnet-cli/src/manifest.rs2.5 KiBsourcehistory
1use crate::ConfigureState;2use clap::Clap;3use jrsonnet_evaluator::{error::Result, EvaluationState, ManifestFormat};4use std::{path::PathBuf, str::FromStr};56pub enum ManifestFormatName {7	/// Expect string as output, and write them directly8	String,9	Json,10	Yaml,11}1213impl FromStr for ManifestFormatName {14	type Err = &'static str;15	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {16		Ok(match s {17			"string" => ManifestFormatName::String,18			"json" => ManifestFormatName::Json,19			"yaml" => ManifestFormatName::Yaml,20			_ => return Err("no such format"),21		})22	}23}2425#[derive(Clap)]26#[clap(help_heading = "MANIFESTIFICATION OUTPUT")]27pub struct ManifestOpts {28	/// Output format, wraps resulting value to corresponding std.manifest call.29	/// If set to `string` then plain string value is expected to be returned,30	/// otherwise output will be serialized to the specified format.31	#[clap(long, short = 'f', default_value = "json", possible_values = &["string", "json", "yaml"])]32	format: ManifestFormatName,33	/// Expect plain string as output.34	/// Shortcut for `--format=string` thus this option is mutually exclusive with `format` option.35	#[clap(long, short = 'S')]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}45impl ConfigureState for ManifestOpts {46	fn configure(&self, state: &EvaluationState) -> Result<()> {47		if self.string {48			state.set_manifest_format(ManifestFormat::String);49		} else {50			match self.format {51				ManifestFormatName::String => state.set_manifest_format(ManifestFormat::String),52				ManifestFormatName::Json => {53					state.set_manifest_format(ManifestFormat::Json(self.line_padding.unwrap_or(3)))54				}55				ManifestFormatName::Yaml => {56					state.set_manifest_format(ManifestFormat::Yaml(self.line_padding.unwrap_or(2)))57				}58			}59		}60		if self.yaml_stream {61			state.set_manifest_format(ManifestFormat::YamlStream(Box::new(62				state.manifest_format(),63			)))64		}65		Ok(())66	}67}6869#[derive(Clap)]70pub struct OutputOpts {71	/// Write to the output file rather than stdout72	#[clap(long, short = 'o')]73	pub output_file: Option<PathBuf>,74	/// Automatically creates all parent directories for files75	#[clap(long, short = 'c')]76	pub create_output_dirs: bool,77	/// Write multiple files to the directory, list files on stdout78	#[clap(long, short = 'm')]79	pub multi: Option<PathBuf>,80}