From 21a51316cb87808ccb8b17a24e0d8582e2a42848 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sat, 23 Jul 2022 22:27:48 +0000 Subject: [PATCH] feat: `ContextBuilder` Helper for fancier building of user contexts --- --- 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>, +} +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) -> &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, + })) + } +} --- a/crates/jrsonnet-evaluator/src/map.rs +++ b/crates/jrsonnet-evaluator/src/map.rs @@ -23,6 +23,13 @@ } } + pub(crate) fn new(layer: GcHashMap>) -> Self { + Self(Cc::new(LayeredHashMapInternals { + parent: None, + current: layer, + })) + } + pub fn extend(self, new_layer: GcHashMap>) -> Self { Self(Cc::new(LayeredHashMapInternals { parent: Some(self), -- gitstuff