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

difftreelog

fix accept null as std.slice argument/in slicing syntax

Yaroslav Bolyukin2024-11-03parent: #7160d47.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -655,11 +655,11 @@
 				desc: &'static str,
 			) -> Result<Option<T>> {
 				if let Some(value) = expr {
-					Ok(Some(in_frame(
+					Ok(in_frame(
 						loc,
 						|| format!("slice {desc}"),
-						|| T::from_untyped(evaluate(ctx.clone(), value)?),
-					)?))
+						|| <Option<T>>::from_untyped(evaluate(ctx.clone(), value)?),
+					)?)
 				} else {
 					Ok(None)
 				}
modifiedcrates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -655,6 +655,26 @@
 	}
 }
 
+impl<T> Typed for Option<T>
+where
+	T: Typed,
+{
+	const TYPE: &'static ComplexValType =
+		&ComplexValType::UnionRef(&[&ComplexValType::Simple(ValType::Null), T::TYPE]);
+
+	fn into_untyped(typed: Self) -> Result<Val> {
+		typed.map_or_else(|| Ok(Val::Null), |v| T::into_untyped(v))
+	}
+
+	fn from_untyped(untyped: Val) -> Result<Self> {
+		if matches!(untyped, Val::Null) {
+			Ok(None)
+		} else {
+			T::from_untyped(untyped).map(Some)
+		}
+	}
+}
+
 pub struct NativeFn<D: NativeDesc>(D::Value);
 impl<D: NativeDesc> Deref for NativeFn<D> {
 	type Target = D::Value;
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		// 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}
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<Option<i32>>,52	end: Option<Option<i32>>,53	step: Option<Option<BoundedUsize<1, { i32::MAX as usize }>>>,54) -> Result<Val> {55	indexable56		.slice(index.flatten(), end.flatten(), step.flatten())57		.map(Val::from)58}5960#[builtin]61pub fn builtin_map(func: FuncVal, arr: IndexableVal) -> ArrValue {62	let arr = arr.to_array();63	arr.map(func)64}6566#[builtin]67pub fn builtin_map_with_index(func: FuncVal, arr: IndexableVal) -> ArrValue {68	let arr = arr.to_array();69	arr.map_with_index(func)70}7172#[builtin]73pub fn builtin_map_with_key(func: FuncVal, obj: ObjValue) -> Result<ObjValue> {74	let mut out = ObjValueBuilder::new();75	for (k, v) in obj.iter(76		// Makes sense mapped object should be ordered the same way, should not break anything when the output is not ordered (the default).77		// The thrown error might be different, but jsonnet78		// does not specify the evaluation order.79		#[cfg(feature = "exp-preserve-order")]80		true,81	) {82		let v = v?;83		out.field(k.clone())84			.value(func.evaluate_simple(&(k, v), false)?);85	}86	Ok(out.build())87}8889#[builtin]90pub fn builtin_flatmap(91	func: NativeFn<((Either![String, Val],), Val)>,92	arr: IndexableVal,93) -> Result<IndexableVal> {94	use std::fmt::Write;95	match arr {96		IndexableVal::Str(str) => {97			let mut out = String::new();98			for c in str.chars() {99				match func(Either2::A(c.to_string()))? {100					Val::Str(o) => write!(out, "{o}").unwrap(),101					Val::Null => continue,102					_ => bail!("in std.join all items should be strings"),103				};104			}105			Ok(IndexableVal::Str(out.into()))106		}107		IndexableVal::Arr(a) => {108			let mut out = Vec::new();109			for el in a.iter() {110				let el = el?;111				match func(Either2::B(el))? {112					Val::Arr(o) => {113						for oe in o.iter() {114							out.push(oe?);115						}116					}117					Val::Null => continue,118					_ => bail!("in std.join all items should be arrays"),119				};120			}121			Ok(IndexableVal::Arr(out.into()))122		}123	}124}125126#[builtin]127pub fn builtin_filter(func: FuncVal, arr: ArrValue) -> Result<ArrValue> {128	arr.filter(|val| bool::from_untyped(func.evaluate_simple(&(val.clone(),), false)?))129}130131#[builtin]132pub fn builtin_filter_map(133	filter_func: FuncVal,134	map_func: FuncVal,135	arr: ArrValue,136) -> Result<ArrValue> {137	Ok(builtin_filter(filter_func, arr)?.map(map_func))138}139140#[builtin]141pub fn builtin_foldl(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {142	let mut acc = init;143	for i in arr.iter() {144		acc = func.evaluate_simple(&(acc, i?), false)?;145	}146	Ok(acc)147}148149#[builtin]150pub fn builtin_foldr(func: FuncVal, arr: ArrValue, init: Val) -> Result<Val> {151	let mut acc = init;152	for i in arr.iter().rev() {153		acc = func.evaluate_simple(&(i?, acc), false)?;154	}155	Ok(acc)156}157158#[builtin]159pub fn builtin_range(from: i32, to: i32) -> Result<ArrValue> {160	if to < from {161		return Ok(ArrValue::empty());162	}163	Ok(ArrValue::range_inclusive(from, to))164}165166#[builtin]167pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {168	use std::fmt::Write;169	Ok(match sep {170		IndexableVal::Arr(joiner_items) => {171			let mut out = Vec::new();172173			let mut first = true;174			for item in arr.iter() {175				let item = item?.clone();176				if let Val::Arr(items) = item {177					if !first {178						out.reserve(joiner_items.len());179						// TODO: extend180						for item in joiner_items.iter() {181							out.push(item?);182						}183					}184					first = false;185					out.reserve(items.len());186					for item in items.iter() {187						out.push(item?);188					}189				} else if matches!(item, Val::Null) {190					continue;191				} else {192					bail!("in std.join all items should be arrays");193				}194			}195196			IndexableVal::Arr(out.into())197		}198		IndexableVal::Str(sep) => {199			let mut out = String::new();200201			let mut first = true;202			for item in arr.iter() {203				let item = item?.clone();204				if let Val::Str(item) = item {205					if !first {206						out += &sep;207					}208					first = false;209					write!(out, "{item}").unwrap();210				} else if matches!(item, Val::Null) {211					continue;212				} else {213					bail!("in std.join all items should be strings");214				}215			}216217			IndexableVal::Str(out.into())218		}219	})220}221222#[builtin]223pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {224	builtin_join(225		IndexableVal::Str("\n".into()),226		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),227	)228}229230#[builtin]231pub fn builtin_resolve_path(f: String, r: String) -> String {232	let Some(pos) = f.rfind('/') else {233		return r;234	};235	format!("{}{}", &f[..=pos], r)236}237238pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {239	use std::fmt::Write;240	match arr {241		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),242		IndexableVal::Arr(arr) => {243			for ele in arr.iter() {244				let indexable = IndexableVal::from_untyped(ele?)?;245				deep_join_inner(out, indexable)?;246			}247		}248	}249	Ok(())250}251252#[builtin]253pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {254	let mut out = String::new();255	deep_join_inner(&mut out, arr)?;256	Ok(out)257}258259#[builtin]260pub fn builtin_reverse(arr: ArrValue) -> ArrValue {261	arr.reversed()262}263264#[builtin]265pub fn builtin_any(arr: ArrValue) -> Result<bool> {266	for v in arr.iter() {267		let v = bool::from_untyped(v?)?;268		if v {269			return Ok(true);270		}271	}272	Ok(false)273}274275#[builtin]276pub fn builtin_all(arr: ArrValue) -> Result<bool> {277	for v in arr.iter() {278		let v = bool::from_untyped(v?)?;279		if !v {280			return Ok(false);281		}282	}283	Ok(true)284}285286#[builtin]287pub fn builtin_member(arr: IndexableVal, x: Val) -> Result<bool> {288	match arr {289		IndexableVal::Str(str) => {290			let x: IStr = IStr::from_untyped(x)?;291			Ok(!x.is_empty() && str.contains(&*x))292		}293		IndexableVal::Arr(a) => {294			for item in a.iter() {295				let item = item?;296				if equals(&item, &x)? {297					return Ok(true);298				}299			}300			Ok(false)301		}302	}303}304305#[builtin]306pub fn builtin_find(value: Val, arr: ArrValue) -> Result<Vec<usize>> {307	let mut out = Vec::new();308	for (i, ele) in arr.iter().enumerate() {309		let ele = ele?;310		if equals(&ele, &value)? {311			out.push(i);312		}313	}314	Ok(out)315}316317#[builtin]318pub fn builtin_contains(arr: IndexableVal, elem: Val) -> Result<bool> {319	builtin_member(arr, elem)320}321322#[builtin]323pub fn builtin_count(arr: ArrValue, x: Val) -> Result<usize> {324	let mut count = 0;325	for item in arr.iter() {326		if equals(&item?, &x)? {327			count += 1;328		}329	}330	Ok(count)331}332333#[builtin]334pub fn builtin_avg(arr: Vec<f64>, onEmpty: Option<Thunk<Val>>) -> Result<Val> {335	if arr.is_empty() {336		return eval_on_empty(onEmpty);337	}338	Ok(Val::try_num(arr.iter().sum::<f64>() / (arr.len() as f64))?)339}340341#[builtin]342pub fn builtin_remove_at(arr: ArrValue, at: i32) -> Result<ArrValue> {343	let newArrLeft = arr.clone().slice(None, Some(at), None);344	let newArrRight = arr.slice(Some(at + 1), None, None);345346	Ok(ArrValue::extended(newArrLeft, newArrRight))347}348349#[builtin]350pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {351	for (index, item) in arr.iter().enumerate() {352		if equals(&item?, &elem)? {353			return builtin_remove_at(arr.clone(), index as i32);354		}355	}356	Ok(arr)357}358359#[builtin]360pub fn builtin_flatten_arrays(arrs: Vec<ArrValue>) -> ArrValue {361	pub fn flatten_inner(values: &[ArrValue]) -> ArrValue {362		if values.len() == 1 {363			return values[0].clone();364		} else if values.len() == 2 {365			return ArrValue::extended(values[0].clone(), values[1].clone());366		}367		let (a, b) = values.split_at(values.len() / 2);368		ArrValue::extended(flatten_inner(a), flatten_inner(b))369	}370	if arrs.is_empty() {371		return ArrValue::empty();372	} else if arrs.len() == 1 {373		return arrs.into_iter().next().expect("single");374	}375	flatten_inner(&arrs)376}377378#[builtin]379pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {380	fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {381		match value {382			Val::Arr(arr) => {383				for ele in arr.iter() {384					process(ele?, out)?;385				}386			}387			_ => out.push(value),388		}389		Ok(())390	}391	let mut out = Vec::new();392	process(value, &mut out)?;393	Ok(out)394}395396#[builtin]397pub fn builtin_prune(398	a: Val,399400	#[default(false)]401	#[cfg(feature = "exp-preserve-order")]402	preserve_order: bool,403) -> Result<Val> {404	fn is_content(val: &Val) -> bool {405		match val {406			Val::Null => false,407			Val::Arr(a) => !a.is_empty(),408			Val::Obj(o) => !o.is_empty(),409			_ => true,410		}411	}412	Ok(match a {413		Val::Arr(a) => {414			let mut out = Vec::new();415			for (i, ele) in a.iter().enumerate() {416				let ele = ele417					.and_then(|v| {418						builtin_prune(419							v,420							#[cfg(feature = "exp-preserve-order")]421							preserve_order,422						)423					})424					.with_description(|| format!("elem <{i}> pruning"))?;425				if is_content(&ele) {426					out.push(ele);427				}428			}429			Val::Arr(ArrValue::eager(out))430		}431		Val::Obj(o) => {432			let mut out = ObjValueBuilder::new();433			for (name, value) in o.iter(434				#[cfg(feature = "exp-preserve-order")]435				preserve_order,436			) {437				let value = value438					.and_then(|v| {439						builtin_prune(440							v,441							#[cfg(feature = "exp-preserve-order")]442							preserve_order,443						)444					})445					.with_description(|| format!("field <{name}> pruning"))?;446				if !is_content(&value) {447					continue;448				}449				out.field(name).value(value);450			}451			Val::Obj(out.build())452		}453		_ => a,454	})455}