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
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 {