git.delta.rocks / jrsonnet / refs/commits / 18dc4db46973

difftreelog

fix experimental features build

Yaroslav Bolyukin2024-06-11parent: #1086a51.patch.diff
in: master

3 files changed

modifiedcmds/jrsonnet/src/main.rsdiffbeforeafterboth
--- a/cmds/jrsonnet/src/main.rs
+++ b/cmds/jrsonnet/src/main.rs
@@ -186,8 +186,12 @@
 	};
 
 	let tla = opts.tla.tla_opts()?;
-	#[allow(unused_mut)]
-	let mut val = apply_tla(s, &tla, val)?;
+	#[allow(
+		// It is not redundant/unused in exp-apply
+		unused_mut,
+		clippy::redundant_clone,
+	)]
+	let mut val = apply_tla(s.clone(), &tla, val)?;
 
 	#[cfg(feature = "exp-apply")]
 	for apply in opts.input.exp_apply {
modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66	let arr = arr.to_array();67	arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {72	let mut out = ObjValueBuilder::new();73	for (k, v) in obj.iter() {74		let v = v?;75		out.field(k.clone())76			.value(func.evaluate_simple(&(k, v), false)?);77	}78	Ok(out.build())79}8081#[builtin]82pub fn builtin_flatmap(83	func: NativeFn<((Either![String, Val],), Val)>,84	arr: IndexableVal,85) -> Result<IndexableVal> {86	use std::fmt::Write;87	match arr {88		IndexableVal::Str(str) => {89			let mut out = String::new();90			for c in str.chars() {91				match func(Either2::A(c.to_string()))? {92					Val::Str(o) => write!(out, "{o}").unwrap(),93					Val::Null => continue,94					_ => bail!("in std.join all items should be strings"),95				};96			}97			Ok(IndexableVal::Str(out.into()))98		}99		IndexableVal::Arr(a) => {100			let mut out = Vec::new();101			for el in a.iter() {102				let el = el?;103				match func(Either2::B(el))? {104					Val::Arr(o) => {105						for oe in o.iter() {106							out.push(oe?);107						}108					}109					Val::Null => continue,110					_ => bail!("in std.join all items should be arrays"),111				};112			}113			Ok(IndexableVal::Arr(out.into()))114		}115	}116}117118#[builtin]119pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {120	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))121}122123#[builtin]124pub fn builtin_filter_map(125	filter_func: FuncVal,126	map_func: FuncVal,127	arr: ArrValue,128) -> Result<ArrValue> {129	Ok(builtin_filter(filter_func, arr)?.map(map_func))130}131132#[builtin]133pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {134	let mut acc = init;135	for i in arr.iter() {136		acc = func.evaluate_simple(&(acc, i?), false)?;137	}138	Ok(acc)139}140141#[builtin]142pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {143	let mut acc = init;144	for i in arr.iter().rev() {145		acc = func.evaluate_simple(&(i?, acc), false)?;146	}147	Ok(acc)148}149150#[builtin]151pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {152	if to < from {153		return Ok(ArrValue::empty());154	}155	Ok(ArrValue::range_inclusive(from, to))156}157158#[builtin]159pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {160	use std::fmt::Write;161	Ok(match sep {162		IndexableVal::Arr(joiner_items) => {163			let mut out = Vec::new();164165			let mut first = true;166			for item in arr.iter() {167				let item = item?.clone();168				if let Val::Arr(items) = item {169					if !first {170						out.reserve(joiner_items.len());171						// TODO: extend172						for item in joiner_items.iter() {173							out.push(item?);174						}175					}176					first = false;177					out.reserve(items.len());178					for item in items.iter() {179						out.push(item?);180					}181				} else if matches!(item, Val::Null) {182					continue;183				} else {184					bail!("in std.join all items should be arrays");185				}186			}187188			IndexableVal::Arr(out.into())189		}190		IndexableVal::Str(sep) => {191			let mut out = String::new();192193			let mut first = true;194			for item in arr.iter() {195				let item = item?.clone();196				if let Val::Str(item) = item {197					if !first {198						out += &sep;199					}200					first = false;201					write!(out, "{item}").unwrap();202				} else if matches!(item, Val::Null) {203					continue;204				} else {205					bail!("in std.join all items should be strings");206				}207			}208209			IndexableVal::Str(out.into())210		}211	})212}213214#[builtin]215pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {216	builtin_join(217		IndexableVal::Str("\n".into()),218		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),219	)220}221222#[builtin]223pub fn builtin_resolve_path(f: String, r: String) -> String {224	let Some(pos) = f.rfind('/') else {225		return r;226	};227	format!("{}{}", &f[..=pos], r)228}229230pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {231	use std::fmt::Write;232	match arr {233		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),234		IndexableVal::Arr(arr) => {235			for ele in arr.iter() {236				let indexable = IndexableVal::from_untyped(ele?)?;237				deep_join_inner(out, indexable)?;238			}239		}240	}241	Ok(())242}243244#[builtin]245pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {246	let mut out = String::new();247	deep_join_inner(&mut out, arr)?;248	Ok(out)249}250251#[builtin]252pub fn builtin_reverse(arr: ArrValue) -> ArrValue {253	arr.reversed()254}255256#[builtin]257pub fn builtin_any(arr: ArrValue) -> Result<bool> {258	for v in arr.iter() {259		let v = bool::from_untyped(v?)?;260		if v {261			return Ok(true);262		}263	}264	Ok(false)265}266267#[builtin]268pub fn builtin_all(arr: ArrValue) -> Result<bool> {269	for v in arr.iter() {270		let v = bool::from_untyped(v?)?;271		if !v {272			return Ok(false);273		}274	}275	Ok(true)276}277278#[builtin]279pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {280	match arr {281		IndexableVal::Str(str) => {282			let x: IStr = IStr::from_untyped(x)?;283			Ok(!x.is_empty() && str.contains(&*x))284		}285		IndexableVal::Arr(a) => {286			for item in a.iter() {287				let item = item?;288				if equals(&item, &x)? {289					return Ok(true);290				}291			}292			Ok(false)293		}294	}295}296297#[builtin]298pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {299	let mut out = Vec::new();300	for (i, ele) in arr.iter().enumerate() {301		let ele = ele?;302		if equals(&ele, &value)? {303			out.push(i);304		}305	}306	Ok(out)307}308309#[builtin]310pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {311	builtin_member(arr, elem)312}313314#[builtin]315pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {316	let mut count = 0;317	for item in arr.iter() {318		if equals(&item?, &x)? {319			count += 1;320		}321	}322	Ok(count)323}324325#[builtin]326pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {327	if arr.is_empty() {328		return eval_on_empty(onEmpty);329	}330	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)331}332333#[builtin]334pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {335	let newArrLeft = arr.clone().slice(None, Some(at), None);336	let newArrRight = arr.slice(Some(at + 1), None, None);337338	Ok(ArrValue::extended(newArrLeft, newArrRight))339}340341#[builtin]342pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {343	for (index, item) in arr.iter().enumerate() {344		if equals(&item?, &elem)? {345			return builtin_remove_at(arr.clone(), index as i32);346		}347	}348	Ok(arr)349}350351#[builtin]352pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {353	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {354		if values.len() == 1 {355			return values[0].clone();356		} else if values.len() == 2 {357			return ArrValue::extended(values[0].clone(), values[1].clone());358		}359		let (a, b) = values.split_at(values.len() / 2);360		ArrValue::extended(flatten_inner(a), flatten_inner(b))361	}362	if arrs.is_empty() {363		return ArrValue::empty();364	} else if arrs.len() == 1 {365		return arrs.into_iter().next().expect("single");366	}367	flatten_inner(&arrs)368}369370#[builtin]371pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {372	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {373		match value {374			Val::Arr(arr) => {375				for ele in arr.iter() {376					process(ele?, out)?;377				}378			}379			_ => out.push(value),380		}381		Ok(())382	}383	let mut out = Vec::new();384	process(value, &mut out)?;385	Ok(out)386}387388#[builtin]389pub fn builtin_prune(390	a: Val,391392	#[default(false)]393	#[cfg(feature = "exp-preserve-order")]394	preserve_order: bool,395) -> Result<Val> {396	fn is_content(val: &Val) -> bool {397		match val {398			Val::Null => false,399			Val::Arr(a) => !a.is_empty(),400			Val::Obj(o) => !o.is_empty(),401			_ => true,402		}403	}404	Ok(match a {405		Val::Arr(a) => {406			let mut out = Vec::new();407			for (i, ele) in a.iter().enumerate() {408				let ele = ele409					.and_then(|v| {410						builtin_prune(411							v,412							#[cfg(feature = "exp-preserve-order")]413							preserve_order,414						)415					})416					.with_description(|| format!("elem <{i}> pruning"))?;417				if is_content(&ele) {418					out.push(ele);419				}420			}421			Val::Arr(ArrValue::eager(out))422		}423		Val::Obj(o) => {424			let mut out = ObjValueBuilder::new();425			for (name, value) in o.iter(426				#[cfg(feature = "exp-preserve-order")]427				preserve_order,428			) {429				let value = value430					.and_then(|v| {431						builtin_prune(432							v,433							#[cfg(feature = "exp-preserve-order")]434							preserve_order,435						)436					})437					.with_description(|| format!("field <{name}> pruning"))?;438				if !is_content(&value) {439					continue;440				}441				out.field(name).value(value);442			}443			Val::Obj(out.build())444		}445		_ => a,446	})447}
after · crates/jrsonnet-stdlib/src/arrays.rs
1#![allow(non_snake_case)]23use jrsonnet_evaluator::{4	bail,5	function::{builtin, FuncVal},6	runtime_error,7	typed::{BoundedI32, BoundedUsize, Either2, NativeFn, Typed},8	val::{equals, ArrValue, IndexableVal},9	Either, IStr, ObjValue, ObjValueBuilder, Result, ResultExt, Thunk, Val,10};1112pub fn eval_on_empty(on_empty: Option<Thunk<Val>>) -> Result<Val> {13	if let Some(on_empty) = on_empty {14		on_empty.evaluate()15	} else {16		bail!("expected non-empty array")17	}18}1920#[builtin]21pub fn builtin_make_array(sz: BoundedI32<0, { i32::MAX }>, func: FuncVal) -> Result<ArrValue> {22	if *sz == 0 {23		return Ok(ArrValue::empty());24	}25	func.evaluate_trivial().map_or_else(26		|| Ok(ArrValue::range_exclusive(0, *sz).map(func)),27		|trivial| {28			let mut out = Vec::with_capacity(*sz as usize);29			for _ in 0..*sz {30				out.push(trivial.clone());31			}32			Ok(ArrValue::eager(out))33		},34	)35}3637#[builtin]38pub fn builtin_repeat(what: Either![IStr, ArrValue], count: usize) -> Result<Val> {39	Ok(match what {40		Either2::A(s) => Val::string(s.repeat(count)),41		Either2::B(arr) => Val::Arr(42			ArrValue::repeated(arr, count)43				.ok_or_else(|| runtime_error!("repeated length overflow"))?,44		),45	})46}4748#[builtin]49pub fn builtin_slice(50	indexable: IndexableVal,51	index: Option<i32>,52	end: Option<i32>,53	step: Option<BoundedUsize<1, { i32::MAX as usize }>>,54) -> Result<Val> {55	indexable.slice(index, end, step).map(Val::from)56}5758#[builtin]59pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {60	let arr = arr.to_array();61	arr.map(func)62}6364#[builtin]65pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {66	let arr = arr.to_array();67	arr.map_with_index(func)68}6970#[builtin]71pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {72	let mut out = ObjValueBuilder::new();73	for (k, v) in obj.iter(74		// Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).75		// The thrown error might be different, but jsonnet76		// does not specify the evaluation order.77		#[cfg(feature = "exp-preserve-order")]78		true,79	) {80		let v = v?;81		out.field(k.clone())82			.value(func.evaluate_simple(&(k, v), false)?);83	}84	Ok(out.build())85}8687#[builtin]88pub fn builtin_flatmap(89	func: NativeFn<((Either![String, Val],), Val)>,90	arr: IndexableVal,91) -> Result<IndexableVal> {92	use std::fmt::Write;93	match arr {94		IndexableVal::Str(str) => {95			let mut out = String::new();96			for c in str.chars() {97				match func(Either2::A(c.to_string()))? {98					Val::Str(o) => write!(out, "{o}").unwrap(),99					Val::Null => continue,100					_ => bail!("in std.join all items should be strings"),101				};102			}103			Ok(IndexableVal::Str(out.into()))104		}105		IndexableVal::Arr(a) => {106			let mut out = Vec::new();107			for el in a.iter() {108				let el = el?;109				match func(Either2::B(el))? {110					Val::Arr(o) => {111						for oe in o.iter() {112							out.push(oe?);113						}114					}115					Val::Null => continue,116					_ => bail!("in std.join all items should be arrays"),117				};118			}119			Ok(IndexableVal::Arr(out.into()))120		}121	}122}123124#[builtin]125pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {126	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))127}128129#[builtin]130pub fn builtin_filter_map(131	filter_func: FuncVal,132	map_func: FuncVal,133	arr: ArrValue,134) -> Result<ArrValue> {135	Ok(builtin_filter(filter_func, arr)?.map(map_func))136}137138#[builtin]139pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {140	let mut acc = init;141	for i in arr.iter() {142		acc = func.evaluate_simple(&(acc, i?), false)?;143	}144	Ok(acc)145}146147#[builtin]148pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {149	let mut acc = init;150	for i in arr.iter().rev() {151		acc = func.evaluate_simple(&(i?, acc), false)?;152	}153	Ok(acc)154}155156#[builtin]157pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {158	if to < from {159		return Ok(ArrValue::empty());160	}161	Ok(ArrValue::range_inclusive(from, to))162}163164#[builtin]165pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {166	use std::fmt::Write;167	Ok(match sep {168		IndexableVal::Arr(joiner_items) => {169			let mut out = Vec::new();170171			let mut first = true;172			for item in arr.iter() {173				let item = item?.clone();174				if let Val::Arr(items) = item {175					if !first {176						out.reserve(joiner_items.len());177						// TODO: extend178						for item in joiner_items.iter() {179							out.push(item?);180						}181					}182					first = false;183					out.reserve(items.len());184					for item in items.iter() {185						out.push(item?);186					}187				} else if matches!(item, Val::Null) {188					continue;189				} else {190					bail!("in std.join all items should be arrays");191				}192			}193194			IndexableVal::Arr(out.into())195		}196		IndexableVal::Str(sep) => {197			let mut out = String::new();198199			let mut first = true;200			for item in arr.iter() {201				let item = item?.clone();202				if let Val::Str(item) = item {203					if !first {204						out += &sep;205					}206					first = false;207					write!(out, "{item}").unwrap();208				} else if matches!(item, Val::Null) {209					continue;210				} else {211					bail!("in std.join all items should be strings");212				}213			}214215			IndexableVal::Str(out.into())216		}217	})218}219220#[builtin]221pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {222	builtin_join(223		IndexableVal::Str("\n".into()),224		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),225	)226}227228#[builtin]229pub fn builtin_resolve_path(f: String, r: String) -> String {230	let Some(pos) = f.rfind('/') else {231		return r;232	};233	format!("{}{}", &f[..=pos], r)234}235236pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {237	use std::fmt::Write;238	match arr {239		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),240		IndexableVal::Arr(arr) => {241			for ele in arr.iter() {242				let indexable = IndexableVal::from_untyped(ele?)?;243				deep_join_inner(out, indexable)?;244			}245		}246	}247	Ok(())248}249250#[builtin]251pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {252	let mut out = String::new();253	deep_join_inner(&mut out, arr)?;254	Ok(out)255}256257#[builtin]258pub fn builtin_reverse(arr: ArrValue) -> ArrValue {259	arr.reversed()260}261262#[builtin]263pub fn builtin_any(arr: ArrValue) -> Result<bool> {264	for v in arr.iter() {265		let v = bool::from_untyped(v?)?;266		if v {267			return Ok(true);268		}269	}270	Ok(false)271}272273#[builtin]274pub fn builtin_all(arr: ArrValue) -> Result<bool> {275	for v in arr.iter() {276		let v = bool::from_untyped(v?)?;277		if !v {278			return Ok(false);279		}280	}281	Ok(true)282}283284#[builtin]285pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {286	match arr {287		IndexableVal::Str(str) => {288			let x: IStr = IStr::from_untyped(x)?;289			Ok(!x.is_empty() && str.contains(&*x))290		}291		IndexableVal::Arr(a) => {292			for item in a.iter() {293				let item = item?;294				if equals(&item, &x)? {295					return Ok(true);296				}297			}298			Ok(false)299		}300	}301}302303#[builtin]304pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {305	let mut out = Vec::new();306	for (i, ele) in arr.iter().enumerate() {307		let ele = ele?;308		if equals(&ele, &value)? {309			out.push(i);310		}311	}312	Ok(out)313}314315#[builtin]316pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {317	builtin_member(arr, elem)318}319320#[builtin]321pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {322	let mut count = 0;323	for item in arr.iter() {324		if equals(&item?, &x)? {325			count += 1;326		}327	}328	Ok(count)329}330331#[builtin]332pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {333	if arr.is_empty() {334		return eval_on_empty(onEmpty);335	}336	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)337}338339#[builtin]340pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {341	let newArrLeft = arr.clone().slice(None, Some(at), None);342	let newArrRight = arr.slice(Some(at + 1), None, None);343344	Ok(ArrValue::extended(newArrLeft, newArrRight))345}346347#[builtin]348pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {349	for (index, item) in arr.iter().enumerate() {350		if equals(&item?, &elem)? {351			return builtin_remove_at(arr.clone(), index as i32);352		}353	}354	Ok(arr)355}356357#[builtin]358pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {359	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {360		if values.len() == 1 {361			return values[0].clone();362		} else if values.len() == 2 {363			return ArrValue::extended(values[0].clone(), values[1].clone());364		}365		let (a, b) = values.split_at(values.len() / 2);366		ArrValue::extended(flatten_inner(a), flatten_inner(b))367	}368	if arrs.is_empty() {369		return ArrValue::empty();370	} else if arrs.len() == 1 {371		return arrs.into_iter().next().expect("single");372	}373	flatten_inner(&arrs)374}375376#[builtin]377pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {378	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {379		match value {380			Val::Arr(arr) => {381				for ele in arr.iter() {382					process(ele?, out)?;383				}384			}385			_ => out.push(value),386		}387		Ok(())388	}389	let mut out = Vec::new();390	process(value, &mut out)?;391	Ok(out)392}393394#[builtin]395pub fn builtin_prune(396	a: Val,397398	#[default(false)]399	#[cfg(feature = "exp-preserve-order")]400	preserve_order: bool,401) -> Result<Val> {402	fn is_content(val: &Val) -> bool {403		match val {404			Val::Null => false,405			Val::Arr(a) => !a.is_empty(),406			Val::Obj(o) => !o.is_empty(),407			_ => true,408		}409	}410	Ok(match a {411		Val::Arr(a) => {412			let mut out = Vec::new();413			for (i, ele) in a.iter().enumerate() {414				let ele = ele415					.and_then(|v| {416						builtin_prune(417							v,418							#[cfg(feature = "exp-preserve-order")]419							preserve_order,420						)421					})422					.with_description(|| format!("elem <{i}> pruning"))?;423				if is_content(&ele) {424					out.push(ele);425				}426			}427			Val::Arr(ArrValue::eager(out))428		}429		Val::Obj(o) => {430			let mut out = ObjValueBuilder::new();431			for (name, value) in o.iter(432				#[cfg(feature = "exp-preserve-order")]433				preserve_order,434			) {435				let value = value436					.and_then(|v| {437						builtin_prune(438							v,439							#[cfg(feature = "exp-preserve-order")]440							preserve_order,441						)442					})443					.with_description(|| format!("field <{name}> pruning"))?;444				if !is_content(&value) {445					continue;446				}447				out.field(name).value(value);448			}449			Val::Obj(out.build())450		}451		_ => a,452	})453}
modifiedcrates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -147,7 +147,14 @@
 	if equals(&a, &b)? {
 		return Ok(true);
 	}
-	let format = JsonFormat::std_to_json("  ".to_owned(), "\n", ": ");
+	// TODO: Use debug output format
+	let format = JsonFormat::std_to_json(
+		"  ".to_owned(),
+		"\n",
+		": ",
+		#[cfg(feature = "exp-preserve-order")]
+		true,
+	);
 	let a = a.manifest(&format).description("<a> manifestification")?;
 	let b = b.manifest(&format).description("<b> manifestification")?;
 	bail!("assertion failed: A != B\nA: {a}\nB: {b}")
@@ -161,8 +168,30 @@
 	let Some(target) = target.as_obj() else {
 		return Ok(Val::Obj(patch));
 	};
-	let target_fields = target.fields().into_iter().collect::<BTreeSet<IStr>>();
-	let patch_fields = patch.fields().into_iter().collect::<BTreeSet<IStr>>();
+	let target_fields = target
+		.fields(
+			// FIXME: Makes no sense to preserve order for BTreeSet, it would be better to use IndexSet here?
+			// But IndexSet won't allow fast ordered union...
+			// // Makes sense to preserve source ordering where possible.
+			// // May affect evaluation order, but it is not specified by jsonnet spec.
+			// #[cfg(feature = "exp-preserve-order")]
+			// true,
+			#[cfg(feature = "exp-preserve-order")]
+			false,
+		)
+		.into_iter()
+		.collect::<BTreeSet<IStr>>();
+	let patch_fields = patch
+		.fields(
+			// No need to look at the patch field order, I think?
+			// New fields (that will be appended at the end) will be alphabeticaly-ordered,
+			// but it is fine for jsonpatch, I don't think people write jsonpatch in jsonnet,
+			// when they can use mixins.
+			#[cfg(feature = "exp-preserve-order")]
+			false,
+		)
+		.into_iter()
+		.collect::<BTreeSet<IStr>>();
 
 	let mut out = ObjValueBuilder::new();
 	for field in target_fields.union(&patch_fields) {