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
139 }139 }
140}140}
141
142#[derive(Default)]
143pub struct ContextBuilder {
144 bindings: GcHashMap<IStr, Thunk<Val>>,
145}
146impl ContextBuilder {
147 pub fn new() -> Self {
148 Self::default()
149 }
150 pub fn with_capacity(capacity: usize) -> Self {
151 Self {
152 bindings: GcHashMap::with_capacity(capacity),
153 }
154 }
155 pub fn bind(&mut self, name: IStr, value: Thunk<Val>) -> &mut Self {
156 self.bindings.insert(name, value);
157 self
158 }
159 pub fn build(self) -> Context {
160 Context(Cc::new(ContextInternals {
161 bindings: LayeredHashMap::new(self.bindings),
162 dollar: None,
163 sup: None,
164 this: None,
165 }))
166 }
167}
141168
modifiedcrates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -23,6 +23,13 @@
 		}
 	}
 
+	pub(crate) fn new(layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
+		Self(Cc::new(LayeredHashMapInternals {
+			parent: None,
+			current: layer,
+		}))
+	}
+
 	pub fn extend(self, new_layer: GcHashMap<IStr, Thunk<Val>>) -> Self {
 		Self(Cc::new(LayeredHashMapInternals {
 			parent: Some(self),