git.delta.rocks / jrsonnet / refs/commits / 88907d8b259a

difftreelog

perf move ini manifestification to native

Yaroslav Bolyukin2024-05-19parent: #0ef9787.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-cli/src/manifest.rsdiffbeforeafterboth
before · crates/jrsonnet-cli/src/manifest.rs
1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5	JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{TomlFormat, XmlJsonmlFormat, YamlFormat};89#[derive(Clone, Copy, ValueEnum)]10pub enum ManifestFormatName {11	/// Expect string as output, and write them directly12	String,13	Json,14	Yaml,15	Toml,16	XmlJsonml,17}1819#[derive(Parser)]20#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]21pub struct ManifestOpts {22	/// Output format, wraps resulting value to corresponding std.manifest call23	///24	/// [default: json, yaml when -y is used]25	#[clap(long, short = 'f')]26	format: Option<ManifestFormatName>,27	/// Expect plain string as output.28	/// Mutually exclusive with `--format`29	#[clap(long, short = 'S', conflicts_with = "format")]30	string: bool,31	/// Write output as YAML stream, can be used with --format json/yaml32	#[clap(long, short = 'y', conflicts_with = "string")]33	yaml_stream: bool,34	/// Number of spaces to pad output manifest with.35	/// `0` for hard tabs, `-1` for single line output36	///37	/// [default: 3 for json, 2 for yaml/toml]38	#[clap(long)]39	line_padding: Option<usize>,40	/// Preserve order in object manifestification41	#[cfg(feature = "exp-preserve-order")]42	#[clap(long)]43	pub preserve_order: bool,44}45impl ManifestOpts {46	pub fn manifest_format(&self) -> Box<dyn ManifestFormat> {47		let format: Box<dyn ManifestFormat> = if self.string {48			Box::new(StringFormat)49		} else {50			#[cfg(feature = "exp-preserve-order")]51			let preserve_order = self.preserve_order;52			let format = match self.format {53				Some(v) => v,54				None if self.yaml_stream => ManifestFormatName::Yaml,55				None => ManifestFormatName::Json,56			};57			match format {58				ManifestFormatName::String => Box::new(ToStringFormat),59				ManifestFormatName::Json => Box::new(JsonFormat::cli(60					self.line_padding.unwrap_or(3),61					#[cfg(feature = "exp-preserve-order")]62					preserve_order,63				)),64				ManifestFormatName::Yaml => Box::new(YamlFormat::cli(65					self.line_padding.unwrap_or(2),66					#[cfg(feature = "exp-preserve-order")]67					preserve_order,68				)),69				ManifestFormatName::Toml => Box::new(TomlFormat::cli(70					self.line_padding.unwrap_or(2),71					#[cfg(feature = "exp-preserve-order")]72					preserve_order,73				)),74				ManifestFormatName::XmlJsonml => Box::new(XmlJsonmlFormat::cli()),75			}76		};77		if self.yaml_stream {78			Box::new(YamlStreamFormat::cli(format))79		} else {80			format81		}82	}83}8485#[derive(Parser)]86pub struct OutputOpts {87	/// Write to the output file rather than stdout88	#[clap(long, short = 'o')]89	pub output_file: Option<PathBuf>,90	/// Automatically creates all parent directories for files91	#[clap(long, short = 'c')]92	pub create_output_dirs: bool,93	/// Write multiple files to the directory, list files on stdout94	#[clap(long, short = 'm')]95	pub multi: Option<PathBuf>,96}
after · crates/jrsonnet-cli/src/manifest.rs
1use std::path::PathBuf;23use clap::{Parser, ValueEnum};4use jrsonnet_evaluator::manifest::{5	JsonFormat, ManifestFormat, StringFormat, ToStringFormat, YamlStreamFormat,6};7use jrsonnet_stdlib::{IniFormat, TomlFormat, XmlJsonmlFormat, YamlFormat};89#[derive(Clone, Copy, ValueEnum)]10pub enum ManifestFormatName {11	/// Expect string as output, and write them directly12	String,13	Json,14	Yaml,15	Toml,16	XmlJsonml,17	Ini,18}1920#[derive(Parser)]21#[clap(next_help_heading = "MANIFESTIFICATION OUTPUT")]22pub struct ManifestOpts {23	/// Output format, wraps resulting value to corresponding std.manifest call24	///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/yaml33	#[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 output37	///38	/// [default: 3 for json, 2 for yaml/toml]39	#[clap(long)]40	line_padding: Option<usize>,41	/// Preserve order in object manifestification42	#[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			format86		}87	}88}8990#[derive(Parser)]91pub struct OutputOpts {92	/// Write to the output file rather than stdout93	#[clap(long, short = 'o')]94	pub output_file: Option<PathBuf>,95	/// Automatically creates all parent directories for files96	#[clap(long, short = 'c')]97	pub create_output_dirs: bool,98	/// Write multiple files to the directory, list files on stdout99	#[clap(long, short = 'm')]100	pub multi: Option<PathBuf>,101}
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -179,6 +179,7 @@
 		("manifestPython", builtin_manifest_python::INST),
 		("manifestPythonVars", builtin_manifest_python_vars::INST),
 		("manifestXmlJsonml", builtin_manifest_xml_jsonml::INST),
+		("manifestIni", builtin_manifest_ini::INST),
 		// Parse
 		("parseJson", builtin_parse_json::INST),
 		("parseYaml", builtin_parse_yaml::INST),
modifiedcrates/jrsonnet-stdlib/src/manifest/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/mod.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/mod.rs
@@ -1,8 +1,10 @@
+mod ini;
 mod python;
 mod toml;
 mod xml;
 mod yaml;
 
+pub use ini::IniFormat;
 use jrsonnet_evaluator::{
 	function::builtin,
 	manifest::{escape_string_json, JsonFormat, YamlStreamFormat},
@@ -190,3 +192,17 @@
 pub fn builtin_manifest_xml_jsonml(value: Val) -> Result<String> {
 	value.manifest(XmlJsonmlFormat::std_to_xml())
 }
+
+#[builtin]
+pub fn builtin_manifest_ini(
+	ini: Val,
+
+	#[default(false)]
+	#[cfg(feature = "exp-preserve-order")]
+	preserve_order: bool,
+) -> Result<String> {
+	ini.manifest(IniFormat::std(
+		#[cfg(feature = "exp-preserve-order")]
+		preserve_order,
+	))
+}
modifiedcrates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -196,7 +196,7 @@
 		#[cfg(feature = "exp-preserve-order")]
 		options.preserve_order,
 	) {
-		let value = value?;
+		let value = value.with_description(|| format!("field <{key}> evaluation"))?;
 		if is_section(&value)? {
 			sections.push((key, value));
 		} else {
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -25,26 +25,6 @@
     else
       error 'Assertion failed. ' + a + ' != ' + b,
 
-  manifestIni(ini)::
-    local body_lines(body) =
-      std.join([], [
-        local value_or_values = body[k];
-        if std.isArray(value_or_values) then
-          ['%s = %s' % [k, value] for value in value_or_values]
-        else
-          ['%s = %s' % [k, value_or_values]]
-
-        for k in std.objectFields(body)
-      ]);
-
-    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),
-          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],
-          all_sections = [
-      section_lines(k, ini.sections[k])
-      for k in std.objectFields(ini.sections)
-    ];
-    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),
-
   mergePatch(target, patch)::
     if std.isObject(patch) then
       local target_object =