Skip to main content

jrsonnet_cli/
manifest.rs

1use std::path::PathBuf;
2
3use clap::{Parser, ValueEnum};
4use jrsonnet_evaluator::manifest::{
5	JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,
6};
7use jrsonnet_stdlib::{IniFormat, TomlFormat, XmlJsonmlFormat, YamlFormat};
8
9#[derive(Clone, Copy, ValueEnum)]
10pub enum ManifestFormatName {
11	/// Expect string as output, and write them directly
12	String,
13	Json,
14	Yaml,
15	Toml,
16	XmlJsonml,
17	Ini,
18}
19
20#[derive(Parser)]
21#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]
22pub struct ManifestOpts {
23	/// Output format, wraps resulting value to corresponding std.manifest call
24	///
25	/// [default: json, yaml when -y is used]
26	#[clap(long, short = 'f')]
27	format: Option<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/yaml
33	#[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
37	///
38	/// [default: 3 for json, 2 for yaml/toml]
39	#[clap(long)]
40	line_padding: Option<usize>,
41	/// No not add a trailing newline to the output
42	#[clap(long)]
43	pub no_trailing_newline: bool,
44	/// Preserve order in object manifestification
45	#[cfg(feature = "exp-preserve-order")]
46	#[clap(long)]
47	pub preserve_order: bool,
48}
49impl ManifestOpts {
50	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {
51		let format: Box<dyn ManifestFormat> = if self.string {
52			Box::new(StringFormat)
53		} else {
54			#[cfg(feature = "exp-preserve-order")]
55			let preserve_order = self.preserve_order;
56			let format = match self.format {
57				Some(v) => v,
58				None if self.yaml_stream => ManifestFormatName::Yaml,
59				None => ManifestFormatName::Json,
60			};
61			match format {
62				ManifestFormatName::String => Box::new(ToStringFormat),
63				ManifestFormatName::Json => Box::new(JsonFormat::cli(
64					self.line_padding.unwrap_or(3),
65					#[cfg(feature = "exp-preserve-order")]
66					preserve_order,
67				)),
68				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(
69					self.line_padding.unwrap_or(2),
70					#[cfg(feature = "exp-preserve-order")]
71					preserve_order,
72				)),
73				ManifestFormatName::Toml => Box::new(TomlFormat::cli(
74					self.line_padding.unwrap_or(2),
75					#[cfg(feature = "exp-preserve-order")]
76					preserve_order,
77				)),
78				ManifestFormatName::XmlJsonml => Box::new(XmlJsonmlFormat::cli()),
79				ManifestFormatName::Ini => Box::new(IniFormat::cli(
80					#[cfg(feature = "exp-preserve-order")]
81					preserve_order,
82				)),
83			}
84		};
85		if self.yaml_stream {
86			Box::new(YamlStreamFormat::cli(format))
87		} else {
88			format
89		}
90	}
91}
92
93#[derive(Parser)]
94pub struct OutputOpts {
95	/// Write to the output file rather than stdout
96	#[clap(long, short = 'o')]
97	pub output_file: Option<PathBuf>,
98	/// Automatically creates all parent directories for files
99	#[clap(long, short = 'c')]
100	pub create_output_dirs: bool,
101	/// Write multiple files to the directory, list files on stdout
102	#[clap(long, short = 'm')]
103	pub multi: Option<PathBuf>,
104}