git.delta.rocks / jrsonnet / refs/commits / 8f8545cd220d

difftreelog

feat implement std.flattenDeepArray builtin

Yaroslav Bolyukin2024-04-07parent: #69c135a.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
303 flatten_inner(&arrs)303 flatten_inner(&arrs)
304}304}
305
306#[builtin]
307pub fn builtin_flatten_deep_array(value: Val) -> Result<Vec<Val>> {
308 fn process(value: Val, out: &mut Vec<Val>) -> Result<()> {
309 match value {
310 Val::Arr(arr) => {
311 for ele in arr.iter() {
312 process(ele?, out)?;
313 }
314 }
315 _ => out.push(value),
316 }
317 Ok(())
318 }
319 let mut out = Vec::new();
320 process(value, &mut out)?;
321 Ok(out)
322}
305323
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -48,6 +48,7 @@
 #[cfg(feature = "exp-regex")]
 pub use crate::regex::*;
 
+#[allow(clippy::too_many_lines)]
 pub fn stdlib_uncached(settings: Rc<RefCell<Settings>>) -> ObjValue {
 	let mut builder = ObjValueBuilder::new();
 
@@ -59,6 +60,7 @@
 
 	builder.with_super(eval);
 
+	// FIXME: Use PHF
 	for (name, builtin) in [
 		// Types
 		("type", builtin_type::INST),
@@ -89,6 +91,7 @@
 		("removeAt", builtin_remove_at::INST),
 		("remove", builtin_remove::INST),
 		("flattenArrays", builtin_flatten_arrays::INST),
+		("flattenDeepArray", builtin_flatten_deep_array::INST),
 		("filterMap", builtin_filter_map::INST),
 		// Math
 		("abs", builtin_abs::INST),
@@ -196,7 +199,7 @@
 		("__compare", builtin___compare::INST),
 	]
 	.iter()
-	.cloned()
+	.copied()
 	{
 		builder.method(name, builtin);
 	}
@@ -268,10 +271,10 @@
 			let locs = loc.0.map_source_locations(&[loc.1]);
 			eprint!(
 				" {}:{}",
-				match loc.0.source_path().path() {
-					Some(p) => self.resolver.resolve(p),
-					None => loc.0.source_path().to_string(),
-				},
+				loc.0.source_path().path().map_or_else(
+					|| loc.0.source_path().to_string(),
+					|p| self.resolver.resolve(p)
+				),
 				locs[0].line
 			);
 		}
@@ -416,6 +419,6 @@
 impl StateExt for State {
 	fn with_stdlib(&self) {
 		let initializer = ContextInitializer::new(self.clone(), PathResolver::new_cwd_fallback());
-		self.settings_mut().context_initializer = tb!(initializer)
+		self.settings_mut().context_initializer = tb!(initializer);
 	}
 }