difftreelog
feat object destructuring defaults
in: master
5 files changed
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -8,6 +8,9 @@
pub fn new() -> Self {
Self(Cc::new(RefCell::new(None)))
}
+ pub fn new_filled(v: T) -> Self {
+ Self(Cc::new(RefCell::new(Some(v))))
+ }
/// # Panics
/// If wrapper is filled already
pub fn fill(self, value: T) {
crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -12,9 +12,11 @@
};
#[allow(clippy::too_many_lines)]
+#[allow(unused_variables)]
pub fn destruct(
d: &Destruct,
parent: Thunk<Val>,
+ fctx: Pending<Context>,
new_bindings: &mut GcHashMap<IStr, Thunk<Val>>,
) -> Result<()> {
match d {
@@ -89,6 +91,7 @@
full: full.clone(),
index: i,
})),
+ fctx.clone(),
new_bindings,
)?;
}
@@ -119,6 +122,7 @@
start: start.len(),
end: end.len(),
})),
+ fctx.clone(),
new_bindings,
)?;
}
@@ -151,6 +155,7 @@
index: i,
end: end.len(),
})),
+ fctx.clone(),
new_bindings,
)?;
}
@@ -189,36 +194,51 @@
Ok(obj)
}
}
- let field_names: Vec<_> = fields.iter().map(|f| f.0.clone()).collect();
+ let field_names: Vec<_> = fields
+ .iter()
+ .filter(|f| f.2.is_none())
+ .map(|f| f.0.clone())
+ .collect();
let full = Thunk::new(tb!(DataThunk {
parent,
field_names: field_names.clone(),
has_rest: rest.is_some()
}));
- for (field, d) in fields {
+ for (field, d, default) in fields {
#[derive(Trace)]
struct FieldThunk {
full: Thunk<ObjValue>,
field: IStr,
+ default: Option<(Pending<Context>, LocExpr)>,
}
impl ThunkValue for FieldThunk {
type Output = Val;
fn get(self: Box<Self>, s: State) -> Result<Self::Output> {
let full = self.full.evaluate(s.clone())?;
- let field = full.get(s, self.field)?.expect("shape is checked");
- Ok(field)
+ if let Some(field) = full.get(s.clone(), self.field)? {
+ Ok(field)
+ } else {
+ let (fctx, expr) = self.default.as_ref().expect("shape is checked");
+ Ok(evaluate(s, fctx.clone().unwrap(), &expr)?)
+ }
}
}
let value = Thunk::new(tb!(FieldThunk {
full: full.clone(),
- field: field.clone()
+ field: field.clone(),
+ default: default.clone().map(|e| (fctx.clone(), e)),
}));
if let Some(d) = d {
- destruct(d, value, new_bindings)?;
+ destruct(d, value, fctx.clone(), new_bindings)?;
} else {
- destruct(&Destruct::Full(field.clone()), value, new_bindings)?;
+ destruct(
+ &Destruct::Full(field.clone()),
+ value,
+ fctx.clone(),
+ new_bindings,
+ )?;
}
}
}
@@ -251,10 +271,10 @@
}
let data = Thunk::new(tb!(EvaluateThunkValue {
name: into.name(),
- fctx,
+ fctx: fctx.clone(),
expr: value.clone(),
}));
- destruct(into, data, new_bindings)?;
+ destruct(into, data, fctx, new_bindings)?;
}
BindSpec::Function {
name,
crates/jrsonnet-evaluator/src/function/parse.rsdiffbeforeafterboth59 destruct(&name, arg, &mut passed_args)?;59 destruct(60 &name,61 arg,62 Pending::new_filled(ctx.clone()),63 &mut passed_args,64 )?;60 filled_positionals += 1;65 filled_positionals += 1;61 Ok(())66 Ok(())96 name: param.0.name().unwrap_or_else(|| "<destruct>".into()),101 name: param.0.name().unwrap_or_else(|| "<destruct>".into()),97 value: param.1.clone().expect("default exists"),102 value: param.1.clone().expect("default exists"),98 })),103 })),104 fctx.clone(),99 &mut defaults,105 &mut defaults,100 )?;106 )?;101 if param.0.name().is_some() {107 if param.0.name().is_some() {230 name: param.0.name().unwrap_or_else(|| "<destruct>".into()),236 name: param.0.name().unwrap_or_else(|| "<destruct>".into()),231 value: v.clone(),237 value: v.clone(),232 })),238 })),239 fctx.clone(),233 &mut bindings,240 &mut bindings,234 )?;241 )?;235 } else {242 } else {238 Thunk::new(tb!(DependsOnUnbound(245 Thunk::new(tb!(DependsOnUnbound(239 param.0.name().unwrap_or_else(|| "<destruct>".into())246 param.0.name().unwrap_or_else(|| "<destruct>".into())240 ))),247 ))),248 fctx.clone(),241 &mut bindings,249 &mut bindings,242 )?;250 )?;243 }251 }crates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/expr.rs
+++ b/crates/jrsonnet-parser/src/expr.rs
@@ -188,7 +188,7 @@
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
-#[derive(Debug, Clone, PartialEq, Eq, Trace)]
+#[derive(Debug, Clone, PartialEq, Trace)]
pub enum Destruct {
Full(IStr),
#[cfg(feature = "exp-destruct")]
@@ -201,7 +201,7 @@
},
#[cfg(feature = "exp-destruct")]
Object {
- fields: Vec<(IStr, Option<Destruct>)>,
+ fields: Vec<(IStr, Option<Destruct>, Option<LocExpr>)>,
rest: Option<DestructRest>,
},
}
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,4 +1,4 @@
-#![allow(clippy::redundant_closure_call)]
+#![allow(clippy::redundant_closure_call, clippy::derive_partial_eq_without_eq)]
use std::rc::Rc;
@@ -109,7 +109,7 @@
}
pub rule destruct_object(s: &ParserSettings) -> expr::Destruct
= "{" _
- fields:(name:id() _ into:(":" _ into:destruct(s) {into})? {(name, into)})**comma()
+ fields:(name:id() into:(_ ":" _ into:destruct(s) {into})? default:(_ "=" _ v:expr(s) {v})? {(name, into, default)})**comma()
rest:(
comma() rest:destruct_rest()? {rest}
/ comma()? {None}