git.delta.rocks / jrsonnet / refs/commits / cc55b2dd2d98

difftreelog

feat add description stacktrace frames for all formats

Yaroslav Bolyukin2024-06-18parent: #912ece7.patch.diff
in: master

5 files changed

modifiedcrates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -183,6 +183,8 @@
 	cur_padding: &mut String,
 	options: &JsonFormat<'_>,
 ) -> Result<()> {
+	use JsonFormatting::*;
+
 	let mtype = options.mtype;
 	match val {
 		Val::Bool(v) => {
@@ -218,89 +220,118 @@
 		}
 		Val::Arr(items) => {
 			buf.push('[');
-			if !items.is_empty() {
-				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
-					buf.push_str(options.newline);
+
+			let old_len = cur_padding.len();
+			cur_padding.push_str(&options.padding);
+
+			let mut had_items = false;
+			for (i, item) in items.iter().enumerate() {
+				had_items = true;
+				let item = item.with_description(|| format!("elem <{i}> evaluation"))?;
+
+				if i != 0 {
+					buf.push(',');
 				}
+				match mtype {
+					Manifest | Std => {
+						buf.push_str(options.newline);
+						buf.push_str(cur_padding);
+					}
+					ToString => buf.push(' '),
+					Minify => {}
+				};
 
-				let old_len = cur_padding.len();
-				cur_padding.push_str(&options.padding);
-				for (i, item) in items.iter().enumerate() {
-					if i != 0 {
-						buf.push(',');
-						if mtype == JsonFormatting::ToString {
-							buf.push(' ');
-						} else if mtype != JsonFormatting::Minify {
-							buf.push_str(options.newline);
-						}
-					}
+				buf.push_str(cur_padding);
+				State::push_description(
+					|| format!("elem <{i}> manifestification"),
+					|| manifest_json_ex_buf(&item, buf, cur_padding, options),
+				)?;
+			}
+
+			cur_padding.truncate(old_len);
+
+			match mtype {
+				Manifest | ToString if !had_items => {
+					// Empty array as "[ ]"
+					buf.push(' ');
+				}
+				Manifest => {
+					buf.push_str(options.newline);
 					buf.push_str(cur_padding);
-					manifest_json_ex_buf(&item?, buf, cur_padding, options)
-						.with_description(|| format!("elem <{i}> manifestification"))?;
 				}
-				cur_padding.truncate(old_len);
-
-				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+				Std => {
+					if !had_items {
+						// Stdlib formats empty array as "[\n\n]"
+						buf.push_str(options.newline);
+					}
 					buf.push_str(options.newline);
 					buf.push_str(cur_padding);
 				}
-			} else if mtype == JsonFormatting::Std {
-				buf.push_str(options.newline);
-				buf.push_str(options.newline);
-				buf.push_str(cur_padding);
-			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {
-				buf.push(' ');
+				Minify | ToString => {}
 			}
+
 			buf.push(']');
 		}
 		Val::Obj(obj) => {
 			obj.run_assertions()?;
 			buf.push('{');
-			let fields = obj.fields(
-				#[cfg(feature = "exp-preserve-order")]
-				options.preserve_order,
-			);
-			if !fields.is_empty() {
-				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
-					buf.push_str(options.newline);
-				}
 
-				let old_len = cur_padding.len();
-				cur_padding.push_str(&options.padding);
-				for (i, field) in fields.into_iter().enumerate() {
-					if i != 0 {
-						buf.push(',');
-						if mtype == JsonFormatting::ToString {
-							buf.push(' ');
-						} else if mtype != JsonFormatting::Minify {
-							buf.push_str(options.newline);
-						}
+			let old_len = cur_padding.len();
+			cur_padding.push_str(&options.padding);
+
+			let mut had_fields = false;
+			for (i, (key, value)) in obj
+				.iter(
+					#[cfg(feature = "exp-preserve-order")]
+					options.preserve_order,
+				)
+				.enumerate()
+			{
+				had_fields = true;
+				let value = value.with_description(|| format!("field <{key}> evaluation"))?;
+
+				if i != 0 {
+					buf.push(',');
+				}
+				match mtype {
+					Manifest | Std => {
+						buf.push_str(options.newline);
+						buf.push_str(cur_padding);
 					}
-					buf.push_str(cur_padding);
-					escape_string_json_buf(&field, buf);
-					buf.push_str(options.key_val_sep);
-					State::push_description(
-						|| format!("field <{}> manifestification", field.clone()),
-						|| {
-							let value = obj.get(field.clone())?.unwrap();
-							manifest_json_ex_buf(&value, buf, cur_padding, options)?;
-							Ok(())
-						},
-					)?;
+					ToString => buf.push(' '),
+					Minify => {}
 				}
-				cur_padding.truncate(old_len);
 
-				if mtype != JsonFormatting::ToString && mtype != JsonFormatting::Minify {
+				escape_string_json_buf(&key, buf);
+				buf.push_str(options.key_val_sep);
+				State::push_description(
+					|| format!("field <{key}> manifestification"),
+					|| manifest_json_ex_buf(&value, buf, cur_padding, options),
+				)?;
+			}
+
+			cur_padding.truncate(old_len);
+
+			match mtype {
+				Manifest | ToString if !had_fields => {
+					// Empty object as "{ }"
+					buf.push(' ');
+				}
+				Manifest => {
 					buf.push_str(options.newline);
 					buf.push_str(cur_padding);
 				}
-			} else if mtype == JsonFormatting::Std {
-				buf.push_str(options.newline);
-				buf.push_str(options.newline);
-				buf.push_str(cur_padding);
-			} else if mtype == JsonFormatting::ToString || mtype == JsonFormatting::Manifest {
-				buf.push(' ');
+				Std => {
+					if !had_fields {
+						// Stdlib formats empty object as "{\n\n}"
+						buf.push_str(options.newline);
+					}
+					buf.push_str(options.newline);
+					buf.push_str(cur_padding);
+				}
+				Minify | ToString => {}
 			}
+
 			buf.push('}');
 		}
 		Val::Func(_) => bail!("tried to manifest function"),
@@ -350,7 +381,7 @@
 		Self {
 			inner,
 			c_document_end,
-			// Stdlib format always inserts newline at the end
+			// Stdlib format always inserts useless newline at the end
 			end_newline: true,
 		}
 	}
@@ -371,10 +402,13 @@
 			)
 		};
 		if !arr.is_empty() {
-			for v in arr.iter() {
-				let v = v?;
+			for (i, v) in arr.iter().enumerate() {
+				let v = v.with_description(|| format!("elem <{i}> evaluation"))?;
 				out.push_str("---\n");
-				self.inner.manifest_buf(v, out)?;
+				State::push_description(
+					|| format!("elem <{i}> manifestification"),
+					|| self.inner.manifest_buf(v, out),
+				)?;
 				out.push('\n');
 			}
 		}
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -11,7 +11,7 @@
 	function::{native::NativeDesc, FuncDesc, FuncVal},
 	typed::CheckType,
 	val::{IndexableVal, StrValue, ThunkMapper},
-	ObjValue, ObjValueBuilder, Result, Thunk, Val,
+	ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,
 };
 
 #[derive(Trace)]
@@ -359,7 +359,12 @@
 			unreachable!("typecheck should fail")
 		};
 		a.iter()
-			.map(|r| r.and_then(T::from_untyped))
+			.enumerate()
+			.map(|(i, r)| {
+				r.and_then(|t| {
+					T::from_untyped(t).with_description(|| format!("parsing elem <{i}>"))
+				})
+			})
 			.collect::<Result<Self>>()
 	}
 }
modifiedcrates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -4,7 +4,7 @@
 	bail,
 	manifest::{escape_string_json_buf, ManifestFormat},
 	val::ArrValue,
-	IStr, ObjValue, Result, Val,
+	IStr, ObjValue, Result, ResultExt, Val, State,
 };
 
 pub struct TomlFormat<'s> {
@@ -106,16 +106,15 @@
 		#[cfg(feature = "exp-bigint")]
 		Val::BigInt(n) => write!(buf, "{n}").unwrap(),
 		Val::Arr(a) => {
-			if a.is_empty() {
-				buf.push_str("[]");
-				return Ok(());
-			}
+			buf.push('[');
+
+			let mut had_items = false;
 			for (i, e) in a.iter().enumerate() {
-				let e = e?;
+				had_items = true;
+				let e = e.with_description(|| format!("elem <{i}> evaluation"))?;
+
 				if i != 0 {
 					buf.push(',');
-				} else {
-					buf.push('[');
 				}
 				if inline {
 					buf.push(' ');
@@ -124,9 +123,15 @@
 					buf.push_str(cur_padding);
 					buf.push_str(&options.padding);
 				}
-				manifest_value(&e, true, buf, "", options)?;
+
+				State::push_description(
+					|| format!("elem <{i}> manifestification"),
+					|| manifest_value(&e, true, buf, "", options),
+				)?;
 			}
-			if inline {
+
+			if !had_items {
+			} else if inline {
 				buf.push(' ');
 			} else {
 				buf.push('\n');
@@ -135,10 +140,10 @@
 			buf.push(']');
 		}
 		Val::Obj(o) => {
-			if o.is_empty() {
-				buf.push_str("{}");
-			}
-			buf.push_str("{ ");
+			o.run_assertions()?;
+			buf.push('{');
+
+			let mut had_fields = false;
 			for (i, (k, v)) in o
 				.iter(
 					#[cfg(feature = "exp-preserve-order")]
@@ -146,15 +151,27 @@
 				)
 				.enumerate()
 			{
-				let v = v?;
+				had_fields = true;
+				let v = v.with_description(|| format!("field <{k}> evaluation"))?;
+
 				if i != 0 {
-					buf.push_str(", ");
+					buf.push(',');
 				}
+				buf.push(' ');
+
 				escape_key_toml_buf(&k, buf);
 				buf.push_str(" = ");
-				manifest_value(&v, true, buf, "", options)?;
+				State::push_description(
+					|| format!("field <{k}> manifestification"),
+					|| manifest_value(&v, true, buf, "", options),
+				)?;
 			}
-			buf.push_str(" }");
+
+			if had_fields {
+				buf.push(' ');
+			}
+
+			buf.push('}');
 		}
 		Val::Null => {
 			bail!("tried to manifest null")
modifiedcrates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,9 +1,9 @@
 use jrsonnet_evaluator::{
 	bail,
 	manifest::{ManifestFormat, ToStringFormat},
-	typed::{ComplexValType, Either4, Typed, ValType},
+	typed::{ComplexValType, Either2, Either4, Typed, ValType},
 	val::{ArrValue, IndexableVal},
-	Either, ObjValue, Result, ResultExt, Val,
+	Either, ObjValue, Result, ResultExt, Val, State,
 };
 
 pub struct XmlJsonmlFormat {
@@ -38,20 +38,22 @@
 	}
 
 	fn from_untyped(untyped: Val) -> Result<Self> {
-		let Val::Arr(arr) = untyped else {
-			if let Val::Str(s) = untyped {
-				return Ok(Self::String(s.to_string()));
-			};
-			bail!("expected JSONML value (an array or string)");
+		let val = <Either![ArrValue, String]>::from_untyped(untyped)
+			.with_description(|| format!("parsing JSONML value (an array or string)"))?;
+		let arr = match val {
+			Either2::A(a) => a,
+			Either2::B(s) => return Ok(Self::String(s)),
 		};
 		if arr.len() < 1 {
-			bail!("JSONML value should have tag");
+			bail!("JSONML value should have tag (array length should be >=1)");
 		};
 		let tag = String::from_untyped(
 			arr.get(0)
 				.with_description(|| "getting JSONML tag")?
 				.expect("length checked"),
-		)?;
+		)
+		.with_description(|| format!("parsing JSONML tag"))?;
+
 		let (has_attrs, attrs) = if arr.len() >= 2 {
 			let maybe_attrs = arr
 				.get(1)
@@ -68,11 +70,16 @@
 		Ok(Self::Tag {
 			tag,
 			attrs,
-			children: Typed::from_untyped(Val::Arr(arr.slice(
-				Some(if has_attrs { 2 } else { 1 }),
-				None,
-				None,
-			)))?,
+			children: State::push_description(
+				|| format!("parsing children"),
+				|| {
+					Typed::from_untyped(Val::Arr(arr.slice(
+						Some(if has_attrs { 2 } else { 1 }),
+						None,
+						None,
+					)))
+				},
+			)?,
 		})
 	}
 }
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
after · crates/jrsonnet-stdlib/src/manifest/yaml.rs
1use std::{borrow::Cow, fmt::Write};23use jrsonnet_evaluator::{4	bail,5	manifest::{escape_string_json_buf, ManifestFormat},6	Result, ResultExt, State, Val,7};89pub struct YamlFormat<'s> {10	/// Padding before fields, i.e11	/// ```yaml12	/// a:13	///   b:14	/// ## <- this15	/// ```16	padding: Cow<'s, str>,17	/// Padding before array elements in objects18	/// ```yaml19	/// a:20	///   - 121	/// ## <- this22	/// ```23	arr_element_padding: Cow<'s, str>,24	/// Should yaml keys appear unescaped, when possible25	/// ```yaml26	/// "safe_key": 127	/// # vs28	/// safe_key: 129	/// ```30	quote_keys: bool,31	/// If true - then order of fields is preserved as written,32	/// instead of sorting alphabetically33	#[cfg(feature = "exp-preserve-order")]34	preserve_order: bool,35}36impl YamlFormat<'_> {37	pub fn cli(38		padding: usize,39		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,40	) -> Self {41		let padding = " ".repeat(padding);42		Self {43			padding: Cow::Owned(padding.clone()),44			arr_element_padding: Cow::Owned(padding),45			quote_keys: false,46			#[cfg(feature = "exp-preserve-order")]47			preserve_order,48		}49	}50	pub fn std_to_yaml(51		indent_array_in_object: bool,52		quote_keys: bool,53		#[cfg(feature = "exp-preserve-order")] preserve_order: bool,54	) -> Self {55		Self {56			padding: Cow::Borrowed("  "),57			arr_element_padding: Cow::Borrowed(if indent_array_in_object { "  " } else { "" }),58			quote_keys,59			#[cfg(feature = "exp-preserve-order")]60			preserve_order,61		}62	}63}64impl ManifestFormat for YamlFormat<'_> {65	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {66		manifest_yaml_ex_buf(&val, buf, &mut String::new(), self)67	}68}6970/// From <https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289>71/// With added date check72fn yaml_needs_quotes(string: &str) -> bool {73	fn need_quotes_spaces(string: &str) -> bool {74		string.starts_with(' ') || string.ends_with(' ')75	}7677	string.is_empty()78		|| need_quotes_spaces(string)79		|| string.starts_with(|c| matches!(c, '&' | '*' | '?' | '|' | '-' | '<' | '>' | '=' | '!' | '%' | '@'))80		|| string.contains(|c| matches!(c, ':' | '{' | '}' | '[' | ']' | ',' | '#' | '`' | '\"' | '\'' | '\\' | '\0'..='\x06' | '\t' | '\n' | '\r' | '\x0e'..='\x1a' | '\x1c'..='\x1f'))81		|| [82			// http://yaml.org/type/bool.html83			"yes", "Yes", "YES", "no", "No", "NO", "True", "TRUE", "true", "False", "FALSE", "false",84			"on", "On", "ON", "off", "Off", "OFF", // http://yaml.org/type/null.html85			"null", "Null", "NULL", "~",86			// > Quoted in std.jsonnet, however, in serde_yaml they were quoted:87			// > Note: 'y', 'Y', 'n', 'N', is not quoted deliberately, as in libyaml. PyYAML also parse88			// > them as string, not booleans, although it is violating the YAML 1.1 specification.89			// > See https://github.com/dtolnay/serde-yaml/pull/83#discussion_r152628088.90			"y", "Y", "n", "N",91			"-.inf", "+.inf", ".inf",92			"-", "---", ""93		].contains(&string)94		|| (string.chars().all(|c| matches!(c, '0'..='9' | '-'))95			&& string.chars().filter(|c| *c == '-').count() == 2)96		|| string.starts_with('.')97		|| string.starts_with("0x")98		|| string.parse::<i64>().is_ok()99		|| string.parse::<f64>().is_ok()100}101102#[allow(dead_code)]103fn manifest_yaml_ex(val: &Val, options: &YamlFormat<'_>) -> Result<String> {104	let mut out = String::new();105	manifest_yaml_ex_buf(val, &mut out, &mut String::new(), options)?;106	Ok(out)107}108109#[allow(clippy::too_many_lines)]110fn manifest_yaml_ex_buf(111	val: &Val,112	buf: &mut String,113	cur_padding: &mut String,114	options: &YamlFormat<'_>,115) -> Result<()> {116	match val {117		Val::Bool(v) => {118			if *v {119				buf.push_str("true");120			} else {121				buf.push_str("false");122			}123		}124		Val::Null => buf.push_str("null"),125		Val::Str(s) => {126			let s = s.clone().into_flat();127			if s.is_empty() {128				buf.push_str("\"\"");129			} else if let Some(s) = s.strip_suffix('\n') {130				buf.push('|');131				for line in s.split('\n') {132					buf.push('\n');133					buf.push_str(cur_padding);134					buf.push_str(&options.padding);135					buf.push_str(line);136				}137			} else if s.contains('\n') {138				buf.push_str("|-");139				for line in s.split('\n') {140					buf.push('\n');141					buf.push_str(cur_padding);142					buf.push_str(&options.padding);143					buf.push_str(line);144				}145			} else if !options.quote_keys && !yaml_needs_quotes(&s) {146				buf.push_str(&s);147			} else {148				escape_string_json_buf(&s, buf);149			}150		}151		Val::Num(n) => write!(buf, "{}", *n).unwrap(),152		#[cfg(feature = "exp-bigint")]153		Val::BigInt(n) => write!(buf, "{}", *n).unwrap(),154		Val::Arr(a) => {155			let mut had_items = false;156			for (i, item) in a.iter().enumerate() {157				had_items = true;158				let item = item.with_description(|| format!("elem <{i}> evaluation"))?;159				if i != 0 {160					buf.push('\n');161					buf.push_str(cur_padding);162				}163				buf.push('-');164				match &item {165					Val::Arr(a) if !a.is_empty() => {166						buf.push('\n');167						buf.push_str(cur_padding);168						buf.push_str(&options.padding);169					}170					_ => buf.push(' '),171				}172				let extra_padding = match &item {173					Val::Arr(a) => !a.is_empty(),174					Val::Obj(o) => !o.is_empty(),175					_ => false,176				};177				let prev_len = cur_padding.len();178				if extra_padding {179					cur_padding.push_str(&options.padding);180				}181				State::push_description(182					|| format!("elem <{i}> manifestification"),183					|| manifest_yaml_ex_buf(&item, buf, cur_padding, options),184				)?;185				cur_padding.truncate(prev_len);186			}187			if !had_items {188				buf.push_str("[]");189			}190		}191		Val::Obj(o) => {192			let mut had_fields = false;193			for (i, (key, value)) in o194				.iter(195					#[cfg(feature = "exp-preserve-order")]196					options.preserve_order,197				)198				.enumerate()199			{200				had_fields = true;201				let value = value.with_description(|| format!("field <{key}> evaluation"))?;202				if i != 0 {203					buf.push('\n');204					buf.push_str(cur_padding);205				}206				if !options.quote_keys && !yaml_needs_quotes(&key) {207					buf.push_str(&key);208				} else {209					escape_string_json_buf(&key, buf);210				}211				buf.push(':');212				let prev_len = cur_padding.len();213				match &value {214					Val::Arr(a) if !a.is_empty() => {215						buf.push('\n');216						buf.push_str(cur_padding);217						buf.push_str(&options.arr_element_padding);218						cur_padding.push_str(&options.arr_element_padding);219					}220					Val::Obj(o) if !o.is_empty() => {221						buf.push('\n');222						buf.push_str(cur_padding);223						buf.push_str(&options.padding);224						cur_padding.push_str(&options.padding);225					}226					_ => buf.push(' '),227				}228				State::push_description(229					|| format!("field <{key}> manifestification"),230					|| manifest_yaml_ex_buf(&value, buf, cur_padding, options),231				)?;232				cur_padding.truncate(prev_len);233			}234			if !had_fields {235				buf.push_str("{}");236			}237		}238		Val::Func(_) => bail!("tried to manifest function"),239	}240	Ok(())241}