difftreelog
style switch clippy to pedantic
in: master
25 files changed
crates/jrsonnet-evaluator/src/builtin/format.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/format.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/format.rs
@@ -86,6 +86,7 @@
}
}
+#[allow(clippy::struct_excessive_bools)]
#[derive(Default, Debug)]
pub struct CFlags {
pub alt: bool,
@@ -270,12 +271,12 @@
return Ok(out);
}
str = &str[offset + 1..];
- let (code, nstr) = parse_code(str)?;
- str = nstr;
+ let code;
+ (code, str) = parse_code(str)?;
bytes = str.as_bytes();
offset = 0;
- out.push(Element::Code(code))
+ out.push(Element::Code(code));
}
}
@@ -313,7 +314,7 @@
.saturating_sub(prefix.len() + digits.len());
if neg {
- out.push('-')
+ out.push('-');
} else if sign {
out.push('+');
} else if blank {
@@ -340,7 +341,7 @@
blank: bool,
sign: bool,
) {
- render_integer(out, iv, padding, precision, blank, sign, 10, "", false)
+ render_integer(out, iv, padding, precision, blank, sign, 10, "", false);
}
pub fn render_octal(
out: &mut String,
@@ -361,8 +362,10 @@
8,
if alt && iv != 0 { "0" } else { "" },
false,
- )
+ );
}
+
+#[allow(clippy::fn_params_excessive_bools)]
pub fn render_hexadecimal(
out: &mut String,
iv: i64,
@@ -387,9 +390,10 @@
(false, _) => "",
},
caps,
- )
+ );
}
+#[allow(clippy::fn_params_excessive_bools)]
pub fn render_float(
out: &mut String,
n: f64,
@@ -431,6 +435,7 @@
}
}
+#[allow(clippy::fn_params_excessive_bools)]
pub fn render_float_sci(
out: &mut String,
n: f64,
@@ -461,6 +466,7 @@
out.push_str(&exponent_str);
}
+#[allow(clippy::too_many_lines)]
pub fn format_code(
s: State,
out: &mut String,
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -158,7 +158,7 @@
'\r' => buf.push_str("\\r"),
'\t' => buf.push_str("\\t"),
c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {
- write!(buf, "\\u{:04x}", c as u32).unwrap()
+ write!(buf, "\\u{:04x}", c as u32).unwrap();
}
c => buf.push(c),
}
@@ -194,7 +194,7 @@
pub preserve_order: bool,
}
-/// From https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289
+/// From <https://github.com/chyh1990/yaml-rust/blob/da52a68615f2ecdd6b7e4567019f280c433c1521/src/emitter.rs#L289>
/// With added date check
fn yaml_needs_quotes(string: &str) -> bool {
fn need_quotes_spaces(string: &str) -> bool {
@@ -227,6 +227,8 @@
manifest_yaml_ex_buf(s, val, &mut out, &mut String::new(), options)?;
Ok(out)
}
+
+#[allow(clippy::too_many_lines)]
fn manifest_yaml_ex_buf(
s: State,
val: &Val,
@@ -238,9 +240,9 @@
match val {
Val::Bool(v) => {
if *v {
- buf.push_str("true")
+ buf.push_str("true");
} else {
- buf.push_str("false")
+ buf.push_str("false");
}
}
Val::Null => buf.push_str("null"),
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,3 +1,6 @@
+// All builtins should return results
+#![allow(clippy::unnecessary_wraps)]
+
use std::collections::HashMap;
use format::{format_arr, format_obj};
@@ -173,7 +176,7 @@
fn builtin_make_array(s: State, sz: usize, func: FuncVal) -> Result<VecVal> {
let mut out = Vec::with_capacity(sz);
for i in 0..sz {
- out.push(func.evaluate_simple(s.clone(), &[i as f64].as_slice())?)
+ out.push(func.evaluate_simple(s.clone(), &[i as f64].as_slice())?);
}
Ok(VecVal(Cc::new(out)))
}
@@ -420,7 +423,7 @@
match func.evaluate_simple(s.clone(), &[Any(el)].as_slice())? {
Val::Arr(o) => {
for oe in o.iter(s.clone()) {
- out.push(oe?)
+ out.push(oe?);
}
}
_ => throw!(RuntimeError(
@@ -707,7 +710,7 @@
#[jrsonnet_macros::builtin]
fn builtin_count(s: State, arr: Vec<Any>, v: Any) -> Result<usize> {
let mut count = 0;
- for item in arr.iter() {
+ for item in &arr {
if equals(s.clone(), &item.0, &v.0)? {
count += 1;
}
crates/jrsonnet-evaluator/src/builtin/sort.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/sort.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/sort.rs
@@ -53,10 +53,10 @@
match (i, sort_type) {
(Val::Str(_), SortKeyType::Unknown) => sort_type = SortKeyType::String,
(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
- (Val::Str(_), SortKeyType::String) => {}
- (Val::Str(_), _) => throw!(SortError::SortElementsShouldHaveEqualType),
- (Val::Num(_), SortKeyType::Number) => {}
- (Val::Num(_), _) => throw!(SortError::SortElementsShouldHaveEqualType),
+ (Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
+ (Val::Str(_) | Val::Num(_), _) => {
+ throw!(SortError::SortElementsShouldHaveEqualType)
+ }
_ => throw!(SortError::SortKeyShouldBeStringOrNumber),
}
}
@@ -91,19 +91,19 @@
Ok(Cc::new(vk.into_iter().map(|v| v.0).collect()))
} else {
// Fast path, identity key getter
- let mut mvalues = (*values).clone();
- let sort_type = get_sort_type(&mut mvalues, |k| k)?;
+ let mut values = (*values).clone();
+ let sort_type = get_sort_type(&mut values, |k| k)?;
match sort_type {
- SortKeyType::Number => mvalues.sort_unstable_by_key(|v| match v {
+ SortKeyType::Number => values.sort_unstable_by_key(|v| match v {
Val::Num(n) => NonNaNf64(*n),
_ => unreachable!(),
}),
- SortKeyType::String => mvalues.sort_unstable_by_key(|v| match v {
+ SortKeyType::String => values.sort_unstable_by_key(|v| match v {
Val::Str(s) => s.clone(),
_ => unreachable!(),
}),
SortKeyType::Unknown => unreachable!(),
};
- Ok(Cc::new(mvalues))
+ Ok(Cc::new(values))
}
}
crates/jrsonnet-evaluator/src/builtin/stdlib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/stdlib.rs
@@ -24,5 +24,5 @@
}
pub fn get_parsed_stdlib() -> LocExpr {
- PARSED_STDLIB.with(|t| t.clone())
+ PARSED_STDLIB.with(Clone::clone)
}
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -79,6 +79,7 @@
pub fn contains_binding(&self, name: IStr) -> bool {
self.0.bindings.contains_key(&name)
}
+ #[must_use]
pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {
{
ctx.0.borrow_mut().replace(self);
@@ -86,16 +87,19 @@
ctx.unwrap()
}
+ #[must_use]
pub fn with_var(self, name: IStr, value: Val) -> Self {
let mut new_bindings = GcHashMap::with_capacity(1);
new_bindings.insert(name, LazyVal::new_resolved(value));
self.extend(new_bindings, None, None, None)
}
+ #[must_use]
pub fn with_this_super(self, new_this: ObjValue, new_super_obj: Option<ObjValue>) -> Self {
self.extend(GcHashMap::new(), None, Some(new_this), new_super_obj)
}
+ #[must_use]
pub fn extend(
self,
new_bindings: GcHashMap<IStr, LazyVal>,
@@ -119,6 +123,7 @@
bindings,
}))
}
+ #[must_use]
pub fn extend_bound(self, new_bindings: GcHashMap<IStr, LazyVal>) -> Self {
let new_this = self.0.this.clone();
let new_super_obj = self.0.super_obj.clone();
@@ -135,7 +140,7 @@
let this = new_this.or_else(|| self.0.this.clone());
let super_obj = new_super_obj.or_else(|| self.0.super_obj.clone());
let mut new = GcHashMap::with_capacity(new_bindings.len());
- for (k, v) in new_bindings.0.into_iter() {
+ for (k, v) in new_bindings.0 {
new.insert(k, v.evaluate(s.clone(), this.clone(), super_obj.clone())?);
}
Ok(self.extend(new, new_dollar, this, super_obj))
crates/jrsonnet-evaluator/src/dynamic.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/dynamic.rs
+++ b/crates/jrsonnet-evaluator/src/dynamic.rs
@@ -8,12 +8,16 @@
pub fn new() -> Self {
Self(Cc::new(RefCell::new(None)))
}
+ /// # Panics
+ /// If wrapper is filled already
pub fn fill(self, value: T) {
assert!(self.0.borrow().is_none(), "wrapper is filled already");
self.0.borrow_mut().replace(value);
}
}
impl<T: Clone + Trace + 'static> FutureWrapper<T> {
+ /// # Panics
+ /// If wrapper is not yet filled
pub fn unwrap(&self) -> T {
self.0.borrow().as_ref().cloned().unwrap()
}
crates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/error.rs
+++ b/crates/jrsonnet-evaluator/src/error.rs
@@ -96,7 +96,8 @@
#[error(
"syntax error: expected {}, got {:?}",
.error.expected,
- .source_code.chars().nth(error.location.offset).map(|c| c.to_string()).unwrap_or_else(|| "EOF".into())
+ .source_code.chars().nth(error.location.offset)
+ .map_or_else(|| "EOF".into(), |c| c.to_string())
)]
ImportSyntaxError {
#[skip_trace]
@@ -190,7 +191,7 @@
impl Debug for LocError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", self.0 .0)?;
- for el in self.0 .1 .0.iter() {
+ for el in &self.0 .1 .0 {
writeln!(f, "\t{:?}", el)?;
}
Ok(())
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -23,8 +23,6 @@
pub fn evaluate_binding_in_future(b: &BindSpec, fctx: FutureWrapper<Context>) -> LazyVal {
let b = b.clone();
if let Some(params) = &b.params {
- let params = params.clone();
-
#[derive(Trace)]
struct LazyMethodBinding {
fctx: FutureWrapper<Context>,
@@ -43,6 +41,8 @@
}
}
+ let params = params.clone();
+
LazyVal::new(TraceBox(Box::new(LazyMethodBinding {
fctx,
name: b.name.clone(),
@@ -69,11 +69,10 @@
}
}
+#[allow(clippy::too_many_lines)]
pub fn evaluate_binding(b: &BindSpec, cctx: ContextCreator) -> (IStr, LazyBinding) {
let b = b.clone();
if let Some(params) = &b.params {
- let params = params.clone();
-
#[derive(Trace)]
struct BindableMethodLazyVal {
this: Option<ObjValue>,
@@ -121,6 +120,8 @@
}
}
+ let params = params.clone();
+
(
b.name.clone(),
LazyBinding::Bindable(Cc::new(TraceBox(Box::new(BindableMethod {
@@ -227,7 +228,7 @@
None => callback(ctx)?,
Some(CompSpec::IfSpec(IfSpecData(cond))) => {
if bool::from_untyped(evaluate(s.clone(), ctx.clone(), cond)?, s.clone())? {
- evaluate_comp(s, ctx, &specs[1..], callback)?
+ evaluate_comp(s, ctx, &specs[1..], callback)?;
}
}
Some(CompSpec::ForSpec(ForSpecData(var, expr))) => {
@@ -239,7 +240,7 @@
ctx.clone().with_var(var.clone(), item?.clone()),
&specs[1..],
callback,
- )?
+ )?;
}
}
_ => throw!(InComprehensionCanOnlyIterateOverArray),
@@ -249,6 +250,7 @@
Ok(())
}
+#[allow(clippy::too_many_lines)]
pub fn evaluate_member_list_object(s: State, ctx: Context, members: &[Member]) -> Result<ObjValue> {
let new_bindings = FutureWrapper::new();
let future_this = FutureWrapper::new();
@@ -278,12 +280,6 @@
visibility,
value,
}) => {
- let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;
- if name.is_none() {
- continue;
- }
- let name = name.unwrap();
-
#[derive(Trace)]
struct ObjMemberBinding {
cctx: ContextCreator,
@@ -305,6 +301,14 @@
)?))
}
}
+
+ let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;
+ let name = if let Some(name) = name {
+ name
+ } else {
+ continue;
+ };
+
builder
.member(name.clone())
.with_add(*plus)
@@ -325,11 +329,6 @@
value,
..
}) => {
- let name = evaluate_field_name(s.clone(), ctx.clone(), name)?;
- if name.is_none() {
- continue;
- }
- let name = name.unwrap();
#[derive(Trace)]
struct ObjMemberBinding {
cctx: ContextCreator,
@@ -352,6 +351,13 @@
)))
}
}
+
+ let name = if let Some(name) = evaluate_field_name(s.clone(), ctx.clone(), name)? {
+ name
+ } else {
+ continue;
+ };
+
builder
.member(name.clone())
.hide()
@@ -505,24 +511,24 @@
throw!(AssertionFailed(
evaluate(s.clone(), ctx, msg)?.to_string(s.clone())?
));
- } else {
- throw!(AssertionFailed(Val::Null.to_string(s.clone())?));
}
+ throw!(AssertionFailed(Val::Null.to_string(s.clone())?));
},
- )?
+ )?;
}
Ok(())
}
-pub fn evaluate_named(s: State, ctx: Context, lexpr: &LocExpr, name: IStr) -> Result<Val> {
+pub fn evaluate_named(s: State, ctx: Context, expr: &LocExpr, name: IStr) -> Result<Val> {
use Expr::*;
- let LocExpr(expr, _loc) = lexpr;
- Ok(match &**expr {
+ let LocExpr(raw_expr, _loc) = expr;
+ Ok(match &**raw_expr {
Function(params, body) => evaluate_method(ctx, name, params.clone(), body.clone()),
- _ => evaluate(s, ctx, lexpr)?,
+ _ => evaluate(s, ctx, expr)?,
})
}
+#[allow(clippy::too_many_lines)]
pub fn evaluate(s: State, ctx: Context, expr: &LocExpr) -> Result<Val> {
use Expr::*;
let LocExpr(expr, loc) = expr;
@@ -532,10 +538,11 @@
Val::Obj(ctx.this().clone().ok_or(CantUseSelfOutsideOfObject)?)
}
Literal(LiteralType::Super) => Val::Obj(
- ctx.super_obj()
- .clone()
- .ok_or(NoSuperFound)?
- .with_this(ctx.this().clone().unwrap()),
+ ctx.super_obj().clone().ok_or(NoSuperFound)?.with_this(
+ ctx.this()
+ .clone()
+ .expect("if super exists - then this should to"),
+ ),
),
Literal(LiteralType::Dollar) => {
Val::Obj(ctx.dollar().clone().ok_or(NoTopLevelObjectFound)?)
@@ -699,9 +706,6 @@
}
}
Slice(value, desc) => {
- let indexable = evaluate(s.clone(), ctx.clone(), value)?;
- let loc = CallLocation::new(loc);
-
fn parse_idx<T: Typed>(
loc: CallLocation,
s: State,
@@ -720,6 +724,9 @@
}
}
+ let indexable = evaluate(s.clone(), ctx.clone(), value)?;
+ let loc = CallLocation::new(loc);
+
let start = parse_idx(loc, s.clone(), &ctx, &desc.start, "start")?;
let end = parse_idx(loc, s.clone(), &ctx, &desc.end, "end")?;
let step = parse_idx(loc, s, &ctx, &desc.step, "step")?;
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -1,3 +1,5 @@
+use std::cmp::Ordering;
+
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
use crate::{
@@ -11,7 +13,7 @@
Ok(match (op, b) {
(Not, Bool(v)) => Bool(!v),
(Minus, Num(n)) => Num(-*n),
- (BitNot, Num(n)) => Num(!(*n as i32) as f64),
+ (BitNot, Num(n)) => Num(f64::from(!(*n as i32))),
(op, o) => throw!(UnaryOperatorDoesNotOperateOnType(op, o.value_type())),
})
}
@@ -22,8 +24,8 @@
(Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),
// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
- (Num(n), Str(o)) => Str(format!("{}{}", n, o).into()),
- (Str(o), Num(n)) => Str(format!("{}{}", o, n).into()),
+ (Num(a), Str(b)) => Str(format!("{a}{b}").into()),
+ (Str(a), Num(b)) => Str(format!("{a}{b}").into()),
(Str(a), o) => Str(format!("{}{}", a, o.clone().to_string(s)?).into()),
(o, Str(a)) => Str(format!("{}{}", o.clone().to_string(s)?, a).into()),
@@ -47,7 +49,12 @@
pub fn evaluate_mod_op(s: State, a: &Val, b: &Val) -> Result<Val> {
use Val::*;
match (a, b) {
- (Num(a), Num(b)) => Ok(Num(a % b)),
+ (Num(a), Num(b)) => {
+ if *b == 0.0 {
+ throw!(DivisionByZero)
+ }
+ Ok(Num(a % b))
+ }
(Str(str), vals) => {
String::into_untyped(std_format(s.clone(), str.clone(), vals.clone())?, s)
}
@@ -75,6 +82,32 @@
})
}
+pub fn evaluate_compare_op(s: State, a: &Val, op: BinaryOpType, b: &Val) -> Result<Ordering> {
+ use Val::*;
+ Ok(match (a, b) {
+ (Str(a), Str(b)) => a.cmp(b),
+ (Num(a), Num(b)) => a.partial_cmp(b).expect("jsonnet numbers are non NaN"),
+ (Arr(a), Arr(b)) => {
+ let ai = a.iter(s.clone());
+ let bi = b.iter(s.clone());
+
+ for (a, b) in ai.zip(bi) {
+ let ord = evaluate_compare_op(s.clone(), &a?, op, &b?)?;
+ if !ord.is_eq() {
+ return Ok(ord);
+ }
+ }
+
+ a.len().cmp(&b.len())
+ }
+ (_, _) => throw!(BinaryOperatorDoesNotOperateOnValues(
+ op,
+ a.value_type(),
+ b.value_type()
+ )),
+ })
+}
+
pub fn evaluate_binary_op_normal(s: State, a: &Val, op: BinaryOpType, b: &Val) -> Result<Val> {
use BinaryOpType::*;
use Val::*;
@@ -84,6 +117,11 @@
(a, Eq, b) => Bool(equals(s, a, b)?),
(a, Neq, b) => Bool(!equals(s, a, b)?),
+ (a, Lt, b) => Bool(evaluate_compare_op(s, a, Lt, b)?.is_lt()),
+ (a, Gt, b) => Bool(evaluate_compare_op(s, a, Gt, b)?.is_gt()),
+ (a, Lte, b) => Bool(evaluate_compare_op(s, a, Lte, b)?.is_le()),
+ (a, Gte, b) => Bool(evaluate_compare_op(s, a, Gte, b)?.is_ge()),
+
(Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
(a, Mod, b) => evaluate_mod_op(s, a, b)?,
@@ -92,17 +130,11 @@
// Bool X Bool
(Bool(a), And, Bool(b)) => Bool(*a && *b),
(Bool(a), Or, Bool(b)) => Bool(*a || *b),
-
- // Str X Str
- (Str(v1), Lt, Str(v2)) => Bool(v1 < v2),
- (Str(v1), Gt, Str(v2)) => Bool(v1 > v2),
- (Str(v1), Lte, Str(v2)) => Bool(v1 <= v2),
- (Str(v1), Gte, Str(v2)) => Bool(v1 >= v2),
// Num X Num
(Num(v1), Mul, Num(v2)) => Val::new_checked_num(v1 * v2)?,
(Num(v1), Div, Num(v2)) => {
- if *v2 <= f64::EPSILON {
+ if *v2 == 0.0 {
throw!(DivisionByZero)
}
Val::new_checked_num(v1 / v2)?
@@ -110,25 +142,20 @@
(Num(v1), Sub, Num(v2)) => Val::new_checked_num(v1 - v2)?,
- (Num(v1), Lt, Num(v2)) => Bool(v1 < v2),
- (Num(v1), Gt, Num(v2)) => Bool(v1 > v2),
- (Num(v1), Lte, Num(v2)) => Bool(v1 <= v2),
- (Num(v1), Gte, Num(v2)) => Bool(v1 >= v2),
-
- (Num(v1), BitAnd, Num(v2)) => Num(((*v1 as i32) & (*v2 as i32)) as f64),
- (Num(v1), BitOr, Num(v2)) => Num(((*v1 as i32) | (*v2 as i32)) as f64),
- (Num(v1), BitXor, Num(v2)) => Num(((*v1 as i32) ^ (*v2 as i32)) as f64),
+ (Num(v1), BitAnd, Num(v2)) => Num(f64::from((*v1 as i32) & (*v2 as i32))),
+ (Num(v1), BitOr, Num(v2)) => Num(f64::from((*v1 as i32) | (*v2 as i32))),
+ (Num(v1), BitXor, Num(v2)) => Num(f64::from((*v1 as i32) ^ (*v2 as i32))),
(Num(v1), Lhs, Num(v2)) => {
if *v2 < 0.0 {
throw!(RuntimeError("shift by negative exponent".into()))
}
- Num(((*v1 as i32) << (*v2 as i32)) as f64)
+ Num(f64::from((*v1 as i32) << (*v2 as i32)))
}
(Num(v1), Rhs, Num(v2)) => {
if *v2 < 0.0 {
throw!(RuntimeError("shift by negative exponent".into()))
}
- Num(((*v1 as i32) >> (*v2 as i32)) as f64)
+ Num(f64::from((*v1 as i32) >> (*v2 as i32)))
}
_ => throw!(BinaryOperatorDoesNotOperateOnValues(
crates/jrsonnet-evaluator/src/function.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function.rs
+++ b/crates/jrsonnet-evaluator/src/function.rs
@@ -145,7 +145,7 @@
tailstrict: bool,
handler: &mut dyn FnMut(&IStr, LazyVal) -> Result<()>,
) -> Result<()> {
- for (name, arg) in self.named.iter() {
+ for (name, arg) in &self.named {
handler(
name,
if tailstrict {
@@ -162,8 +162,8 @@
}
fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
- for (name, _) in self.named.iter() {
- handler(name)
+ for (name, _) in &self.named {
+ handler(name);
}
}
}
@@ -231,7 +231,7 @@
}
}
-impl<A: ArgLike> ArgsLike for HashMap<IStr, A> {
+impl<A: ArgLike, S> ArgsLike for HashMap<IStr, A, S> {
fn unnamed_len(&self) -> usize {
0
}
@@ -325,7 +325,7 @@
}
fn named_names(&self, handler: &mut dyn FnMut(&IStr)) {
- (*self).named_names(handler)
+ (*self).named_names(handler);
}
}
@@ -381,18 +381,13 @@
if passed_args.contains_key(¶m.0.clone()) {
continue;
}
- LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {
- ctx: fctx.clone(),
- name: param.0.clone(),
- value: param.1.clone().unwrap(),
- })));
defaults.insert(
param.0.clone(),
LazyVal::new(TraceBox(Box::new(EvaluateNamedLazyVal {
ctx: fctx.clone(),
name: param.0.clone(),
- value: param.1.clone().unwrap(),
+ value: param.1.clone().expect("default exists"),
}))),
);
filled_args += 1;
@@ -447,7 +442,7 @@
// const INST: &'static Self;
}
-/// You shouldn't probally use this function, use jrsonnet_macros::builtin instead
+/// You shouldn't probally use this function, use `jrsonnet_macros::builtin` instead
///
/// ## Parameters
/// * `ctx`: used for passed argument expressions' execution and for body execution (if `body_ctx` is not set)
@@ -518,10 +513,6 @@
/// Creates Context, which has all argument default values applied
/// and with unbound values causing error to be returned
pub fn parse_default_function_call(body_ctx: Context, params: &ParamsDesc) -> Context {
- let fctx = Context::new_future();
-
- let mut bindings = GcHashMap::new();
-
#[derive(Trace)]
struct DependsOnUnbound(IStr);
impl LazyValValue for DependsOnUnbound {
@@ -530,6 +521,10 @@
}
}
+ let fctx = Context::new_future();
+
+ let mut bindings = GcHashMap::new();
+
for param in params.iter() {
if let Some(v) = ¶m.1 {
bindings.insert(
crates/jrsonnet-evaluator/src/gc.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/gc.rs
+++ b/crates/jrsonnet-evaluator/src/gc.rs
@@ -1,6 +1,7 @@
/// Macros to help deal with Gc
use std::{
borrow::{Borrow, BorrowMut},
+ collections::{HashMap, HashSet},
hash::BuildHasherDefault,
ops::{Deref, DerefMut},
};
@@ -14,7 +15,7 @@
impl<T: ?Sized + Trace> Trace for TraceBox<T> {
fn trace(&self, tracer: &mut Tracer) {
- self.0.trace(tracer)
+ self.0.trace(tracer);
}
fn is_type_tracked() -> bool {
@@ -70,7 +71,7 @@
pub struct GcHashSet<V>(pub FxHashSet<V>);
impl<V> GcHashSet<V> {
pub fn new() -> Self {
- Self(Default::default())
+ Self(HashSet::default())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(FxHashSet::with_capacity_and_hasher(
@@ -111,7 +112,7 @@
pub struct GcHashMap<K, V>(pub FxHashMap<K, V>);
impl<K, V> GcHashMap<K, V> {
pub fn new() -> Self {
- Self(Default::default())
+ Self(HashMap::default())
}
pub fn with_capacity(capacity: usize) -> Self {
Self(FxHashMap::with_capacity_and_hasher(
crates/jrsonnet-evaluator/src/import.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/import.rs
+++ b/crates/jrsonnet-evaluator/src/import.rs
@@ -79,7 +79,7 @@
if direct.exists() {
Ok(direct.into())
} else {
- for library_path in self.library_paths.iter() {
+ for library_path in &self.library_paths {
let mut cloned = library_path.clone();
cloned.push(path);
if cloned.exists() {
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth1#![warn(clippy::all, clippy::nursery)]2#![allow(3 macro_expanded_macro_exports_accessed_by_absolute_paths,4 clippy::ptr_arg5)]67// For jrsonnet-macros8extern crate self as jrsonnet_evaluator;910mod builtin;11mod ctx;12mod dynamic;13pub mod error;14mod evaluate;15pub mod function;16pub mod gc;17mod import;18mod integrations;19mod map;20pub mod native;21mod obj;22pub mod trace;23pub mod typed;24pub mod val;2526use std::{27 cell::{Ref, RefCell, RefMut},28 collections::HashMap,29 fmt::Debug,30 path::{Path, PathBuf},31 rc::Rc,32};3334pub use ctx::*;35pub use dynamic::*;36use error::{Error::*, LocError, Result, StackTraceElement};37pub use evaluate::*;38use function::{Builtin, CallLocation, TlaArg};39use gc::{GcHashMap, TraceBox};40use gcmodule::{Cc, Trace, Weak};41pub use import::*;42pub use jrsonnet_interner::IStr;43pub use jrsonnet_parser as parser;44use jrsonnet_parser::*;45pub use obj::*;46use trace::{location_to_offset, offset_to_location, CodeLocation, CompactFormat, TraceFormat};47pub use val::{LazyVal, ManifestFormat, Val};4849pub trait Bindable: Trace + 'static {50 fn bind(51 &self,52 s: State,53 this: Option<ObjValue>,54 super_obj: Option<ObjValue>,55 ) -> Result<LazyVal>;56}5758#[derive(Clone, Trace)]59pub enum LazyBinding {60 Bindable(Cc<TraceBox<dyn Bindable>>),61 Bound(LazyVal),62}6364impl Debug for LazyBinding {65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {66 write!(f, "LazyBinding")67 }68}69impl LazyBinding {70 pub fn evaluate(71 &self,72 s: State,73 this: Option<ObjValue>,74 super_obj: Option<ObjValue>,75 ) -> Result<LazyVal> {76 match self {77 Self::Bindable(v) => v.bind(s, this, super_obj),78 Self::Bound(v) => Ok(v.clone()),79 }80 }81}8283pub struct EvaluationSettings {84 /// Limits recursion by limiting the number of stack frames85 pub max_stack: usize,86 /// Limits amount of stack trace items preserved87 pub max_trace: usize,88 /// Used for s`td.extVar`89 pub ext_vars: HashMap<IStr, Val>,90 /// Used for ext.native91 pub ext_natives: HashMap<IStr, Cc<TraceBox<dyn Builtin>>>,92 /// TLA vars93 pub tla_vars: HashMap<IStr, TlaArg>,94 /// Global variables are inserted in default context95 pub globals: HashMap<IStr, Val>,96 /// Used to resolve file locations/contents97 pub import_resolver: Box<dyn ImportResolver>,98 /// Used in manifestification functions99 pub manifest_format: ManifestFormat,100 /// Used for bindings101 pub trace_format: Box<dyn TraceFormat>,102}103impl Default for EvaluationSettings {104 fn default() -> Self {105 Self {106 max_stack: 200,107 max_trace: 20,108 globals: Default::default(),109 ext_vars: Default::default(),110 ext_natives: Default::default(),111 tla_vars: Default::default(),112 import_resolver: Box::new(DummyImportResolver),113 manifest_format: ManifestFormat::Json {114 padding: 4,115 #[cfg(feature = "exp-preserve-order")]116 preserve_order: false,117 },118 trace_format: Box::new(CompactFormat {119 padding: 4,120 resolver: trace::PathResolver::Absolute,121 }),122 }123 }124}125126#[derive(Default)]127struct EvaluationData {128 /// Used for stack overflow detection, stacktrace is populated on unwind129 stack_depth: usize,130 /// Updated every time stack entry is popt131 stack_generation: usize,132133 breakpoints: Breakpoints,134 /// Contains file source codes and evaluation results for imports and pretty-printed stacktraces135 files: GcHashMap<Rc<Path>, FileData>,136 str_files: GcHashMap<Rc<Path>, IStr>,137 bin_files: GcHashMap<Rc<Path>, Rc<[u8]>>,138}139140pub struct FileData {141 source_code: IStr,142 parsed: LocExpr,143 evaluated: Option<Val>,144}145146#[allow(clippy::type_complexity)]147pub struct Breakpoint {148 loc: ExprLocation,149 collected: RefCell<HashMap<usize, (usize, Vec<Result<Val>>)>>,150}151#[derive(Default)]152struct Breakpoints(Vec<Rc<Breakpoint>>);153impl Breakpoints {154 fn insert(155 &self,156 stack_depth: usize,157 stack_generation: usize,158 loc: &ExprLocation,159 result: Result<Val>,160 ) -> Result<Val> {161 if self.0.is_empty() {162 return result;163 }164 for item in self.0.iter() {165 if item.loc.belongs_to(loc) {166 let mut collected = item.collected.borrow_mut();167 let (depth, vals) = collected.entry(stack_generation).or_default();168 if stack_depth > *depth {169 vals.clear();170 }171 vals.push(result.clone());172 }173 }174 result175 }176}177178#[derive(Default)]179pub struct EvaluationStateInternals {180 /// Internal state181 data: RefCell<EvaluationData>,182 /// Settings, safe to change at runtime183 settings: RefCell<EvaluationSettings>,184}185186/// Maintains stack trace and import resolution187#[derive(Default, Clone)]188pub struct State(Rc<EvaluationStateInternals>);189190impl State {191 /// Parses and adds file as loaded192 pub fn add_file(&self, path: Rc<Path>, source_code: IStr) -> Result<LocExpr> {193 let parsed = parse(194 &source_code,195 &ParserSettings {196 file_name: path.clone(),197 },198 )199 .map_err(|error| ImportSyntaxError {200 error: Box::new(error),201 path: path.to_owned(),202 source_code: source_code.clone(),203 })?;204 self.add_parsed_file(path, source_code, parsed.clone())?;205206 Ok(parsed)207 }208209 pub fn reset_evaluation_state(&self, name: &Path) {210 self.data_mut()211 .files212 .get_mut(name)213 .unwrap()214 .evaluated215 .take();216 }217218 /// Adds file by source code and parsed expr219 pub fn add_parsed_file(220 &self,221 name: Rc<Path>,222 source_code: IStr,223 parsed: LocExpr,224 ) -> Result<()> {225 self.data_mut().files.insert(226 name,227 FileData {228 source_code,229 parsed,230 evaluated: None,231 },232 );233234 Ok(())235 }236 pub fn get_source(&self, name: &Path) -> Option<IStr> {237 let ro_map = &self.data().files;238 ro_map.get(name).map(|value| value.source_code.clone())239 }240 pub fn map_source_locations(&self, file: &Path, locs: &[usize]) -> Vec<CodeLocation> {241 offset_to_location(&self.get_source(file).unwrap_or_else(|| "".into()), locs)242 }243 pub fn map_from_source_location(244 &self,245 file: &Path,246 line: usize,247 column: usize,248 ) -> Option<usize> {249 location_to_offset(&self.get_source(file).unwrap(), line, column)250 }251 pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {252 let file_path = self.resolve_file(from, path)?;253 {254 let data = self.data();255 let files = &data.files;256 if files.contains_key(&file_path as &Path) {257 drop(data);258 return self.evaluate_loaded_file_raw(&file_path);259 }260 }261 let contents = self.load_file_str(&file_path)?;262 self.add_file(file_path.clone(), contents)?;263 self.evaluate_loaded_file_raw(&file_path)264 }265 pub(crate) fn import_file_str(&self, from: &Path, path: &Path) -> Result<IStr> {266 let path = self.resolve_file(from, path)?;267 if !self.data().str_files.contains_key(&path) {268 let file_str = self.load_file_str(&path)?;269 self.data_mut().str_files.insert(path.clone(), file_str);270 }271 Ok(self.data().str_files.get(&path).cloned().unwrap())272 }273 pub(crate) fn import_file_bin(&self, from: &Path, path: &Path) -> Result<Rc<[u8]>> {274 let path = self.resolve_file(from, path)?;275 if !self.data().bin_files.contains_key(&path) {276 let file_bin = self.load_file_bin(&path)?;277 self.data_mut().bin_files.insert(path.clone(), file_bin);278 }279 Ok(self.data().bin_files.get(&path).cloned().unwrap())280 }281282 fn evaluate_loaded_file_raw(&self, name: &Path) -> Result<Val> {283 let expr: LocExpr = {284 let ro_map = &self.data().files;285 let value = ro_map286 .get(name)287 .unwrap_or_else(|| panic!("file not added: {:?}", name));288 if let Some(ref evaluated) = value.evaluated {289 return Ok(evaluated.clone());290 }291 value.parsed.clone()292 };293 let value = evaluate(self.clone(), self.create_default_context(), &expr)?;294 {295 self.data_mut()296 .files297 .get_mut(name)298 .unwrap()299 .evaluated300 .replace(value.clone());301 }302 Ok(value)303 }304305 /// Adds standard library global variable (std) to this evaluator306 pub fn with_stdlib(&self) -> &Self {307 use jrsonnet_stdlib::STDLIB_STR;308 let std_path: Rc<Path> = PathBuf::from("std.jsonnet").into();309310 self.add_parsed_file(311 std_path.clone(),312 STDLIB_STR.to_owned().into(),313 builtin::get_parsed_stdlib(),314 )315 .unwrap();316 let val = self.evaluate_loaded_file_raw(&std_path).unwrap();317 self.settings_mut().globals.insert("std".into(), val);318 self319 }320321 /// Creates context with all passed global variables322 pub fn create_default_context(&self) -> Context {323 let globals = &self.settings().globals;324 let mut new_bindings = GcHashMap::with_capacity(globals.len());325 for (name, value) in globals.iter() {326 new_bindings.insert(name.clone(), LazyVal::new_resolved(value.clone()));327 }328 Context::new().extend_bound(new_bindings)329 }330331 /// Executes code creating a new stack frame332 pub fn push<T>(333 &self,334 e: CallLocation,335 frame_desc: impl FnOnce() -> String,336 f: impl FnOnce() -> Result<T>,337 ) -> Result<T> {338 {339 let mut data = self.data_mut();340 let stack_depth = &mut data.stack_depth;341 if *stack_depth > self.max_stack() {342 // Error creation uses data, so i drop guard here343 drop(data);344 throw!(StackOverflow);345 } else {346 *stack_depth += 1;347 }348 }349 let result = f();350 {351 let mut data = self.data_mut();352 data.stack_depth -= 1;353 data.stack_generation += 1;354 }355 if let Err(mut err) = result {356 err.trace_mut().0.push(StackTraceElement {357 location: e.0.cloned(),358 desc: frame_desc(),359 });360 return Err(err);361 }362 result363 }364365 /// Executes code creating a new stack frame366 pub fn push_val(367 &self,368 e: &ExprLocation,369 frame_desc: impl FnOnce() -> String,370 f: impl FnOnce() -> Result<Val>,371 ) -> Result<Val> {372 {373 let mut data = self.data_mut();374 let stack_depth = &mut data.stack_depth;375 if *stack_depth > self.max_stack() {376 // Error creation uses data, so i drop guard here377 drop(data);378 throw!(StackOverflow);379 } else {380 *stack_depth += 1;381 }382 }383 let mut result = f();384 {385 let mut data = self.data_mut();386 data.stack_depth -= 1;387 data.stack_generation += 1;388 result = data389 .breakpoints390 .insert(data.stack_depth, data.stack_generation, e, result);391 }392 if let Err(mut err) = result {393 err.trace_mut().0.push(StackTraceElement {394 location: Some(e.clone()),395 desc: frame_desc(),396 });397 return Err(err);398 }399 result400 }401 /// Executes code creating a new stack frame402 pub fn push_description<T>(403 &self,404 frame_desc: impl FnOnce() -> String,405 f: impl FnOnce() -> Result<T>,406 ) -> Result<T> {407 {408 let mut data = self.data_mut();409 let stack_depth = &mut data.stack_depth;410 if *stack_depth > self.max_stack() {411 // Error creation uses data, so i drop guard here412 drop(data);413 throw!(StackOverflow);414 } else {415 *stack_depth += 1;416 }417 }418 let result = f();419 {420 let mut data = self.data_mut();421 data.stack_depth -= 1;422 data.stack_generation += 1;423 }424 if let Err(mut err) = result {425 err.trace_mut().0.push(StackTraceElement {426 location: None,427 desc: frame_desc(),428 });429 return Err(err);430 }431 result432 }433434 pub fn stringify_err(&self, e: &LocError) -> String {435 let mut out = String::new();436 self.settings()437 .trace_format438 .write_trace(&mut out, self, e)439 .unwrap();440 out441 }442443 pub fn manifest(&self, val: Val) -> Result<IStr> {444 self.push_description(445 || "manifestification".to_string(),446 || val.manifest(self.clone(), &self.manifest_format()),447 )448 }449 pub fn manifest_multi(&self, val: Val) -> Result<Vec<(IStr, IStr)>> {450 val.manifest_multi(self.clone(), &self.manifest_format())451 }452 pub fn manifest_stream(&self, val: Val) -> Result<Vec<IStr>> {453 val.manifest_stream(self.clone(), &self.manifest_format())454 }455456 /// If passed value is function then call with set TLA457 pub fn with_tla(&self, val: Val) -> Result<Val> {458 Ok(match val {459 Val::Func(func) => self.push_description(460 || "during TLA call".to_owned(),461 || {462 func.evaluate(463 self.clone(),464 self.create_default_context(),465 CallLocation::native(),466 &self.settings().tla_vars,467 true,468 )469 },470 )?,471 v => v,472 })473 }474}475476/// Internals477impl State {478 fn data(&self) -> Ref<EvaluationData> {479 self.0.data.borrow()480 }481 fn data_mut(&self) -> RefMut<EvaluationData> {482 self.0.data.borrow_mut()483 }484 pub fn settings(&self) -> Ref<EvaluationSettings> {485 self.0.settings.borrow()486 }487 pub fn settings_mut(&self) -> RefMut<EvaluationSettings> {488 self.0.settings.borrow_mut()489 }490}491492/// Raw methods evaluate passed values but don't perform TLA execution493impl State {494 pub fn evaluate_file_raw(&self, name: &Path) -> Result<Val> {495 self.import_file(&std::env::current_dir().expect("cwd"), name)496 }497 pub fn evaluate_file_raw_nocwd(&self, name: &Path) -> Result<Val> {498 self.import_file(&PathBuf::from("."), name)499 }500 /// Parses and evaluates the given snippet501 pub fn evaluate_snippet_raw(&self, source: Rc<Path>, code: IStr) -> Result<Val> {502 let parsed = parse(503 &code,504 &ParserSettings {505 file_name: source.clone(),506 },507 )508 .map_err(|e| ImportSyntaxError {509 path: source.clone(),510 source_code: code.clone(),511 error: Box::new(e),512 })?;513 self.add_parsed_file(source, code, parsed.clone())?;514 self.evaluate_expr_raw(parsed)515 }516 /// Evaluates the parsed expression517 pub fn evaluate_expr_raw(&self, code: LocExpr) -> Result<Val> {518 evaluate(self.clone(), self.create_default_context(), &code)519 }520}521522/// Settings utilities523impl State {524 pub fn add_ext_var(&self, name: IStr, value: Val) {525 self.settings_mut().ext_vars.insert(name, value);526 }527 pub fn add_ext_str(&self, name: IStr, value: IStr) {528 self.add_ext_var(name, Val::Str(value));529 }530 pub fn add_ext_code(&self, name: IStr, code: IStr) -> Result<()> {531 let value =532 self.evaluate_snippet_raw(PathBuf::from(format!("ext_code {}", name)).into(), code)?;533 self.add_ext_var(name, value);534 Ok(())535 }536537 pub fn add_tla(&self, name: IStr, value: Val) {538 self.settings_mut()539 .tla_vars540 .insert(name, TlaArg::Val(value));541 }542 pub fn add_tla_str(&self, name: IStr, value: IStr) {543 self.settings_mut()544 .tla_vars545 .insert(name, TlaArg::String(value));546 }547 pub fn add_tla_code(&self, name: IStr, code: IStr) -> Result<()> {548 let parsed = self.add_file(PathBuf::from(format!("tla_code {}", name)).into(), code)?;549 self.settings_mut()550 .tla_vars551 .insert(name, TlaArg::Code(parsed));552 Ok(())553 }554555 pub fn resolve_file(&self, from: &Path, path: &Path) -> Result<Rc<Path>> {556 self.settings().import_resolver.resolve_file(from, path)557 }558 pub fn load_file_str(&self, path: &Path) -> Result<IStr> {559 self.settings().import_resolver.load_file_str(path)560 }561 pub fn load_file_bin(&self, path: &Path) -> Result<Rc<[u8]>> {562 self.settings().import_resolver.load_file_bin(path)563 }564565 pub fn import_resolver(&self) -> Ref<dyn ImportResolver> {566 Ref::map(self.settings(), |s| &*s.import_resolver)567 }568 pub fn set_import_resolver(&self, resolver: Box<dyn ImportResolver>) {569 self.settings_mut().import_resolver = resolver;570 }571572 pub fn add_native(&self, name: IStr, cb: Cc<TraceBox<dyn Builtin>>) {573 self.settings_mut().ext_natives.insert(name, cb);574 }575576 pub fn manifest_format(&self) -> ManifestFormat {577 self.settings().manifest_format.clone()578 }579 pub fn set_manifest_format(&self, format: ManifestFormat) {580 self.settings_mut().manifest_format = format;581 }582583 pub fn trace_format(&self) -> Ref<dyn TraceFormat> {584 Ref::map(self.settings(), |s| &*s.trace_format)585 }586 pub fn set_trace_format(&self, format: Box<dyn TraceFormat>) {587 self.settings_mut().trace_format = format;588 }589590 pub fn max_trace(&self) -> usize {591 self.settings().max_trace592 }593 pub fn set_max_trace(&self, trace: usize) {594 self.settings_mut().max_trace = trace;595 }596597 pub fn max_stack(&self) -> usize {598 self.settings().max_stack599 }600 pub fn set_max_stack(&self, trace: usize) {601 self.settings_mut().max_stack = trace;602 }603}604605pub fn cc_ptr_eq<T>(a: &Cc<T>, b: &Cc<T>) -> bool {606 let a = a as &T;607 let b = b as &T;608 std::ptr::eq(a, b)609}610611fn weak_raw<T>(a: Weak<T>) -> *const () {612 unsafe { std::mem::transmute(a) }613}614fn weak_ptr_eq<T>(a: Weak<T>, b: Weak<T>) -> bool {615 std::ptr::eq(weak_raw(a), weak_raw(b))616}617618#[test]619fn weak_unsafe() {620 let a = Cc::new(1);621 let b = Cc::new(2);622623 let aw1 = a.clone().downgrade();624 let aw2 = a.clone().downgrade();625 let aw3 = a.clone().downgrade();626627 let bw = b.clone().downgrade();628629 assert!(weak_ptr_eq(aw1, aw2));630 assert!(!weak_ptr_eq(aw3, bw));631}632633#[cfg(test)]634pub mod tests {635 use std::{636 path::{Path, PathBuf},637 rc::Rc,638 };639640 use gcmodule::{Cc, Trace};641 use jrsonnet_parser::*;642643 use super::Val;644 use crate::{645 error::Error::*,646 function::{BuiltinParam, CallLocation},647 gc::TraceBox,648 native::NativeCallbackHandler,649 val::primitive_equals,650 State,651 };652653 #[test]654 #[should_panic]655 fn eval_state_stacktrace() {656 let state = State::default();657 state658 .push(659 CallLocation::new(&ExprLocation(PathBuf::from("test1.jsonnet").into(), 10, 20)),660 || "outer".to_owned(),661 || {662 state.push(663 CallLocation::new(&ExprLocation(664 PathBuf::from("test2.jsonnet").into(),665 30,666 40,667 )),668 || "inner".to_owned(),669 || Err(RuntimeError("".into()).into()),670 )?;671 Ok(Val::Null)672 },673 )674 .unwrap();675 }676677 #[test]678 fn eval_state_standard() {679 let state = State::default();680 state.with_stdlib();681 assert!(primitive_equals(682 &state683 .evaluate_snippet_raw(684 PathBuf::from("raw.jsonnet").into(),685 r#"std.assertEqual(std.base64("test"), "dGVzdA==")"#.into()686 )687 .unwrap(),688 &Val::Bool(true),689 )690 .unwrap());691 }692693 macro_rules! eval {694 ($str: expr) => {{695 let evaluator = State::default();696 evaluator.with_stdlib();697 evaluator698 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())699 .unwrap()700 }};701 }702 macro_rules! eval_json {703 ($str: expr) => {{704 let evaluator = State::default();705 evaluator.with_stdlib();706 evaluator707 .evaluate_snippet_raw(PathBuf::from("raw.jsonnet").into(), $str.into())708 .unwrap()709 .to_json(0)710 .unwrap()711 .replace("\n", "")712 }};713 }714715 /// Asserts given code returns `true`716 macro_rules! assert_eval {717 ($str: expr) => {718 assert!(primitive_equals(&eval!($str), &Val::Bool(true)).unwrap())719 };720 }721722 /// Asserts given code returns `false`723 macro_rules! assert_eval_neg {724 ($str: expr) => {725 assert!(primitive_equals(&eval!($str), &Val::Bool(false)).unwrap())726 };727 }728 macro_rules! assert_json {729 ($str: expr, $out: expr) => {730 assert_eq!(eval_json!($str), $out.replace("\t", ""))731 };732 }733734 /// Sanity checking, before trusting to another tests735 #[test]736 fn equality_operator() {737 assert_eval!("2 == 2");738 assert_eval_neg!("2 != 2");739 assert_eval!("2 != 3");740 assert_eval_neg!("2 == 3");741 assert_eval!("'Hello' == 'Hello'");742 assert_eval_neg!("'Hello' != 'Hello'");743 assert_eval!("'Hello' != 'World'");744 assert_eval_neg!("'Hello' == 'World'");745 }746747 #[test]748 fn math_evaluation() {749 assert_eval!("2 + 2 * 2 == 6");750 assert_eval!("3 + (2 + 2 * 2) == 9");751 }752753 #[test]754 fn string_concat() {755 assert_eval!("'Hello' + 'World' == 'HelloWorld'");756 assert_eval!("'Hello' * 3 == 'HelloHelloHello'");757 assert_eval!("'Hello' + 'World' * 3 == 'HelloWorldWorldWorld'");758 }759760 #[test]761 fn faster_join() {762 assert_eval!("std.join([0,0], [[1,2],[3,4],[5,6]]) == [1,2,0,0,3,4,0,0,5,6]");763 assert_eval!("std.join(',', ['1','2','3','4']) == '1,2,3,4'");764 }765766 #[test]767 fn function_contexts() {768 assert_eval!(769 r#"770 local k = {771 t(name = self.h): [self.h, name],772 h: 3,773 };774 local f = {775 t: k.t(),776 h: 4,777 };778 f.t[0] == f.t[1]779 "#780 );781 }782783 #[test]784 fn local() {785 assert_eval!("local a = 2; local b = 3; a + b == 5");786 assert_eval!("local a = 1, b = a + 1; a + b == 3");787 assert_eval!("local a = 1; local a = 2; a == 2");788 }789790 #[test]791 fn object_lazyness() {792 assert_json!("local a = {a:error 'test'}; {}", r#"{}"#);793 }794795 #[test]796 fn object_inheritance() {797 assert_json!("{a: self.b} + {b:3}", r#"{"a": 3,"b": 3}"#);798 }799800 #[test]801 fn object_assertion_success() {802 eval!("{assert \"a\" in self} + {a:2}");803 }804805 #[test]806 fn object_assertion_error() {807 eval!("{assert \"a\" in self}");808 }809810 #[test]811 fn lazy_args() {812 eval!("local test(a) = 2; test(error '3')");813 }814815 #[test]816 #[should_panic]817 fn tailstrict_args() {818 eval!("local test(a) = 2; test(error '3') tailstrict");819 }820821 #[test]822 #[should_panic]823 fn no_binding_error() {824 eval!("a");825 }826827 #[test]828 fn test_object() {829 assert_json!("{a:2}", r#"{"a": 2}"#);830 assert_json!("{a:2+2}", r#"{"a": 4}"#);831 assert_json!("{a:2}+{b:2}", r#"{"a": 2,"b": 2}"#);832 assert_json!("{b:3}+{b:2}", r#"{"b": 2}"#);833 assert_json!("{b:3}+{b+:2}", r#"{"b": 5}"#);834 assert_json!("local test='a'; {[test]:2}", r#"{"a": 2}"#);835 assert_json!(836 r#"837 {838 name: "Alice",839 welcome: "Hello " + self.name + "!",840 }841 "#,842 r#"{"name": "Alice","welcome": "Hello Alice!"}"#843 );844 assert_json!(845 r#"846 {847 name: "Alice",848 welcome: "Hello " + self.name + "!",849 } + {850 name: "Bob"851 }852 "#,853 r#"{"name": "Bob","welcome": "Hello Bob!"}"#854 );855 }856857 #[test]858 fn functions() {859 assert_json!(r#"local a = function(b, c = 2) b + c; a(2)"#, "4");860 assert_json!(861 r#"local a = function(b, c = "Dear") b + c + d, d = "World"; a("Hello")"#,862 r#""HelloDearWorld""#863 );864 }865866 #[test]867 fn local_methods() {868 assert_json!(r#"local a(b, c = 2) = b + c; a(2)"#, "4");869 assert_json!(870 r#"local a(b, c = "Dear") = b + c + d, d = "World"; a("Hello")"#,871 r#""HelloDearWorld""#872 );873 }874875 #[test]876 fn object_locals() {877 assert_json!(r#"{local a = 3, b: a}"#, r#"{"b": 3}"#);878 assert_json!(r#"{local a = 3, local c = a, b: c}"#, r#"{"b": 3}"#);879 assert_json!(880 r#"{local a = function (b) {[b]:4}, test: a("test")}"#,881 r#"{"test": {"test": 4}}"#882 );883 }884885 #[test]886 fn object_comp() {887 assert_json!(888 r#"{local t = "a", ["h"+i+"_"+z]: if "h"+(i-1)+"_"+z in self then t+1 else 0+t for i in [1,2,3] for z in [2,3,4] if z != i}"#,889 "{\"h1_2\": \"0a\",\"h1_3\": \"0a\",\"h1_4\": \"0a\",\"h2_3\": \"a1\",\"h2_4\": \"a1\",\"h3_2\": \"0a\",\"h3_4\": \"a1\"}"890 )891 }892893 #[test]894 fn direct_self() {895 println!(896 "{:#?}",897 eval!(898 r#"899 {900 local me = self,901 a: 3,902 b(): me.a,903 }904 "#905 )906 );907 }908909 #[test]910 fn indirect_self() {911 // `self` assigned to `me` was lost when being912 // referenced from field913 eval!(914 r#"{915 local me = self,916 a: 3,917 b: me.a,918 }.b"#919 );920 }921922 // We can't trust other tests (And official jsonnet testsuite), if assert is not working correctly923 #[test]924 fn std_assert_ok() {925 eval!("std.assertEqual(4.5 << 2, 16)");926 }927928 #[test]929 #[should_panic]930 fn std_assert_failure() {931 eval!("std.assertEqual(4.5 << 2, 15)");932 }933934 #[test]935 fn string_is_string() {936 assert!(primitive_equals(937 &eval!("local arr = 'hello'; (!std.isArray(arr)) && (!std.isString(arr))"),938 &Val::Bool(false),939 )940 .unwrap());941 }942943 #[test]944 fn base64_works() {945 assert_json!(r#"std.base64("test")"#, r#""dGVzdA==""#);946 }947948 #[test]949 fn utf8_chars() {950 assert_json!(951 r#"local c="😎";{c:std.codepoint(c),l:std.length(c)}"#,952 r#"{"c": 128526,"l": 1}"#953 )954 }955956 #[test]957 fn json() {958 assert_json!(959 r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#,960 r#""{\n\"a\": 3,\n\"b\": 4,\n\"c\": 6\n}""#961 );962 }963964 #[test]965 fn json_minified() {966 assert_json!(967 r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,968 r#""{\"a\":3,\"b\":4,\"c\":6}""#969 );970 }971972 #[test]973 fn parse_json() {974 assert_json!(975 r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,976 r#"{"a": -1,"b": 1,"c": 3.141,"d": []}"#977 );978 }979980 #[test]981 fn test() {982 assert_json!(983 r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#,984 "[[1,4],[1,5],[1,6],[2,4],[2,5],[2,6],[3,4],[3,5],[3,6]]"985 );986 }987988 #[test]989 fn sjsonnet() {990 eval!(991 r#"992 local x0 = {k: 1};993 local x1 = {k: x0.k + x0.k};994 local x2 = {k: x1.k + x1.k};995 local x3 = {k: x2.k + x2.k};996 local x4 = {k: x3.k + x3.k};997 local x5 = {k: x4.k + x4.k};998 local x6 = {k: x5.k + x5.k};999 local x7 = {k: x6.k + x6.k};1000 local x8 = {k: x7.k + x7.k};1001 local x9 = {k: x8.k + x8.k};1002 local x10 = {k: x9.k + x9.k};1003 local x11 = {k: x10.k + x10.k};1004 local x12 = {k: x11.k + x11.k};1005 local x13 = {k: x12.k + x12.k};1006 local x14 = {k: x13.k + x13.k};1007 local x15 = {k: x14.k + x14.k};1008 local x16 = {k: x15.k + x15.k};1009 local x17 = {k: x16.k + x16.k};1010 local x18 = {k: x17.k + x17.k};1011 local x19 = {k: x18.k + x18.k};1012 local x20 = {k: x19.k + x19.k};1013 local x21 = {k: x20.k + x20.k};1014 x21.k1015 "#1016 );1017 }10181019 // This test is commented out by default, because of huge compilation slowdown1020 // #[bench]1021 // fn bench_codegen(b: &mut Bencher) {1022 // b.iter(|| {1023 // #[allow(clippy::all)]1024 // let stdlib = {1025 // use jrsonnet_parser::*;1026 // include!(concat!(env!("OUT_DIR"), "/stdlib.rs"))1027 // };1028 // stdlib1029 // })1030 // }10311032 /*1033 #[bench]1034 fn bench_serialize(b: &mut Bencher) {1035 b.iter(|| {1036 bincode::deserialize::<jrsonnet_parser::LocExpr>(include_bytes!(concat!(1037 env!("OUT_DIR"),1038 "/stdlib.bincode"1039 )))1040 .expect("deserialize stdlib")1041 })1042 }10431044 #[bench]1045 fn bench_parse(b: &mut Bencher) {1046 b.iter(|| {1047 jrsonnet_parser::parse(1048 jrsonnet_stdlib::STDLIB_STR,1049 &jrsonnet_parser::ParserSettings {1050 loc_data: true,1051 file_name: Rc::new(PathBuf::from("std.jsonnet")),1052 },1053 )1054 })1055 }1056 */10571058 #[test]1059 fn equality() {1060 println!(1061 "{:?}",1062 jrsonnet_parser::parse(1063 "{ x: 1, y: 2 } == { x: 1, y: 2 }",1064 &ParserSettings {1065 file_name: PathBuf::from("equality").into(),1066 }1067 )1068 );1069 assert_eval!("{ x: 1, y: 2 } == { x: 1, y: 2 }")1070 }10711072 #[test]1073 fn native_ext() -> crate::error::Result<()> {1074 use super::native::NativeCallback;1075 let evaluator = State::default();10761077 evaluator.with_stdlib();10781079 #[derive(Trace)]1080 struct NativeAdd;1081 impl NativeCallbackHandler for NativeAdd {1082 fn call(&self, from: Option<Rc<Path>>, args: &[Val]) -> crate::error::Result<Val> {1083 assert_eq!(1084 &from.unwrap() as &Path,1085 &PathBuf::from("native_caller.jsonnet")1086 );1087 match (&args[0], &args[1]) {1088 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a + b)),1089 (_, _) => unreachable!(),1090 }1091 }1092 }1093 evaluator.settings_mut().ext_natives.insert(1094 "native_add".into(),1095 #[allow(deprecated)]1096 Cc::new(TraceBox(Box::new(NativeCallback::new(1097 vec![1098 BuiltinParam {1099 name: "a".into(),1100 has_default: false,1101 },1102 BuiltinParam {1103 name: "b".into(),1104 has_default: false,1105 },1106 ],1107 TraceBox(Box::new(NativeAdd)),1108 )))),1109 );1110 dbg!(evaluator.settings().ext_natives.keys().collect::<Vec<_>>());1111 evaluator.evaluate_snippet_raw(1112 PathBuf::from("native_caller.jsonnet").into(),1113 "std.assertEqual(std.native(\"native_add\")(1, 2), 3)".into(),1114 )?;1115 dbg!(evaluator.settings().ext_natives.keys().collect::<Vec<_>>());1116 Ok(())1117 }11181119 #[test]1120 fn constant_intrinsic() -> crate::error::Result<()> {1121 assert_eval!(1122 "local std2 = std; local std = std2 { primitiveEquals(a, b):: false }; 1 == 1"1123 );1124 Ok(())1125 }11261127 #[test]1128 fn standalone_super() -> crate::error::Result<()> {1129 assert_eval!(1130 r#"1131 local obj = {1132 a: 1,1133 b: 2,1134 c: 3,1135 };1136 local test = obj + {1137 fields: std.objectFields(super),1138 d: 5,1139 };1140 test.fields == ['a', 'b', 'c']1141 "#1142 );1143 Ok(())1144 }11451146 #[test]1147 fn comp_self() -> crate::error::Result<()> {1148 assert_eval!(1149 r#"1150 std.objectFields({1151 a:{1152 [name]: name for name in std.objectFields(self)1153 },1154 b: 2,1155 c: 3,1156 }.a) == ['a', 'b', 'c']1157 "#1158 );11591160 Ok(())1161 }11621163 struct TestImportResolver(Vec<u8>);1164 impl crate::import::ImportResolver for TestImportResolver {1165 fn resolve_file(&self, _: &Path, _: &Path) -> crate::error::Result<Rc<Path>> {1166 Ok(PathBuf::from("/test").into())1167 }11681169 fn load_file_contents(&self, _: &Path) -> crate::error::Result<Vec<u8>> {1170 Ok(self.0.clone())1171 }11721173 unsafe fn as_any(&self) -> &dyn std::any::Any {1174 panic!()1175 }11761177 fn load_file_bin(&self, _resolved: &Path) -> crate::error::Result<Rc<[u8]>> {1178 panic!()1179 }1180 }11811182 #[test]1183 fn issue_23() {1184 let state = State::default();1185 state.set_import_resolver(Box::new(TestImportResolver(r#"import "/test""#.into())));1186 let _ = state.evaluate_file_raw(&PathBuf::from("/test"));1187 }11881189 #[test]1190 fn issue_40() {1191 let state = State::default();1192 state.with_stdlib();11931194 let error = state1195 .evaluate_snippet_raw(1196 PathBuf::from("issue40.jsonnet").into(),1197 r#"1198 local conf = {1199 n: ""1200 };12011202 local result = conf + {1203 assert std.isNumber(self.n): "is number"1204 };12051206 std.manifestJsonEx(result, "")1207 "#1208 .into(),1209 )1210 .unwrap_err();1211 assert_eq!(error.error().to_string(), "assert failed: is number");1212 }12131214 #[test]1215 fn test_ascii_upper_lower() {1216 assert_eval!(r#"std.assertEqual(std.asciiUpper("aBc😀"), "ABC😀")"#);1217 assert_eval!(r#"std.assertEqual(std.asciiLower("aBc😀"), "abc😀")"#);1218 }12191220 #[test]1221 fn test_member() {1222 assert_eval!(r#"!std.member("", "")"#);1223 assert_eval!(r#"std.member("abc", "a")"#);1224 assert_eval!(r#"!std.member("abc", "d")"#);1225 assert_eval!(r#"!std.member([], "")"#);1226 assert_eval!(r#"std.member(["a", "b", "c"], "a")"#);1227 assert_eval!(r#"!std.member(["a", "b", "c"], "d")"#);1228 }12291230 #[test]1231 fn test_count() {1232 assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);1233 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);1234 assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);1235 }12361237 mod derive_typed {1238 use std::path::PathBuf;12391240 use crate::{typed::Typed, State};12411242 #[derive(PartialEq, Debug, Typed)]1243 struct MyTyped {1244 a: u32,1245 #[typed(rename = "b")]1246 c: String,1247 }12481249 #[test]1250 fn test() {1251 let es = State::default();1252 let val = eval!("{a: 14, b: 'Hello, world!'}");1253 let typed = MyTyped::try_from(val).unwrap();12541255 assert_eq!(1256 typed,1257 MyTyped {1258 a: 14,1259 c: "Hello, world!".to_string()1260 }1261 );1262 es.settings_mut()1263 .globals1264 .insert("mytyped".into(), typed.try_into().unwrap());12651266 let v = es1267 .evaluate_snippet_raw(1268 PathBuf::from("raw.jsonnet").into(),1269 "1270 mytyped == {a: 14, b: 'Hello, world!'}1271 "1272 .into(),1273 )1274 .unwrap()1275 .as_bool()1276 .unwrap();1277 assert!(v)1278 }1279 }1280}crates/jrsonnet-evaluator/src/map.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/map.rs
+++ b/crates/jrsonnet-evaluator/src/map.rs
@@ -34,8 +34,7 @@
.0
.parent
.as_ref()
- .map(|p| p.contains_key(key))
- .unwrap_or(false)
+ .map_or(false, |p| p.contains_key(key))
}
}
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -37,7 +37,7 @@
fn call(&self, s: State, ctx: Context, loc: CallLocation, args: &dyn ArgsLike) -> Result<Val> {
let args = parse_builtin_call(s.clone(), ctx, &self.params, args, true)?;
let mut out_args = Vec::with_capacity(self.params.len());
- for p in self.params.iter() {
+ for p in &self.params {
out_args.push(args[&p.name].evaluate(s.clone())?);
}
self.handler.call(s, loc.0.map(|l| l.0.clone()), &out_args)
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -2,6 +2,7 @@
cell::RefCell,
fmt::Debug,
hash::{Hash, Hasher},
+ ptr::addr_of,
};
use gcmodule::{Cc, Trace, Weak};
@@ -20,6 +21,11 @@
#[cfg(not(feature = "exp-preserve-order"))]
mod ordering {
+ #![allow(
+ // This module works as stub for preserve-order feature
+ clippy::unused_self,
+ )]
+
use gcmodule::Trace;
#[derive(Clone, Copy, Default, Debug, Trace)]
@@ -89,6 +95,7 @@
use ordering::*;
+#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Trace)]
pub struct ObjMember {
pub add: bool,
@@ -113,6 +120,7 @@
Errored(LocError),
}
+#[allow(clippy::module_name_repetitions)]
#[derive(Trace)]
#[force_tracking]
pub struct ObjValueInternals {
@@ -136,10 +144,11 @@
impl Eq for WeakObjValue {}
impl Hash for WeakObjValue {
fn hash<H: Hasher>(&self, hasher: &mut H) {
- hasher.write_usize(weak_raw(self.0.clone()) as usize)
+ hasher.write_usize(weak_raw(self.0.clone()) as usize);
}
}
+#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Trace)]
pub struct ObjValue(pub(crate) Cc<ObjValueInternals>);
impl Debug for ObjValue {
@@ -178,6 +187,7 @@
pub fn new_empty() -> Self {
Self::new(None, Cc::new(GcHashMap::new()), Cc::new(Vec::new()))
}
+ #[must_use]
pub fn extend_from(&self, super_obj: Self) -> Self {
match &self.0.super_obj {
None => Self::new(
@@ -200,6 +210,8 @@
pub fn extend_field(&mut self, name: IStr) -> ObjMemberBuilder<ExtendBuilder> {
ObjMemberBuilder::new(ExtendBuilder(self), name, FieldIndex::default())
}
+
+ #[must_use]
pub fn with_this(&self, this_obj: Self) -> Self {
Self(Cc::new(ObjValueInternals {
super_obj: self.0.super_obj.clone(),
@@ -222,11 +234,7 @@
if !self.0.this_entries.is_empty() {
return false;
}
- self.0
- .super_obj
- .as_ref()
- .map(|s| s.is_empty())
- .unwrap_or(true)
+ self.0.super_obj.as_ref().map_or(true, Self::is_empty)
}
/// Run callback for every field found in object
@@ -254,15 +262,15 @@
let new_sort_key = FieldSortKey::new(depth, member.original_index);
match member.visibility {
Visibility::Normal => {
- let entry = out.entry(name.to_owned());
+ let entry = out.entry(name.clone());
let v = entry.or_insert((true, new_sort_key));
v.1 = new_sort_key;
}
Visibility::Hidden => {
- out.insert(name.to_owned(), (false, new_sort_key));
+ out.insert(name.clone(), (false, new_sort_key));
}
Visibility::Unhide => {
- out.insert(name.to_owned(), (true, new_sort_key));
+ out.insert(name.clone(), (true, new_sort_key));
}
};
false
@@ -356,8 +364,7 @@
}
pub fn has_field(&self, name: IStr) -> bool {
self.field_visibility(name)
- .map(|v| v.is_visible())
- .unwrap_or(false)
+ .map_or(false, |v| v.is_visible())
}
pub fn get(&self, s: State, key: IStr) -> Result<Option<Val>> {
@@ -459,10 +466,11 @@
impl Eq for ObjValue {}
impl Hash for ObjValue {
fn hash<H: Hasher>(&self, hasher: &mut H) {
- hasher.write_usize(&*self.0 as *const _ as usize)
+ hasher.write_usize(addr_of!(*self.0) as usize);
}
}
+#[allow(clippy::module_name_repetitions)]
pub struct ObjValueBuilder {
super_obj: Option<ObjValue>,
map: GcHashMap<IStr, ObjMember>,
@@ -510,6 +518,7 @@
}
}
+#[allow(clippy::module_name_repetitions)]
#[must_use = "value not added unless binding() was called"]
pub struct ObjMemberBuilder<Kind> {
kind: Kind,
@@ -583,7 +592,7 @@
CallLocation(location.as_ref()),
|| format!("field <{}> initializtion", name.clone()),
|| throw!(DuplicateFieldName(name.clone())),
- )?
+ )?;
}
Ok(())
}
@@ -592,14 +601,14 @@
pub struct ExtendBuilder<'v>(&'v mut ObjValue);
impl<'v> ObjMemberBuilder<ExtendBuilder<'v>> {
pub fn value(self, value: Val) {
- self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)))
+ self.binding(LazyBinding::Bound(LazyVal::new_resolved(value)));
}
pub fn bindable(self, bindable: TraceBox<dyn Bindable>) {
- self.binding(LazyBinding::Bindable(Cc::new(bindable)))
+ self.binding(LazyBinding::Bindable(Cc::new(bindable)));
}
pub fn binding(self, binding: LazyBinding) {
let (receiver, name, member) = self.build_member(binding);
let new = receiver.0.clone();
- *receiver.0 = new.extend_with_raw_member(name, member)
+ *receiver.0 = new.extend_with_raw_member(name, member);
}
}
crates/jrsonnet-evaluator/src/trace/location.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/location.rs
+++ b/crates/jrsonnet-evaluator/src/trace/location.rs
@@ -1,3 +1,4 @@
+#[allow(clippy::module_name_repetitions)]
#[derive(Clone, PartialEq, Debug)]
pub struct CodeLocation {
pub offset: usize,
@@ -9,6 +10,7 @@
pub line_end_offset: usize,
}
+#[allow(clippy::module_name_repetitions)]
pub fn location_to_offset(mut file: &str, mut line: usize, column: usize) -> Option<usize> {
let mut offset = 0;
while line > 1 {
@@ -21,13 +23,14 @@
Some(offset)
}
+#[allow(clippy::module_name_repetitions)]
pub fn offset_to_location(file: &str, offsets: &[usize]) -> Vec<CodeLocation> {
if offsets.is_empty() {
return vec![];
}
let mut line = 1;
let mut column = 1;
- let max_offset = *offsets.iter().max().unwrap();
+ let max_offset = *offsets.iter().max().expect("offsets is not empty");
let mut offset_map = offsets
.iter()
crates/jrsonnet-evaluator/src/trace/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/trace/mod.rs
+++ b/crates/jrsonnet-evaluator/src/trace/mod.rs
@@ -19,14 +19,18 @@
impl PathResolver {
pub fn resolve(&self, from: &Path) -> String {
match self {
- Self::FileName => from.file_name().unwrap().to_string_lossy().into_owned(),
+ Self::FileName => from
+ .file_name()
+ .expect("file name exists")
+ .to_string_lossy()
+ .into_owned(),
Self::Absolute => from.to_string_lossy().into_owned(),
Self::Relative(base) => {
if from.is_relative() {
return from.to_string_lossy().into_owned();
}
pathdiff::diff_paths(from, base)
- .unwrap()
+ .expect("base is absolute")
.to_string_lossy()
.into_owned()
}
@@ -35,6 +39,7 @@
}
/// Implements pretty-printing of traces
+#[allow(clippy::module_name_repetitions)]
pub trait TraceFormat {
fn write_trace(
&self,
@@ -88,8 +93,9 @@
error,
} = error.error()
{
+ use std::fmt::Write;
+
writeln!(out)?;
- use std::fmt::Write;
let mut n = self.resolver.resolve(path);
let mut offset = error.location.offset;
let is_eof = if offset >= source_code.len() {
@@ -134,7 +140,7 @@
let align = file_names
.iter()
.flatten()
- .map(|e| e.len())
+ .map(String::len)
.max()
.unwrap_or(0);
for (el, file) in error.trace().0.iter().zip(file_names) {
@@ -166,7 +172,7 @@
error: &LocError,
) -> Result<(), std::fmt::Error> {
write!(out, "{}", error.error())?;
- for item in error.trace().0.iter() {
+ for item in &error.trace().0 {
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
@@ -227,7 +233,7 @@
)?;
}
let trace = &error.trace();
- for item in trace.0.iter() {
+ for item in &trace.0 {
writeln!(out)?;
let desc = &item.desc;
if let Some(source) = &item.location {
@@ -273,7 +279,7 @@
let snippet = Snippet {
opt: FormatOptions {
color: true,
- ..Default::default()
+ ..FormatOptions::default()
},
title: None,
footer: vec![],
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -38,6 +38,7 @@
<Self as Typed>::TYPE.check(s, &value)?;
match value {
Val::Num(n) => {
+ #[allow(clippy::float_cmp)]
if n.trunc() != n {
throw!(RuntimeError(
format!(
@@ -95,6 +96,7 @@
<Self as Typed>::TYPE.check(s, &value)?;
match value {
Val::Num(n) => {
+ #[allow(clippy::float_cmp)]
if n.trunc() != n {
throw!(RuntimeError(
format!(
@@ -160,7 +162,7 @@
impl Typed for usize {
// It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility
const TYPE: &'static ComplexValType =
- &ComplexValType::BoundedNumber(Some(0.0), Some(4294967295.0));
+ &ComplexValType::BoundedNumber(Some(0.0), Some(u32::MAX as f64));
fn into_untyped(value: Self, _: State) -> Result<Val> {
if value > u32::MAX as Self {
@@ -173,6 +175,7 @@
<Self as Typed>::TYPE.check(s, &value)?;
match value {
Val::Num(n) => {
+ #[allow(clippy::float_cmp)]
if n.trunc() != n {
throw!(RuntimeError(
"cannot convert number with fractional part to usize".into()
@@ -263,7 +266,7 @@
}
/// To be used in Vec<Any>
-/// Regular Val can't be used here, because it has wrong TryFrom::Error type
+/// Regular Val can't be used here, because it has wrong `TryFrom::Error` type
#[derive(Clone)]
pub struct Any(pub Val);
@@ -279,7 +282,7 @@
}
}
-/// Specialization, provides faster TryFrom<VecVal> for Val
+/// Specialization, provides faster `TryFrom<VecVal>` for Val
pub struct VecVal(pub Cc<Vec<Val>>);
impl Typed for VecVal {
@@ -310,22 +313,20 @@
}
fn from_untyped(value: Val, s: State) -> Result<Self> {
+ if let Val::Arr(ArrValue::Bytes(bytes)) = value {
+ return Ok(Self(bytes));
+ }
+ <Self as Typed>::TYPE.check(s.clone(), &value)?;
match value {
- Val::Arr(ArrValue::Bytes(bytes)) => Ok(Self(bytes)),
- _ => {
- <Self as Typed>::TYPE.check(s.clone(), &value)?;
- match value {
- Val::Arr(a) => {
- let mut out = Vec::with_capacity(a.len());
- for e in a.iter(s.clone()) {
- let r = e?;
- out.push(u8::from_untyped(r, s.clone())?);
- }
- Ok(Self(out.into()))
- }
- _ => unreachable!(),
+ Val::Arr(a) => {
+ let mut out = Vec::with_capacity(a.len());
+ for e in a.iter(s.clone()) {
+ let r = e?;
+ out.push(u8::from_untyped(r, s.clone())?);
}
+ Ok(Self(out.into()))
}
+ _ => unreachable!(),
}
}
}
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -71,11 +71,11 @@
if line.trim().is_empty() {
continue;
}
- if i != 0 {
+ if i == 0 {
+ write!(f, " - ")?;
+ } else {
writeln!(f)?;
write!(f, " ")?;
- } else {
- write!(f, " - ")?;
}
write!(f, "{}", line)?;
}
@@ -94,7 +94,7 @@
Ok(_) => Ok(()),
Err(mut e) => {
if let Error::TypeError(e) = &mut e.error_mut() {
- (e.1).0.push(path())
+ (e.1).0.push(path());
}
Err(e)
}
@@ -145,6 +145,7 @@
}
impl CheckType for ComplexValType {
+ #[allow(clippy::too_many_lines)]
fn check(&self, s: State, value: &Val) -> Result<()> {
match self {
Self::Any => Ok(()),
@@ -202,7 +203,7 @@
|| format!("property {}", k),
|| ValuePathItem::Field((*k).into()),
|| v.check(s.clone(), &got_v),
- )?
+ )?;
} else {
return Err(
TypeError::MissingProperty((*k).into(), self.clone()).into()
@@ -245,13 +246,13 @@
}
Self::Sum(types) => {
for ty in types.iter() {
- ty.check(s.clone(), value)?
+ ty.check(s.clone(), value)?;
}
Ok(())
}
Self::SumRef(types) => {
for ty in types.iter() {
- ty.check(s.clone(), value)?
+ ty.check(s.clone(), value)?;
}
Ok(())
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -32,6 +32,7 @@
Pending,
}
+#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Trace)]
pub struct LazyVal(Cc<RefCell<LazyValInternals>>);
impl LazyVal {
@@ -50,7 +51,7 @@
LazyValInternals::Computed(v) => return Ok(v.clone()),
LazyValInternals::Errored(e) => return Err(e.clone()),
LazyValInternals::Pending => return Err(InfiniteRecursionDetected.into()),
- _ => (),
+ LazyValInternals::Waiting(..) => (),
};
let value = if let LazyValInternals::Waiting(value) =
std::mem::replace(&mut *self.0.borrow_mut(), LazyValInternals::Pending)
@@ -114,6 +115,7 @@
}
}
+#[allow(clippy::module_name_repetitions)]
#[derive(Trace, Clone)]
pub enum FuncVal {
/// Plain function implemented in jsonnet
@@ -225,10 +227,10 @@
let rem = diff % self.step();
let div = diff / self.step();
- if rem != 0 {
+ if rem == 0 {
+ div
+ } else {
div + 1
- } else {
- div
}
}
}
@@ -248,11 +250,17 @@
pub fn new_eager() -> Self {
Self::Eager(Cc::new(Vec::new()))
}
+
+ /// # Panics
+ /// If a > b
pub fn new_range(a: i32, b: i32) -> Self {
assert!(a <= b);
Self::Range(a, b)
}
+ /// # Panics
+ /// If passed numbers are incorrect
+ #[must_use]
pub fn slice(self, from: Option<usize>, to: Option<usize>, step: Option<usize>) -> Self {
let len = self.len();
let from = from.unwrap_or(0);
@@ -289,7 +297,7 @@
match self {
Self::Bytes(i) => i
.get(index)
- .map_or(Ok(None), |v| Ok(Some(Val::Num(*v as f64)))),
+ .map_or(Ok(None), |v| Ok(Some(Val::Num(f64::from(*v))))),
Self::Lazy(vec) => {
if let Some(v) = vec.get(index) {
Ok(Some(v.evaluate(s)?))
@@ -333,7 +341,7 @@
match self {
Self::Bytes(i) => i
.get(index)
- .map(|b| LazyVal::new_resolved(Val::Num(*b as f64))),
+ .map(|b| LazyVal::new_resolved(Val::Num(f64::from(*b)))),
Self::Lazy(vec) => vec.get(index).cloned(),
Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
Self::Extended(v) => {
@@ -374,7 +382,7 @@
Self::Bytes(i) => {
let mut out = Vec::with_capacity(i.len());
for v in i.iter() {
- out.push(Val::Num(*v as f64));
+ out.push(Val::Num(f64::from(*v)));
}
Cc::new(out)
}
@@ -396,7 +404,7 @@
Self::Range(a, b) => {
let mut out = Vec::with_capacity(self.len());
for i in *a..*b {
- out.push(Val::Num(i as f64));
+ out.push(Val::Num(f64::from(i)));
}
Cc::new(out)
}
@@ -414,7 +422,7 @@
.take(v.to() - v.from())
.step_by(v.step())
{
- out.push(v.evaluate(s.clone())?)
+ out.push(v.evaluate(s.clone())?);
}
Cc::new(out)
}
@@ -423,28 +431,27 @@
pub fn iter(&self, s: State) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
- Self::Bytes(b) => Ok(Val::Num(b[idx] as f64)),
+ Self::Bytes(b) => Ok(Val::Num(f64::from(b[idx]))),
Self::Lazy(l) => l[idx].evaluate(s.clone()),
Self::Eager(e) => Ok(e[idx].clone()),
- Self::Extended(_) => self.get(s.clone(), idx).map(|e| e.unwrap()),
- Self::Range(..) => self.get(s.clone(), idx).map(|e| e.unwrap()),
- Self::Reversed(..) => self.get(s.clone(), idx).map(|e| e.unwrap()),
- Self::Slice(..) => self.get(s.clone(), idx).map(|e| e.unwrap()),
+ Self::Extended(..) | Self::Range(..) | Self::Reversed(..) | Self::Slice(..) => {
+ self.get(s.clone(), idx).map(|e| e.expect("idx < len"))
+ }
})
}
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
(0..self.len()).map(move |idx| match self {
- Self::Bytes(b) => LazyVal::new_resolved(Val::Num(b[idx] as f64)),
+ Self::Bytes(b) => LazyVal::new_resolved(Val::Num(f64::from(b[idx]))),
Self::Lazy(l) => l[idx].clone(),
Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
- Self::Extended(_) => self.get_lazy(idx).unwrap(),
- Self::Range(..) => self.get_lazy(idx).unwrap(),
- Self::Reversed(..) => self.get_lazy(idx).unwrap(),
- Self::Slice(..) => self.get_lazy(idx).unwrap(),
+ Self::Slice(..) | Self::Extended(..) | Self::Range(..) | Self::Reversed(..) => {
+ self.get_lazy(idx).expect("idx < len")
+ }
})
}
+ #[must_use]
pub fn reversed(self) -> Self {
Self::Reversed(Box::new(self))
}
@@ -493,6 +500,7 @@
}
}
+#[allow(clippy::module_name_repetitions)]
pub enum IndexableVal {
Str(IStr),
Arr(ArrValue),
@@ -708,7 +716,7 @@
preserve_order,
},
)
- .map(|s| s.into())
+ .map(Into::into)
}
/// Calls `std.manifestJson`
@@ -730,7 +738,7 @@
preserve_order,
},
)
- .map(|s| s.into())
+ .map(Into::into)
}
pub fn to_yaml(
@@ -751,7 +759,7 @@
preserve_order,
},
)
- .map(|s| s.into())
+ .map(Into::into)
}
pub fn into_indexable(self) -> Result<IndexableVal> {
Ok(match self {
@@ -824,8 +832,8 @@
for field in fields {
if !equals(
s.clone(),
- &a.get(s.clone(), field.clone())?.unwrap(),
- &b.get(s.clone(), field)?.unwrap(),
+ &a.get(s.clone(), field.clone())?.expect("field exists"),
+ &b.get(s.clone(), field)?.expect("field exists"),
)? {
return Ok(false);
}
crates/jrsonnet-evaluator/tests/common.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/tests/common.rs
+++ b/crates/jrsonnet-evaluator/tests/common.rs
@@ -12,6 +12,15 @@
}
#[macro_export]
+macro_rules! ensure {
+ ($v:expr $(,)?) => {
+ if !$v {
+ ::jrsonnet_evaluator::throw_runtime!("assertion failed: {}", stringify!($v))
+ }
+ };
+}
+
+#[macro_export]
macro_rules! ensure_val_eq {
($s:expr, $a:expr, $b:expr) => {{
if !::jrsonnet_evaluator::val::equals($s.clone(), &$a.clone(), &$b.clone())? {
crates/jrsonnet-evaluator/tests/sanity.rsdiffbeforeafterboth--- /dev/null
+++ b/crates/jrsonnet-evaluator/tests/sanity.rs
@@ -0,0 +1,46 @@
+use std::path::PathBuf;
+
+use jrsonnet_evaluator::{error::Result, throw_runtime, State, Val};
+
+mod common;
+
+#[test]
+fn assert_positive() -> Result<()> {
+ let s = State::default();
+ s.with_stdlib();
+
+ let v = s.evaluate_snippet_raw(PathBuf::new().into(), "assert 1 == 1: 'fail'; null".into())?;
+ ensure_val_eq!(s.clone(), v, Val::Null);
+ let v = s.evaluate_snippet_raw(PathBuf::new().into(), "std.assertEqual(1, 1)".into())?;
+ ensure_val_eq!(s.clone(), v, Val::Bool(true));
+
+ Ok(())
+}
+
+#[test]
+fn assert_negative() -> Result<()> {
+ let s = State::default();
+ s.with_stdlib();
+
+ {
+ let e = match s
+ .evaluate_snippet_raw(PathBuf::new().into(), "assert 1 == 2: 'fail'; null".into())
+ {
+ Ok(_) => throw_runtime!("assertion should fail"),
+ Err(e) => e,
+ };
+ let e = s.stringify_err(&e);
+ ensure!(e.starts_with("assert failed: fail\n"));
+ }
+ {
+ let e = match s.evaluate_snippet_raw(PathBuf::new().into(), "std.assertEqual(1, 2)".into())
+ {
+ Ok(_) => throw_runtime!("assertion should fail"),
+ Err(e) => e,
+ };
+ let e = s.stringify_err(&e);
+ ensure!(e.starts_with("runtime error: Assertion failed. 1 != 2"))
+ }
+
+ Ok(())
+}
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -157,8 +157,8 @@
});
}
- match &ty as &Type {
- Type::Reference(r) if type_is_path(&r.elem, &name).is_some() => return Ok(Self::This),
+ match ty as &Type {
+ Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),
_ => {}
}
@@ -465,14 +465,13 @@
"strategy should be set when flattening Option",
));
}
- } else {
- if attr.flatten_ok {
- return Err(Error::new(
- field.span(),
- "flatten(ok) is only useable on optional fields",
- ));
- }
+ } else if attr.flatten_ok {
+ return Err(Error::new(
+ field.span(),
+ "flatten(ok) is only useable on optional fields",
+ ));
}
+
Ok(Self {
attr,
ident,