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
4 bail,4 bail,
5 manifest::{escape_string_json_buf, ManifestFormat},5 manifest::{escape_string_json_buf, ManifestFormat},
6 val::ArrValue,6 val::ArrValue,
7 IStr, ObjValue, Result, Val,7 IStr, ObjValue, Result, ResultExt, Val, State,
8};8};
99
10pub struct TomlFormat<'s> {10pub struct TomlFormat<'s> {
106 #[cfg(feature = "exp-bigint")]106 #[cfg(feature = "exp-bigint")]
107 Val::BigInt(n) => write!(buf, "{n}").unwrap(),107 Val::BigInt(n) => write!(buf, "{n}").unwrap(),
108 Val::Arr(a) => {108 Val::Arr(a) => {
109 if a.is_empty() {
110 buf.push_str("[]");109 buf.push('[');
111 return Ok(());110
112 }111 let mut had_items = false;
113 for (i, e) in a.iter().enumerate() {112 for (i, e) in a.iter().enumerate() {
113 had_items = true;
114 let e = e?;114 let e = e.with_description(|| format!("elem <{i}> evaluation"))?;
115
115 if i != 0 {116 if i != 0 {
116 buf.push(',');117 buf.push(',');
117 } else {118 }
118 buf.push('[');
119 }
120 if inline {119 if inline {
121 buf.push(' ');120 buf.push(' ');
122 } else {121 } else {
125 buf.push_str(&options.padding);124 buf.push_str(&options.padding);
126 }125 }
126
127 State::push_description(
128 || format!("elem <{i}> manifestification"),
127 manifest_value(&e, true, buf, "", options)?;129 || manifest_value(&e, true, buf, "", options),
130 )?;
128 }131 }
132
133 if !had_items {
129 if inline {134 } else if inline {
130 buf.push(' ');135 buf.push(' ');
131 } else {136 } else {
132 buf.push('\n');137 buf.push('\n');
135 buf.push(']');140 buf.push(']');
136 }141 }
137 Val::Obj(o) => {142 Val::Obj(o) => {
138 if o.is_empty() {143 o.run_assertions()?;
139 buf.push_str("{}");
140 }
141 buf.push_str("{ ");144 buf.push('{');
145
146 let mut had_fields = false;
142 for (i, (k, v)) in o147 for (i, (k, v)) in o
143 .iter(148 .iter(
144 #[cfg(feature = "exp-preserve-order")]149 #[cfg(feature = "exp-preserve-order")]
145 options.preserve_order,150 options.preserve_order,
146 )151 )
147 .enumerate()152 .enumerate()
148 {153 {
154 had_fields = true;
149 let v = v?;155 let v = v.with_description(|| format!("field <{k}> evaluation"))?;
156
150 if i != 0 {157 if i != 0 {
151 buf.push_str(", ");158 buf.push(',');
152 }159 }
160 buf.push(' ');
161
153 escape_key_toml_buf(&k, buf);162 escape_key_toml_buf(&k, buf);
154 buf.push_str(" = ");163 buf.push_str(" = ");
164 State::push_description(
165 || format!("field <{k}> manifestification"),
155 manifest_value(&v, true, buf, "", options)?;166 || manifest_value(&v, true, buf, "", options),
167 )?;
156 }168 }
169
170 if had_fields {
171 buf.push(' ');
172 }
173
157 buf.push_str(" }");174 buf.push('}');
158 }175 }
159 Val::Null => {176 Val::Null => {
160 bail!("tried to manifest null")177 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
--- 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"),