difftreelog
feat add description stacktrace frames for all formats
in: master
5 files changed
crates/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');
}
}
crates/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>>()
}
}
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth1use std::borrow::Cow;23use jrsonnet_evaluator::{4 bail,5 manifest::{escape_string_json_buf, ManifestFormat},6 val::ArrValue,7 IStr, ObjValue, Result, Val,8};910pub struct TomlFormat<'s> {11 /// Padding before fields, i.e12 /// ```toml13 /// [a]14 /// b = 115 /// ## <- this16 /// ```17 padding: Cow<'s, str>,18 /// Do not emit sections for objects, consisting only from sections:19 /// ```toml20 /// # false21 /// [a]22 /// [a.b]23 ///24 /// # true25 /// [a.b]26 /// ```27 skip_empty_sections: bool,28 /// If true - then order of fields is preserved as written,29 /// instead of sorting alphabetically30 #[cfg(feature = "exp-preserve-order")]31 preserve_order: bool,32}33impl TomlFormat<'_> {34 pub fn cli(35 padding: usize,36 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,37 ) -> Self {38 let padding = " ".repeat(padding);39 Self {40 padding: Cow::Owned(padding),41 skip_empty_sections: true,42 #[cfg(feature = "exp-preserve-order")]43 preserve_order,44 }45 }46 pub fn std_to_toml(47 padding: String,48 #[cfg(feature = "exp-preserve-order")] preserve_order: bool,49 ) -> Self {50 Self {51 padding: Cow::Owned(padding),52 skip_empty_sections: false,53 #[cfg(feature = "exp-preserve-order")]54 preserve_order,55 }56 }57}5859fn bare_allowed(s: &str) -> bool {60 s.bytes()61 .all(|c| matches!(c, b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-'))62}6364fn escape_key_toml_buf(key: &str, buf: &mut String) {65 if bare_allowed(key) {66 buf.push_str(key);67 } else {68 escape_string_json_buf(key, buf);69 }70}7172fn is_section(val: &Val) -> Result<bool> {73 Ok(match val {74 Val::Arr(a) => {75 if a.is_empty() {76 return Ok(false);77 }78 for e in a.iter() {79 let e = e?;80 if !matches!(e, Val::Obj(_)) {81 return Ok(false);82 }83 }84 true85 }86 Val::Obj(_) => true,87 _ => false,88 })89}9091fn manifest_value(92 val: &Val,93 inline: bool,94 buf: &mut String,95 cur_padding: &str,96 options: &TomlFormat<'_>,97) -> Result<()> {98 use std::fmt::Write;99 match val {100 Val::Bool(true) => buf.push_str("true"),101 Val::Bool(false) => buf.push_str("false"),102 Val::Str(s) => {103 escape_string_json_buf(&s.clone().into_flat(), buf);104 }105 Val::Num(n) => write!(buf, "{n}").unwrap(),106 #[cfg(feature = "exp-bigint")]107 Val::BigInt(n) => write!(buf, "{n}").unwrap(),108 Val::Arr(a) => {109 if a.is_empty() {110 buf.push_str("[]");111 return Ok(());112 }113 for (i, e) in a.iter().enumerate() {114 let e = e?;115 if i != 0 {116 buf.push(',');117 } else {118 buf.push('[');119 }120 if inline {121 buf.push(' ');122 } else {123 buf.push('\n');124 buf.push_str(cur_padding);125 buf.push_str(&options.padding);126 }127 manifest_value(&e, true, buf, "", options)?;128 }129 if inline {130 buf.push(' ');131 } else {132 buf.push('\n');133 buf.push_str(cur_padding);134 }135 buf.push(']');136 }137 Val::Obj(o) => {138 if o.is_empty() {139 buf.push_str("{}");140 }141 buf.push_str("{ ");142 for (i, (k, v)) in o143 .iter(144 #[cfg(feature = "exp-preserve-order")]145 options.preserve_order,146 )147 .enumerate()148 {149 let v = v?;150 if i != 0 {151 buf.push_str(", ");152 }153 escape_key_toml_buf(&k, buf);154 buf.push_str(" = ");155 manifest_value(&v, true, buf, "", options)?;156 }157 buf.push_str(" }");158 }159 Val::Null => {160 bail!("tried to manifest null")161 }162 Val::Func(_) => {163 bail!("tried to manifest function")164 }165 }166 Ok(())167}168169fn manifest_table_internal(170 obj: &ObjValue,171 path: &mut Vec<IStr>,172 buf: &mut String,173 cur_padding: &mut String,174 options: &TomlFormat<'_>,175) -> Result<()> {176 let mut sections = Vec::new();177 let mut first = true;178 for (key, value) in obj.iter(179 #[cfg(feature = "exp-preserve-order")]180 options.preserve_order,181 ) {182 let value = value?;183 if is_section(&value)? {184 sections.push((key, value));185 } else {186 if !first {187 buf.push('\n');188 }189 first = false;190 buf.push_str(cur_padding);191 escape_key_toml_buf(&key, buf);192 buf.push_str(" = ");193 manifest_value(&value, false, buf, cur_padding, options)?;194 }195 }196 for (k, v) in sections {197 if !first {198 buf.push_str("\n\n");199 }200 first = false;201 path.push(k);202 match v {203 Val::Obj(obj) => manifest_table(&obj, path, buf, cur_padding, options)?,204 Val::Arr(arr) => manifest_table_array(&arr, path, buf, cur_padding, options)?,205 _ => unreachable!("iterating over sections"),206 }207 path.pop();208 }209 Ok(())210}211212fn manifest_table(213 obj: &ObjValue,214 path: &mut Vec<IStr>,215 buf: &mut String,216 cur_padding: &mut String,217 options: &TomlFormat<'_>,218) -> Result<()> {219 if options.skip_empty_sections220 && !obj.is_empty()221 && obj222 .iter(223 #[cfg(feature = "exp-preserve-order")]224 false,225 )226 .try_fold(true, |c, (_, v)| Ok(c && is_section(&v?)?) as Result<bool>)?227 {228 manifest_table_internal(obj, path, buf, cur_padding, options)?;229 return Ok(());230 }231 buf.push_str(cur_padding);232 buf.push('[');233 for (i, k) in path.iter().enumerate() {234 if i != 0 {235 buf.push('.');236 }237 escape_key_toml_buf(k, buf);238 }239 buf.push(']');240 if obj.is_empty() {241 return Ok(());242 }243 buf.push('\n');244 let prev_len = cur_padding.len();245 cur_padding.push_str(&options.padding);246 manifest_table_internal(obj, path, buf, cur_padding, options)?;247 cur_padding.truncate(prev_len);248 Ok(())249}250fn manifest_table_array(251 arr: &ArrValue,252 path: &mut Vec<IStr>,253 buf: &mut String,254 cur_padding: &mut String,255 options: &TomlFormat<'_>,256) -> Result<()> {257 let mut formatted_path = String::new();258 {259 formatted_path.push_str(cur_padding);260 formatted_path.push_str("[[");261 for (i, k) in path.iter().enumerate() {262 if i != 0 {263 formatted_path.push('.');264 }265 escape_key_toml_buf(k, &mut formatted_path);266 }267 formatted_path.push_str("]]");268 }269 let prev_len = cur_padding.len();270 cur_padding.push_str(&options.padding);271 for (i, e) in arr.iter().enumerate() {272 let obj = e.expect("already tested").as_obj().expect("already tested");273 if i != 0 {274 buf.push_str("\n\n");275 }276 buf.push_str(&formatted_path);277 if obj.is_empty() {278 continue;279 }280 buf.push('\n');281 manifest_table_internal(&obj, path, buf, cur_padding, options)?;282 }283 cur_padding.truncate(prev_len);284 Ok(())285}286287impl ManifestFormat for TomlFormat<'_> {288 fn manifest_buf(&self, val: Val, buf: &mut String) -> jrsonnet_evaluator::Result<()> {289 match val {290 Val::Obj(obj) => {291 manifest_table_internal(&obj, &mut Vec::new(), buf, &mut String::new(), self)292 }293 _ => bail!("toml body should be object"),294 }295 }296}crates/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,
+ )))
+ },
+ )?,
})
}
}
crates/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"),