difftreelog
refactor object builder
in: master
3 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -2,14 +2,14 @@
error::Error::*,
evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
- FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjMember, ObjValue, ObjectAssertion,
+ FutureWrapper, LazyBinding, LazyVal, LazyValValue, ObjValue, ObjValueBuilder, ObjectAssertion,
Result, Val,
};
use jrsonnet_gc::{Gc, Trace};
use jrsonnet_interner::IStr;
use jrsonnet_parser::{
ArgsDesc, AssertStmt, BindSpec, CompSpec, Expr, ExprLocation, FieldMember, ForSpecData,
- IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc, Visibility,
+ IfSpecData, LiteralType, LocExpr, Member, ObjBody, ParamsDesc,
};
use jrsonnet_types::ValType;
use rustc_hash::{FxHashMap, FxHasher};
@@ -254,8 +254,7 @@
new_bindings.fill(bindings);
}
- let mut new_members = FxHashMap::default();
- let mut assertions: Vec<Box<dyn ObjectAssertion>> = Vec::new();
+ let mut builder = ObjValueBuilder::new();
for member in members.iter() {
match member {
Member::Field(FieldMember {
@@ -291,19 +290,16 @@
)?))
}
}
- new_members.insert(
- name.clone(),
- ObjMember {
- add: *plus,
- visibility: *visibility,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
- context_creator: context_creator.clone(),
- value: value.clone(),
- name,
- }))),
- location: value.1.clone(),
- },
- );
+ builder
+ .member(name.clone())
+ .with_add(*plus)
+ .with_visibility(*visibility)
+ .with_location(value.1.clone())
+ .bindable(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ name,
+ }));
}
Member::Field(FieldMember {
name,
@@ -338,20 +334,16 @@
)))
}
}
- new_members.insert(
- name.clone(),
- ObjMember {
- add: false,
- visibility: Visibility::Hidden,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
- context_creator: context_creator.clone(),
- value: value.clone(),
- params: params.clone(),
- name,
- }))),
- location: value.1.clone(),
- },
- );
+ builder
+ .member(name.clone())
+ .hide()
+ .with_location(value.1.clone())
+ .binding(LazyBinding::Bindable(Gc::new(Box::new(ObjMemberBinding {
+ context_creator: context_creator.clone(),
+ value: value.clone(),
+ params: params.clone(),
+ name,
+ }))));
}
Member::BindStmt(_) => {}
Member::AssertStmt(stmt) => {
@@ -371,14 +363,14 @@
evaluate_assert(ctx, &self.assert)
}
}
- assertions.push(Box::new(ObjectAssert {
+ builder.assert(Box::new(ObjectAssert {
context_creator: context_creator.clone(),
assert: stmt.clone(),
}));
}
}
}
- let this = ObjValue::new(None, Gc::new(new_members), Gc::new(assertions));
+ let this = builder.build();
future_this.fill(this.clone());
Ok(this)
}
@@ -388,7 +380,7 @@
ObjBody::MemberList(members) => evaluate_member_list_object(context, members)?,
ObjBody::ObjComp(obj) => {
let future_this = FutureWrapper::new();
- let mut new_members = FxHashMap::default();
+ let mut builder = ObjValueBuilder::new();
evaluate_comp(context.clone(), &obj.compspecs, &mut |ctx| {
let new_bindings = FutureWrapper::new();
let context_creator = ContextCreator(context.clone(), new_bindings.clone());
@@ -435,18 +427,13 @@
)?))
}
}
- new_members.insert(
- n,
- ObjMember {
- add: false,
- visibility: Visibility::Normal,
- invoke: LazyBinding::Bindable(Gc::new(Box::new(ObjCompBinding {
- context: ctx,
- value: obj.value.clone(),
- }))),
- location: obj.value.1.clone(),
- },
- );
+ builder
+ .member(n)
+ .with_location(obj.value.1.clone())
+ .bindable(Box::new(ObjCompBinding {
+ context: ctx,
+ value: obj.value.clone(),
+ }));
}
v => throw!(FieldMustBeStringGot(v.value_type())),
}
@@ -454,7 +441,7 @@
Ok(())
})?;
- let this = ObjValue::new(None, Gc::new(new_members), Gc::new(Vec::new()));
+ let this = builder.build();
future_this.fill(this.clone());
this
}
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -1,16 +1,9 @@
use crate::{
error::{Error::*, LocError, Result},
- throw, LazyBinding, LazyVal, ObjMember, ObjValue, Val,
+ throw, ObjValueBuilder, Val,
};
-use jrsonnet_gc::Gc;
-use jrsonnet_parser::Visibility;
-use rustc_hash::FxHasher;
use serde_json::{Map, Number, Value};
-use std::{
- collections::HashMap,
- convert::{TryFrom, TryInto},
- hash::BuildHasherDefault,
-};
+use std::convert::{TryFrom, TryInto};
impl TryFrom<&Val> for Value {
type Error = LocError;
@@ -54,29 +47,18 @@
Value::Number(n) => Self::Num(n.as_f64().expect("as f64")),
Value::String(s) => Self::Str((s as &str).into()),
Value::Array(a) => {
- let mut out = Vec::with_capacity(a.len());
+ let mut out: Vec<Val> = Vec::with_capacity(a.len());
for v in a {
- out.push(LazyVal::new_resolved(v.into()));
+ out.push(v.into());
}
Self::Arr(out.into())
}
Value::Object(o) => {
- let mut entries = HashMap::with_capacity_and_hasher(
- o.len(),
- BuildHasherDefault::<FxHasher>::default(),
- );
+ let mut builder = ObjValueBuilder::with_capacity(o.len());
for (k, v) in o {
- entries.insert(
- (k as &str).into(),
- ObjMember {
- add: false,
- visibility: Visibility::Normal,
- invoke: LazyBinding::Bound(LazyVal::new_resolved(v.into())),
- location: None,
- },
- );
+ builder.member((k as &str).into()).value(v.into());
}
- Self::Obj(ObjValue::new(None, Gc::new(entries), Gc::new(Vec::new())))
+ Val::Obj(builder.build())
}
}
}
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth1use crate::operator::evaluate_add_op;1use crate::operator::evaluate_add_op;2use crate::{LazyBinding, Result, Val};2use crate::{Bindable, LazyBinding, LazyVal, Result, Val};3use jrsonnet_gc::{Gc, GcCell, Trace};3use jrsonnet_gc::{Gc, GcCell, Trace};4use jrsonnet_interner::IStr;4use jrsonnet_interner::IStr;5use jrsonnet_parser::{ExprLocation, Visibility};5use jrsonnet_parser::{ExprLocation, Visibility};6use rustc_hash::{FxHashMap, FxHashSet};6use rustc_hash::{FxHashMap, FxHashSet, FxHasher};7use std::collections::HashMap;7use std::hash::{Hash, Hasher};8use std::hash::{Hash, Hasher};8use std::{fmt::Debug, hash::BuildHasherDefault};9use std::{fmt::Debug, hash::BuildHasherDefault};910275 }276 }276}277}278279pub struct ObjValueBuilder {280 super_obj: Option<ObjValue>,281 map: FxHashMap<IStr, ObjMember>,282 assertions: Vec<Box<dyn ObjectAssertion>>,283}284impl ObjValueBuilder {285 pub fn new() -> Self {286 Self::with_capacity(0)287 }288 pub fn with_capacity(capacity: usize) -> Self {289 Self {290 super_obj: None,291 map: HashMap::with_capacity_and_hasher(292 capacity,293 BuildHasherDefault::<FxHasher>::default(),294 ),295 assertions: Vec::new(),296 }297 }298 pub fn reserve_asserts(&mut self, capacity: usize) -> &mut Self {299 self.assertions.reserve_exact(capacity);300 self301 }302 pub fn with_super(&mut self, super_obj: ObjValue) -> &mut Self {303 self.super_obj = Some(super_obj);304 self305 }306307 pub fn assert(&mut self, assertion: Box<dyn ObjectAssertion>) -> &mut Self {308 self.assertions.push(assertion);309 self310 }311 pub fn member(&mut self, name: IStr) -> ObjMemberBuilder {312 ObjMemberBuilder {313 value: self,314 name,315 add: false,316 visibility: Visibility::Normal,317 location: None,318 }319 }320321 pub fn build(self) -> ObjValue {322 ObjValue::new(self.super_obj, Gc::new(self.map), Gc::new(self.assertions))323 }324}325326#[must_use = "value not added unless binding() was called"]327pub struct ObjMemberBuilder<'v> {328 value: &'v mut ObjValueBuilder,329 name: IStr,330 add: bool,331 visibility: Visibility,332 location: Option<ExprLocation>,333}334335impl<'v> ObjMemberBuilder<'v> {336 pub fn with_add(mut self, add: bool) -> Self {337 self.add = add;338 self339 }340 pub fn add(self) -> Self {341 self.with_add(true)342 }343 pub fn with_visibility(mut self, visibility: Visibility) -> Self {344 self.visibility = visibility;345 self346 }347 pub fn hide(self) -> Self {348 self.with_visibility(Visibility::Hidden)349 }350 pub fn with_location(mut self, location: Option<ExprLocation>) -> Self {351 self.location = location;352 self353 }354 pub fn value(self, value: Val) -> &'v mut ObjValueBuilder {355 self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))356 }357 pub fn bindable(self, bindable: Box<dyn Bindable>) -> &'v mut ObjValueBuilder {358 self.binding(LazyBinding::Bindable(Gc::new(bindable)))359 }360 pub fn binding(self, binding: LazyBinding) -> &'v mut ObjValueBuilder {361 self.value.map.insert(362 self.name,363 ObjMember {364 add: self.add,365 visibility: self.visibility,366 invoke: binding,367 location: self.location,368 },369 );370 self.value371 }372}277373