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	/// Preserve order in object manifestification
42	#[cfg(feature = "exp-preserve-order")]
43	#[clap(long)]
44	pub preserve_order: bool,
45}
46impl ManifestOpts {
47	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {
48		let format: Box<dyn ManifestFormat> = if self.string {
49			Box::new(StringFormat)
50		} else {
51			#[cfg(feature = "exp-preserve-order")]
52			let preserve_order = self.preserve_order;
53			let format = match self.format {
54				Some(v) => v,
55				None if self.yaml_stream => ManifestFormatName::Yaml,
56				None => ManifestFormatName::Json,
57			};
58			match format {
59				ManifestFormatName::String => Box::new(ToStringFormat),
60				ManifestFormatName::Json => Box::new(JsonFormat::cli(
61					self.line_padding.unwrap_or(3),
62					#[cfg(feature = "exp-preserve-order")]
63					preserve_order,
64				)),
65				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(
66					self.line_padding.unwrap_or(2),
67					#[cfg(feature = "exp-preserve-order")]
68					preserve_order,
69				)),
70				ManifestFormatName::Toml => Box::new(TomlFormat::cli(
71					self.line_padding.unwrap_or(2),
72					#[cfg(feature = "exp-preserve-order")]
73					preserve_order,
74				)),
75				ManifestFormatName::XmlJsonml => Box::new(XmlJsonmlFormat::cli()),
76				ManifestFormatName::Ini => Box::new(IniFormat::cli(
77					#[cfg(feature = "exp-preserve-order")]
78					preserve_order,
79				)),
80			}
81		};
82		if self.yaml_stream {
83			Box::new(YamlStreamFormat::cli(format))
84		} else {
85			format
86		}
87	}
88}
89
90#[derive(Parser)]
91pub struct OutputOpts {
92	/// Write to the output file rather than stdout
93	#[clap(long, short = 'o')]
94	pub output_file: Option<PathBuf>,
95	/// Automatically creates all parent directories for files
96	#[clap(long, short = 'c')]
97	pub create_output_dirs: bool,
98	/// Write multiple files to the directory, list files on stdout
99	#[clap(long, short = 'm')]
100	pub multi: Option<PathBuf>,
101}