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
after · crates/jrsonnet-stdlib/src/manifest/xml.rs
1use jrsonnet_evaluator::{2	bail,3	manifest::{ManifestFormat, ToStringFormat},4	typed::{ComplexValType, Either2, Either4, Typed, ValType},5	val::{ArrValue, IndexableVal},6	Either, ObjValue, Result, ResultExt, Val, State,7};89pub struct XmlJsonmlFormat {10	force_closing: bool,11}12impl XmlJsonmlFormat {13	pub fn std_to_xml() -> Self {14		Self {15			force_closing: true,16		}17	}18	pub fn cli() -> Self {19		Self {20			force_closing: false,21		}22	}23}2425enum JSONMLValue {26	Tag {27		tag: String,28		attrs: ObjValue,29		children: Vec<JSONMLValue>,30	},31	String(String),32}33impl Typed for JSONMLValue {34	const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);3536	fn into_untyped(_typed: Self) -> Result<Val> {37		unreachable!("not used, reserved for parseXML?")38	}3940	fn from_untyped(untyped: Val) -> Result<Self> {41		let val = <Either![ArrValue, String]>::from_untyped(untyped)42			.with_description(|| format!("parsing JSONML value (an array or string)"))?;43		let arr = match val {44			Either2::A(a) => a,45			Either2::B(s) => return Ok(Self::String(s)),46		};47		if arr.len() < 1 {48			bail!("JSONML value should have tag (array length should be >=1)");49		};50		let tag = String::from_untyped(51			arr.get(0)52				.with_description(|| "getting JSONML tag")?53				.expect("length checked"),54		)55		.with_description(|| format!("parsing JSONML tag"))?;5657		let (has_attrs, attrs) = if arr.len() >= 2 {58			let maybe_attrs = arr59				.get(1)60				.with_description(|| "getting JSONML attrs")?61				.expect("length checked");62			if let Val::Obj(attrs) = maybe_attrs {63				(true, attrs)64			} else {65				(false, ObjValue::new_empty())66			}67		} else {68			(false, ObjValue::new_empty())69		};70		Ok(Self::Tag {71			tag,72			attrs,73			children: State::push_description(74				|| format!("parsing children"),75				|| {76					Typed::from_untyped(Val::Arr(arr.slice(77						Some(if has_attrs { 2 } else { 1 }),78						None,79						None,80					)))81				},82			)?,83		})84	}85}8687impl ManifestFormat for XmlJsonmlFormat {88	fn manifest_buf(&self, val: Val, buf: &mut String) -> Result<()> {89		let val = JSONMLValue::from_untyped(val).with_description(|| "parsing JSONML value")?;90		manifest_jsonml(&val, buf, self)91	}92}9394fn manifest_jsonml(v: &JSONMLValue, buf: &mut String, opts: &XmlJsonmlFormat) -> Result<()> {95	match v {96		JSONMLValue::Tag {97			tag,98			attrs,99			children,100		} => {101			let has_children = !children.is_empty();102			buf.push('<');103			buf.push_str(&tag);104			attrs.run_assertions()?;105			for (key, value) in attrs.iter(106				// Not much sense to preserve order here107				#[cfg(feature = "exp-preserve-order")]108				false,109			) {110				buf.push(' ');111				buf.push_str(&key);112				buf.push('=');113				buf.push('"');114				let value = value?;115				let value = if let Val::Str(s) = value {116					s.to_string()117				} else {118					ToStringFormat.manifest(value)?119				};120				escape_string_xml_buf(&value, buf);121				buf.push('"');122			}123			if !has_children && !opts.force_closing {124				buf.push('/');125			}126			buf.push('>');127			for child in children {128				manifest_jsonml(&child, buf, opts)?;129			}130			if has_children || opts.force_closing {131				buf.push('<');132				buf.push('/');133				buf.push_str(&tag);134				buf.push('>');135			}136			Ok(())137		}138		JSONMLValue::String(s) => {139			escape_string_xml_buf(s, buf);140			Ok(())141		}142	}143}144145pub fn escape_string_xml(str: &str) -> String {146	let mut out = String::new();147	escape_string_xml_buf(str, &mut out);148	out149}150151fn escape_string_xml_buf(str: &str, out: &mut String) {152	if str.is_empty() {153		return;154	}155	let mut remaining = str;156157	let mut found = false;158	while let Some(position) = remaining159		.bytes()160		.position(|c| matches!(c, b'<' | b'>' | b'&' | b'"' | b'\''))161	{162		found = true;163164		let (plain, rem) = remaining.split_at(position);165		out.push_str(plain);166167		out.push_str(match rem.as_bytes()[0] {168			b'<' => "&lt;",169			b'>' => "&gt;",170			b'&' => "&amp;",171			b'"' => "&quot;",172			b'\'' => "&apos;",173			_ => unreachable!("position() searches for those matches"),174		});175176		remaining = &rem[1..];177	}178	if !found {179		// No match - no escapes required180		out.push_str(&str);181		return;182	}183	out.push_str(&remaining);184}
modifiedcrates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -3,7 +3,7 @@
 use jrsonnet_evaluator::{
 	bail,
 	manifest::{escape_string_json_buf, ManifestFormat},
-	Result, Val,
+	Result, ResultExt, State, Val,
 };
 
 pub struct YamlFormat<'s> {
@@ -152,80 +152,87 @@
 		#[cfg(feature = "exp-bigint")]
 		Val::BigInt(n) => write!(buf, "{}", *n).unwrap(),
 		Val::Arr(a) => {
-			if a.is_empty() {
-				buf.push_str("[]");
-			} else {
-				for (i, item) in a.iter().enumerate() {
-					if i != 0 {
+			let mut had_items = false;
+			for (i, item) in a.iter().enumerate() {
+				had_items = true;
+				let item = item.with_description(|| format!("elem <{i}> evaluation"))?;
+				if i != 0 {
+					buf.push('\n');
+					buf.push_str(cur_padding);
+				}
+				buf.push('-');
+				match &item {
+					Val::Arr(a) if !a.is_empty() => {
 						buf.push('\n');
 						buf.push_str(cur_padding);
-					}
-					let item = item?;
-					buf.push('-');
-					match &item {
-						Val::Arr(a) if !a.is_empty() => {
-							buf.push('\n');
-							buf.push_str(cur_padding);
-							buf.push_str(&options.padding);
-						}
-						_ => buf.push(' '),
-					}
-					let extra_padding = match &item {
-						Val::Arr(a) => !a.is_empty(),
-						Val::Obj(o) => !o.is_empty(),
-						_ => false,
-					};
-					let prev_len = cur_padding.len();
-					if extra_padding {
-						cur_padding.push_str(&options.padding);
+						buf.push_str(&options.padding);
 					}
-					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
-					cur_padding.truncate(prev_len);
+					_ => buf.push(' '),
 				}
+				let extra_padding = match &item {
+					Val::Arr(a) => !a.is_empty(),
+					Val::Obj(o) => !o.is_empty(),
+					_ => false,
+				};
+				let prev_len = cur_padding.len();
+				if extra_padding {
+					cur_padding.push_str(&options.padding);
+				}
+				State::push_description(
+					|| format!("elem <{i}> manifestification"),
+					|| manifest_yaml_ex_buf(&item, buf, cur_padding, options),
+				)?;
+				cur_padding.truncate(prev_len);
 			}
+			if !had_items {
+				buf.push_str("[]");
+			}
 		}
 		Val::Obj(o) => {
-			if o.is_empty() {
-				buf.push_str("{}");
-			} else {
-				for (i, key) in o
-					.fields(
-						#[cfg(feature = "exp-preserve-order")]
-						options.preserve_order,
-					)
-					.iter()
-					.enumerate()
-				{
-					if i != 0 {
+			let mut had_fields = false;
+			for (i, (key, value)) in o
+				.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('\n');
+					buf.push_str(cur_padding);
+				}
+				if !options.quote_keys && !yaml_needs_quotes(&key) {
+					buf.push_str(&key);
+				} else {
+					escape_string_json_buf(&key, buf);
+				}
+				buf.push(':');
+				let prev_len = cur_padding.len();
+				match &value {
+					Val::Arr(a) if !a.is_empty() => {
 						buf.push('\n');
 						buf.push_str(cur_padding);
+						buf.push_str(&options.arr_element_padding);
+						cur_padding.push_str(&options.arr_element_padding);
 					}
-					if !options.quote_keys && !yaml_needs_quotes(key) {
-						buf.push_str(key);
-					} else {
-						escape_string_json_buf(key, buf);
+					Val::Obj(o) if !o.is_empty() => {
+						buf.push('\n');
+						buf.push_str(cur_padding);
+						buf.push_str(&options.padding);
+						cur_padding.push_str(&options.padding);
 					}
-					buf.push(':');
-					let prev_len = cur_padding.len();
-					let item = o.get(key.clone())?.expect("field exists");
-					match &item {
-						Val::Arr(a) if !a.is_empty() => {
-							buf.push('\n');
-							buf.push_str(cur_padding);
-							buf.push_str(&options.arr_element_padding);
-							cur_padding.push_str(&options.arr_element_padding);
-						}
-						Val::Obj(o) if !o.is_empty() => {
-							buf.push('\n');
-							buf.push_str(cur_padding);
-							buf.push_str(&options.padding);
-							cur_padding.push_str(&options.padding);
-						}
-						_ => buf.push(' '),
-					}
-					manifest_yaml_ex_buf(&item, buf, cur_padding, options)?;
-					cur_padding.truncate(prev_len);
+					_ => buf.push(' '),
 				}
+				State::push_description(
+					|| format!("field <{key}> manifestification"),
+					|| manifest_yaml_ex_buf(&value, buf, cur_padding, options),
+				)?;
+				cur_padding.truncate(prev_len);
+			}
+			if !had_fields {
+				buf.push_str("{}");
 			}
 		}
 		Val::Func(_) => bail!("tried to manifest function"),