git.delta.rocks / jrsonnet / refs/commits / 21a51316cb87

difftreelog

feat `ContextBuilder`

Yaroslav Bolyukin2022-07-23parent: #41db4d5.patch.diff
in: master
Helper for fancier building of user contexts

2 files changed

modifiedcrates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -138,3 +138,30 @@
 		Cc::ptr_eq(&self.0, &other.0)
 	}
 }
+
+#[derive(Default)]
+pub struct ContextBuilder {
+	bindings: GcHashMap<IStr, Thunk<Val>>,
+}
+impl ContextBuilder {
+	pub fn new() -> Self {
+		Self::default()
+	}
+	pub fn with_capacity(capacity: usize) -> Self {
+		Self {
+			bindings: GcHashMap::with_capacity(capacity),
+		}
+	}
+	pub fn bind(&mut self, name: IStr, value: Thunk<Val>) -> &mut Self {
+		self.bindings.insert(name, value);
+		self
+	}
+	pub fn build(self) -> Context {
+		Context(Cc::new(ContextInternals {
+			bindings: LayeredHashMap::new(self.bindings),
+			dollar: None,
+			sup: None,
+			this: None,
+		}))
+	}
+}
modifiedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
23 }23 }
24 }24 }
25
26 pub(crate) fn new(layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
27 Self(Cc::new(LayeredHashMapInternals {
28 parent: None,
29 current: layer,
30 }))
31 }
2532
26 pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {33 pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
27 Self(Cc::new(LayeredHashMapInternals {34 Self(Cc::new(LayeredHashMapInternals {