git.delta.rocks / jrsonnet / refs/commits / 0ef9787dd3a6

difftreelog

fix ToStringFormat should not quote top-level string

Yaroslav Bolyukin2024-06-18parent: #12a8bf0.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -84,8 +84,9 @@
 			debug_truncate_strings: None,
 		}
 	}
-	// Same format as std.toString
-	pub fn std_to_string() -> Self {
+	/// Same format as std.toString, except does not keeps top-level string as-is
+	/// To avoid confusion, the format is private in jrsonnet, use [`ToStringFormat`] instead
+	const fn std_to_string_helper() -> Self {
 		Self {
 			padding: Cow::Borrowed(""),
 			mtype: JsonFormatting::ToString,
@@ -344,10 +345,17 @@
 	}
 }
 
+/// Same as [`JsonFormat`] with pre-set options, but top-level string is serialized as-is,
+/// without quoting.
 pub struct ToStringFormat;
 impl ManifestFormat for ToStringFormat {
 	fn manifest_buf(&self, val: Val, out: &mut String) -> Result<()> {
-		JsonFormat::std_to_string().manifest_buf(val, out)
+		const JSON_TO_STRING: JsonFormat = JsonFormat::std_to_string_helper();
+		if let Some(str) = val.as_str() {
+			out.push_str(&str);
+			return Ok(());
+		}
+		JSON_TO_STRING.manifest_buf(val, out)
 	}
 	fn file_trailing_newline(&self) -> bool {
 		false
modifiedcrates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth
132 Optional,132 Optional,
133 Default(Expr),133 Default(Expr),
134}134}
135impl Optionality {
136 fn is_optional(&self) -> bool {
137 !matches!(self, Self::Required)
138 }
139}
140135
141enum ArgInfo {136enum ArgInfo {
142 Normal {137 Normal {