difftreelog
feat `ContextBuilder`
in: master
Helper for fancier building of user contexts
2 files changed
crates/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,
+ }))
+ }
+}
crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth23 }23 }24 }24 }2526 pub(crate) fn new(layer: GcHashMap<IStr, Thunk<Val>>) -> Self {27 Self(Cc::new(LayeredHashMapInternals {28 parent: None,29 current: layer,30 }))31 }253226 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 {