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.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,7 +1,22 @@
-#![warn(clippy::all, clippy::nursery)]
+#![warn(clippy::all, clippy::nursery, clippy::pedantic)]
#![allow(
macro_expanded_macro_exports_accessed_by_absolute_paths,
- clippy::ptr_arg
+ clippy::ptr_arg,
+ // Too verbose
+ clippy::must_use_candidate,
+ // A lot of functions pass around errors thrown by code
+ clippy::missing_errors_doc,
+ // A lot of pointers have interior Rc
+ clippy::needless_pass_by_value,
+ // Its fine
+ clippy::wildcard_imports,
+ clippy::enum_glob_use,
+ clippy::module_name_repetitions,
+ // TODO: fix individual issues, however this works as intended almost everywhere
+ clippy::cast_precision_loss,
+ clippy::cast_possible_wrap,
+ clippy::cast_possible_truncation,
+ clippy::cast_sign_loss,
)]
// For jrsonnet-macros
@@ -105,10 +120,10 @@
Self {
max_stack: 200,
max_trace: 20,
- globals: Default::default(),
- ext_vars: Default::default(),
- ext_natives: Default::default(),
- tla_vars: Default::default(),
+ globals: HashMap::default(),
+ ext_vars: HashMap::default(),
+ ext_natives: HashMap::default(),
+ tla_vars: HashMap::default(),
import_resolver: Box::new(DummyImportResolver),
manifest_format: ManifestFormat::Json {
padding: 4,
@@ -161,7 +176,7 @@
if self.0.is_empty() {
return result;
}
- for item in self.0.iter() {
+ for item in &self.0 {
if item.loc.belongs_to(loc) {
let mut collected = item.collected.borrow_mut();
let (depth, vals) = collected.entry(stack_generation).or_default();
@@ -198,7 +213,7 @@
)
.map_err(|error| ImportSyntaxError {
error: Box::new(error),
- path: path.to_owned(),
+ path: path.clone(),
source_code: source_code.clone(),
})?;
self.add_parsed_file(path, source_code, parsed.clone())?;
@@ -210,7 +225,7 @@
self.data_mut()
.files
.get_mut(name)
- .unwrap()
+ .expect("file not found")
.evaluated
.take();
}
@@ -246,7 +261,11 @@
line: usize,
column: usize,
) -> Option<usize> {
- location_to_offset(&self.get_source(file).unwrap(), line, column)
+ location_to_offset(
+ &self.get_source(file).expect("file not found"),
+ line,
+ column,
+ )
}
pub fn import_file(&self, from: &Path, path: &Path) -> Result<Val> {
let file_path = self.resolve_file(from, path)?;
@@ -312,8 +331,10 @@
STDLIB_STR.to_owned().into(),
builtin::get_parsed_stdlib(),
)
- .unwrap();
- let val = self.evaluate_loaded_file_raw(&std_path).unwrap();
+ .expect("stdlib is correct");
+ let val = self
+ .evaluate_loaded_file_raw(&std_path)
+ .expect("stdlib is correct");
self.settings_mut().globals.insert("std".into(), val);
self
}
@@ -342,9 +363,8 @@
// Error creation uses data, so i drop guard here
drop(data);
throw!(StackOverflow);
- } else {
- *stack_depth += 1;
}
+ *stack_depth += 1;
}
let result = f();
{
@@ -376,9 +396,8 @@
// Error creation uses data, so i drop guard here
drop(data);
throw!(StackOverflow);
- } else {
- *stack_depth += 1;
}
+ *stack_depth += 1;
}
let mut result = f();
{
@@ -411,9 +430,8 @@
// Error creation uses data, so i drop guard here
drop(data);
throw!(StackOverflow);
- } else {
- *stack_depth += 1;
}
+ *stack_depth += 1;
}
let result = f();
{
@@ -431,6 +449,8 @@
result
}
+ /// # Panics
+ /// In case of formatting failure
pub fn stringify_err(&self, e: &LocError) -> String {
let mut out = String::new();
self.settings()
@@ -1232,49 +1252,5 @@
assert_eval!(r#"std.assertEqual(std.count([], ""), 0)"#);
assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "d"), 0)"#);
assert_eval!(r#"std.assertEqual(std.count(["a", "b", "a"], "a"), 2)"#);
- }
-
- mod derive_typed {
- use std::path::PathBuf;
-
- use crate::{typed::Typed, State};
-
- #[derive(PartialEq, Debug, Typed)]
- struct MyTyped {
- a: u32,
- #[typed(rename = "b")]
- c: String,
- }
-
- #[test]
- fn test() {
- let es = State::default();
- let val = eval!("{a: 14, b: 'Hello, world!'}");
- let typed = MyTyped::try_from(val).unwrap();
-
- assert_eq!(
- typed,
- MyTyped {
- a: 14,
- c: "Hello, world!".to_string()
- }
- );
- es.settings_mut()
- .globals
- .insert("mytyped".into(), typed.try_into().unwrap());
-
- let v = es
- .evaluate_snippet_raw(
- PathBuf::from("raw.jsonnet").into(),
- "
- mytyped == {a: 14, b: 'Hello, world!'}
- "
- .into(),
- )
- .unwrap()
- .as_bool()
- .unwrap();
- assert!(v)
- }
}
}
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.rsdiffbeforeafterboth1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4 parenthesized,5 parse::{Parse, ParseStream},6 parse_macro_input,7 punctuated::Punctuated,8 spanned::Spanned,9 token::{self, Comma},10 Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11 PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16 Ident: PartialEq<I>,17{18 let attrs = attrs19 .iter()20 .filter(|a| a.path.is_ident(&ident))21 .collect::<Vec<_>>();22 if attrs.len() > 1 {23 return Err(Error::new(24 attrs[1].span(),25 "this attribute may be specified only once",26 ));27 } else if attrs.is_empty() {28 return Ok(None);29 }30 let attr = attrs[0];31 let attr = attr.parse_args::<A>()?;3233 Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37 path.leading_colon.is_none()38 && !path.segments.is_empty()39 && path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43 match ty {44 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45 let args = &path.path.segments.iter().last().unwrap().arguments;46 Some(args)47 }48 _ => None,49 }50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53 Ok(if let Some(args) = type_is_path(ty, "Option") {54 // It should have only on angle-bracketed param ("<String>"):55 let generic_arg = match args {56 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),57 _ => return Err(Error::new(args.span(), "missing option generic")),58 };59 // This argument must be a type:60 match generic_arg {61 GenericArgument::Type(ty) => Some(ty),62 _ => {63 return Err(Error::new(64 generic_arg.span(),65 "option generic should be a type",66 ))67 }68 }69 } else {70 None71 })72}7374struct Field {75 name: Ident,76 _colon: Token![:],77 ty: Type,78}79impl Parse for Field {80 fn parse(input: ParseStream) -> syn::Result<Self> {81 Ok(Self {82 name: input.parse()?,83 _colon: input.parse()?,84 ty: input.parse()?,85 })86 }87}8889mod kw {90 syn::custom_keyword!(fields);91 syn::custom_keyword!(rename);92 syn::custom_keyword!(flatten);93 syn::custom_keyword!(ok);94}9596struct EmptyAttr;97impl Parse for EmptyAttr {98 fn parse(_input: ParseStream) -> Result<Self> {99 Ok(Self)100 }101}102103struct BuiltinAttrs {104 fields: Vec<Field>,105}106impl Parse for BuiltinAttrs {107 fn parse(input: ParseStream) -> syn::Result<Self> {108 if input.is_empty() {109 return Ok(Self { fields: Vec::new() });110 }111 input.parse::<kw::fields>()?;112 let fields;113 parenthesized!(fields in input);114 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;115 Ok(Self {116 fields: p.into_iter().collect(),117 })118 }119}120121enum ArgInfo {122 Normal {123 ty: Box<Type>,124 is_option: bool,125 name: String,126 cfg_attrs: Vec<Attribute>,127 // ident: Ident,128 },129 Lazy {130 is_option: bool,131 name: String,132 },133 State,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let arg = match arg {141 FnArg::Receiver(_) => unreachable!(),142 FnArg::Typed(a) => a,143 };144 let ident = match &arg.pat as &Pat {145 Pat::Ident(i) => i.ident.clone(),146 _ => return Err(Error::new(arg.pat.span(), "arg should be plain identifier")),147 };148 let ty = &arg.ty;149 if type_is_path(ty, "State").is_some() {150 return Ok(Self::State);151 } else if type_is_path(ty, "CallLocation").is_some() {152 return Ok(Self::Location);153 } else if type_is_path(ty, "LazyVal").is_some() {154 return Ok(Self::Lazy {155 is_option: false,156 name: ident.to_string(),157 });158 }159160 match &ty as &Type {161 Type::Reference(r) if type_is_path(&r.elem, &name).is_some() => return Ok(Self::This),162 _ => {}163 }164165 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {166 if type_is_path(ty, "LazyVal").is_some() {167 return Ok(Self::Lazy {168 is_option: true,169 name: ident.to_string(),170 });171 }172173 (true, Box::new(ty.clone()))174 } else {175 (false, ty.clone())176 };177178 let cfg_attrs = arg179 .attrs180 .iter()181 .filter(|a| a.path.is_ident("cfg"))182 .cloned()183 .collect();184185 Ok(Self::Normal {186 ty,187 is_option,188 name: ident.to_string(),189 cfg_attrs,190 })191 }192}193194#[proc_macro_attribute]195pub fn builtin(196 attr: proc_macro::TokenStream,197 item: proc_macro::TokenStream,198) -> proc_macro::TokenStream {199 let attr = parse_macro_input!(attr as BuiltinAttrs);200 let item: ItemFn = parse_macro_input!(item);201202 match builtin_inner(attr, item) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {209 let result = match fun.sig.output {210 ReturnType::Default => {211 return Err(Error::new(212 fun.sig.span(),213 "builtin should return something",214 ))215 }216 ReturnType::Type(_, ref ty) => ty.clone(),217 };218 let result_inner = if let Some(args) = type_is_path(&result, "Result") {219 let generic_arg = match args {220 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),221 _ => return Err(Error::new(args.span(), "missing result generic")),222 };223 // This argument must be a type:224 match generic_arg {225 GenericArgument::Type(ty) => ty,226 _ => {227 return Err(Error::new(228 generic_arg.span(),229 "option generic should be a type",230 ))231 }232 }233 } else {234 return Err(Error::new(result.span(), "return value should be result"));235 };236237 let name = fun.sig.ident.to_string();238 let args = fun239 .sig240 .inputs241 .iter()242 .map(|arg| ArgInfo::parse(&name, arg))243 .collect::<Result<Vec<_>>>()?;244245 let params_desc = args.iter().flat_map(|a| match a {246 ArgInfo::Normal {247 is_option,248 name,249 cfg_attrs,250 ..251 } => Some(quote! {252 #(#cfg_attrs)*253 BuiltinParam {254 name: std::borrow::Cow::Borrowed(#name),255 has_default: #is_option,256 },257 }),258 ArgInfo::Lazy { is_option, name } => Some(quote! {259 BuiltinParam {260 name: std::borrow::Cow::Borrowed(#name),261 has_default: #is_option,262 },263 }),264 ArgInfo::State => None,265 ArgInfo::Location => None,266 ArgInfo::This => None,267 });268269 let pass = args.iter().map(|a| match a {270 ArgInfo::Normal {271 ty,272 is_option,273 name,274 cfg_attrs,275 } => {276 let eval = quote! {s.push_description(277 || format!("argument <{}> evaluation", #name),278 || <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),279 )?};280 let value = if *is_option {281 quote! {if let Some(value) = parsed.get(#name) {282 Some(#eval)283 } else {284 None285 },}286 } else {287 quote! {{288 let value = parsed.get(#name).expect("args shape is checked");289 #eval290 },}291 };292 quote! {293 #(#cfg_attrs)*294 #value295 }296 }297 ArgInfo::Lazy { is_option, name } => {298 if *is_option {299 quote! {if let Some(value) = parsed.get(#name) {300 Some(value.clone())301 } else {302 None303 }}304 } else {305 quote! {306 parsed.get(#name).expect("args shape is correct").clone(),307 }308 }309 }310 ArgInfo::State => quote! {s.clone(),},311 ArgInfo::Location => quote! {location,},312 ArgInfo::This => quote! {self,},313 });314315 let fields = attr.fields.iter().map(|field| {316 let name = &field.name;317 let ty = &field.ty;318 quote! {319 pub #name: #ty,320 }321 });322323 let name = &fun.sig.ident;324 let vis = &fun.vis;325 let static_ext = if attr.fields.is_empty() {326 quote! {327 impl #name {328 pub const INST: &'static dyn StaticBuiltin = &#name {};329 }330 impl StaticBuiltin for #name {}331 }332 } else {333 quote! {}334 };335 let static_derive_copy = if attr.fields.is_empty() {336 quote! {, Copy}337 } else {338 quote! {}339 };340341 Ok(quote! {342 #fun343 #[doc(hidden)]344 #[allow(non_camel_case_types)]345 #[derive(Clone, gcmodule::Trace #static_derive_copy)]346 #vis struct #name {347 #(#fields)*348 }349 const _: () = {350 use ::jrsonnet_evaluator::{351 State, Val,352 function::{Builtin, CallLocation, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},353 error::Result, Context, typed::Typed,354 parser::ExprLocation,355 };356 const PARAMS: &'static [BuiltinParam] = &[357 #(#params_desc)*358 ];359360 #static_ext361 impl Builtin for #name362 where363 Self: 'static364 {365 fn name(&self) -> &str {366 stringify!(#name)367 }368 fn params(&self) -> &[BuiltinParam] {369 PARAMS370 }371 fn call(&self, s: State, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {372 let parsed = parse_builtin_call(s.clone(), ctx, &PARAMS, args, false)?;373374 let result: #result = #name(#(#pass)*);375 let result = result?;376 <#result_inner>::into_untyped(result, s)377 }378 }379 };380 })381}382383#[derive(Default)]384struct TypedAttr {385 rename: Option<String>,386 flatten: bool,387 /// flatten(ok) strategy for flattened optionals388 /// field would be None in case of any parsing error (as in serde)389 flatten_ok: bool,390}391impl Parse for TypedAttr {392 fn parse(input: ParseStream) -> syn::Result<Self> {393 let mut out = Self::default();394 loop {395 let lookahead = input.lookahead1();396 if lookahead.peek(kw::rename) {397 input.parse::<kw::rename>()?;398 input.parse::<Token![=]>()?;399 let name = input.parse::<LitStr>()?;400 if out.rename.is_some() {401 return Err(Error::new(402 name.span(),403 "rename attribute may only be specified once",404 ));405 }406 out.rename = Some(name.value());407 } else if lookahead.peek(kw::flatten) {408 input.parse::<kw::flatten>()?;409 out.flatten = true;410 if input.peek(token::Paren) {411 let content;412 parenthesized!(content in input);413 let lookahead = content.lookahead1();414 if lookahead.peek(kw::ok) {415 content.parse::<kw::ok>()?;416 out.flatten_ok = true;417 } else {418 return Err(lookahead.error());419 }420 }421 } else if input.is_empty() {422 break;423 } else {424 return Err(lookahead.error());425 }426 if input.peek(Token![,]) {427 input.parse::<Token![,]>()?;428 } else {429 break;430 }431 }432 // input.parse::<kw::rename>()?;433 // input.parse::<Token![=]>()?;434 // let rename = input.parse::<LitStr>()?.value();435 Ok(out)436 }437}438439struct TypedField {440 attr: TypedAttr,441 ident: Ident,442 ty: Type,443 is_option: bool,444}445impl TypedField {446 fn parse(field: &syn::Field) -> Result<Self> {447 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();448 let ident = if let Some(ident) = field.ident.clone() {449 ident450 } else {451 return Err(Error::new(452 field.span(),453 "this field should appear in output object, but it has no visible name",454 ));455 };456 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {457 (true, ty.clone())458 } else {459 (false, field.ty.clone())460 };461 if is_option && attr.flatten {462 if !attr.flatten_ok {463 return Err(Error::new(464 field.span(),465 "strategy should be set when flattening Option",466 ));467 }468 } else {469 if attr.flatten_ok {470 return Err(Error::new(471 field.span(),472 "flatten(ok) is only useable on optional fields",473 ));474 }475 }476 Ok(Self {477 attr,478 ident,479 ty,480 is_option,481 })482 }483 /// None if this field is flattened in jsonnet output484 fn name(&self) -> Option<String> {485 if self.attr.flatten {486 return None;487 }488 Some(489 self.attr490 .rename491 .clone()492 .unwrap_or_else(|| self.ident.to_string()),493 )494 }495496 fn expand_field(&self) -> Option<TokenStream> {497 if self.is_option {498 return None;499 }500 let name = self.name()?;501 let ty = &self.ty;502 Some(quote! {503 (#name, <#ty>::TYPE)504 })505 }506 fn expand_parse(&self) -> TokenStream {507 let ident = &self.ident;508 let ty = &self.ty;509 if self.attr.flatten {510 // optional flatten is handled in same way as serde511 return if self.is_option {512 quote! {513 #ident: <#ty>::parse(&obj, s.clone()).ok(),514 }515 } else {516 quote! {517 #ident: <#ty>::parse(&obj, s.clone())?,518 }519 };520 };521522 let name = self.name().unwrap();523 let value = if self.is_option {524 quote! {525 if let Some(value) = obj.get(s.clone(), #name.into())? {526 Some(<#ty>::from_untyped(value, s.clone())?)527 } else {528 None529 }530 }531 } else {532 quote! {533 <#ty>::from_untyped(obj.get(s.clone(), #name.into())?.ok_or_else(|| Error::NoSuchField(#name.into()))?, s.clone())?534 }535 };536537 quote! {538 #ident: #value,539 }540 }541 fn expand_serialize(&self) -> Result<TokenStream> {542 let ident = &self.ident;543 let ty = &self.ty;544 Ok(if let Some(name) = self.name() {545 if self.is_option {546 quote! {547 if let Some(value) = self.#ident {548 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(value, s.clone())?)?;549 }550 }551 } else {552 quote! {553 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(self.#ident, s.clone())?)?;554 }555 }556 } else if self.is_option {557 quote! {558 if let Some(value) = self.#ident {559 value.serialize(s.clone(), out)?;560 }561 }562 } else {563 quote! {564 self.#ident.serialize(s.clone(), out)?;565 }566 })567 }568}569570#[proc_macro_derive(Typed, attributes(typed))]571pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {572 let input = parse_macro_input!(item as DeriveInput);573574 match derive_typed_inner(input) {575 Ok(v) => v.into(),576 Err(e) => e.to_compile_error().into(),577 }578}579580fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {581 let data = match &input.data {582 syn::Data::Struct(s) => s,583 _ => return Err(Error::new(input.span(), "only structs supported")),584 };585586 let ident = &input.ident;587 let fields = data588 .fields589 .iter()590 .map(TypedField::parse)591 .collect::<Result<Vec<_>>>()?;592593 let typed = {594 let fields = fields595 .iter()596 .flat_map(TypedField::expand_field)597 .collect::<Vec<_>>();598 let len = fields.len();599 quote! {600 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [601 #(#fields,)*602 ];603 impl Typed for #ident {604 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);605606 fn from_untyped(value: Val, s: State) -> Result<Self> {607 let obj = value.as_obj().expect("shape is correct");608 Self::parse(&obj, s)609 }610611 fn into_untyped(value: Self, s: State) -> Result<Val> {612 let mut out = ObjValueBuilder::new();613 value.serialize(s, &mut out)?;614 Ok(Val::Obj(out.build()))615 }616617 }618 }619 };620621 let fields_parse = fields.iter().map(TypedField::expand_parse);622 let fields_serialize = fields623 .iter()624 .map(TypedField::expand_serialize)625 .collect::<Result<Vec<_>>>()?;626627 Ok(quote! {628 const _: () = {629 use ::jrsonnet_evaluator::{630 typed::{ComplexValType, Typed, TypedObj, CheckType},631 Val, State,632 error::{LocError, Error, Result},633 ObjValueBuilder, ObjValue,634 };635636 #typed637638 impl TypedObj for #ident {639 fn serialize(self, s: State, out: &mut ObjValueBuilder) -> Result<(), LocError> {640 #(#fields_serialize)*641642 Ok(())643 }644 fn parse(obj: &ObjValue, s: State) -> Result<Self, LocError> {645 Ok(Self {646 #(#fields_parse)*647 })648 }649 }650 };651 })652}1use proc_macro2::TokenStream;2use quote::quote;3use syn::{4 parenthesized,5 parse::{Parse, ParseStream},6 parse_macro_input,7 punctuated::Punctuated,8 spanned::Spanned,9 token::{self, Comma},10 Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, Path,11 PathArguments, Result, ReturnType, Token, Type,12};1314fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>15where16 Ident: PartialEq<I>,17{18 let attrs = attrs19 .iter()20 .filter(|a| a.path.is_ident(&ident))21 .collect::<Vec<_>>();22 if attrs.len() > 1 {23 return Err(Error::new(24 attrs[1].span(),25 "this attribute may be specified only once",26 ));27 } else if attrs.is_empty() {28 return Ok(None);29 }30 let attr = attrs[0];31 let attr = attr.parse_args::<A>()?;3233 Ok(Some(attr))34}3536fn path_is(path: &Path, needed: &str) -> bool {37 path.leading_colon.is_none()38 && !path.segments.is_empty()39 && path.segments.iter().last().unwrap().ident == needed40}4142fn type_is_path<'ty>(ty: &'ty Type, needed: &str) -> Option<&'ty PathArguments> {43 match ty {44 Type::Path(path) if path.qself.is_none() && path_is(&path.path, needed) => {45 let args = &path.path.segments.iter().last().unwrap().arguments;46 Some(args)47 }48 _ => None,49 }50}5152fn extract_type_from_option(ty: &Type) -> Result<Option<&Type>> {53 Ok(if let Some(args) = type_is_path(ty, "Option") {54 // It should have only on angle-bracketed param ("<String>"):55 let generic_arg = match args {56 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),57 _ => return Err(Error::new(args.span(), "missing option generic")),58 };59 // This argument must be a type:60 match generic_arg {61 GenericArgument::Type(ty) => Some(ty),62 _ => {63 return Err(Error::new(64 generic_arg.span(),65 "option generic should be a type",66 ))67 }68 }69 } else {70 None71 })72}7374struct Field {75 name: Ident,76 _colon: Token![:],77 ty: Type,78}79impl Parse for Field {80 fn parse(input: ParseStream) -> syn::Result<Self> {81 Ok(Self {82 name: input.parse()?,83 _colon: input.parse()?,84 ty: input.parse()?,85 })86 }87}8889mod kw {90 syn::custom_keyword!(fields);91 syn::custom_keyword!(rename);92 syn::custom_keyword!(flatten);93 syn::custom_keyword!(ok);94}9596struct EmptyAttr;97impl Parse for EmptyAttr {98 fn parse(_input: ParseStream) -> Result<Self> {99 Ok(Self)100 }101}102103struct BuiltinAttrs {104 fields: Vec<Field>,105}106impl Parse for BuiltinAttrs {107 fn parse(input: ParseStream) -> syn::Result<Self> {108 if input.is_empty() {109 return Ok(Self { fields: Vec::new() });110 }111 input.parse::<kw::fields>()?;112 let fields;113 parenthesized!(fields in input);114 let p = Punctuated::<Field, Comma>::parse_terminated(&fields)?;115 Ok(Self {116 fields: p.into_iter().collect(),117 })118 }119}120121enum ArgInfo {122 Normal {123 ty: Box<Type>,124 is_option: bool,125 name: String,126 cfg_attrs: Vec<Attribute>,127 // ident: Ident,128 },129 Lazy {130 is_option: bool,131 name: String,132 },133 State,134 Location,135 This,136}137138impl ArgInfo {139 fn parse(name: &str, arg: &FnArg) -> Result<Self> {140 let arg = match arg {141 FnArg::Receiver(_) => unreachable!(),142 FnArg::Typed(a) => a,143 };144 let ident = match &arg.pat as &Pat {145 Pat::Ident(i) => i.ident.clone(),146 _ => return Err(Error::new(arg.pat.span(), "arg should be plain identifier")),147 };148 let ty = &arg.ty;149 if type_is_path(ty, "State").is_some() {150 return Ok(Self::State);151 } else if type_is_path(ty, "CallLocation").is_some() {152 return Ok(Self::Location);153 } else if type_is_path(ty, "LazyVal").is_some() {154 return Ok(Self::Lazy {155 is_option: false,156 name: ident.to_string(),157 });158 }159160 match ty as &Type {161 Type::Reference(r) if type_is_path(&r.elem, name).is_some() => return Ok(Self::This),162 _ => {}163 }164165 let (is_option, ty) = if let Some(ty) = extract_type_from_option(ty)? {166 if type_is_path(ty, "LazyVal").is_some() {167 return Ok(Self::Lazy {168 is_option: true,169 name: ident.to_string(),170 });171 }172173 (true, Box::new(ty.clone()))174 } else {175 (false, ty.clone())176 };177178 let cfg_attrs = arg179 .attrs180 .iter()181 .filter(|a| a.path.is_ident("cfg"))182 .cloned()183 .collect();184185 Ok(Self::Normal {186 ty,187 is_option,188 name: ident.to_string(),189 cfg_attrs,190 })191 }192}193194#[proc_macro_attribute]195pub fn builtin(196 attr: proc_macro::TokenStream,197 item: proc_macro::TokenStream,198) -> proc_macro::TokenStream {199 let attr = parse_macro_input!(attr as BuiltinAttrs);200 let item: ItemFn = parse_macro_input!(item);201202 match builtin_inner(attr, item) {203 Ok(v) => v.into(),204 Err(e) => e.into_compile_error().into(),205 }206}207208fn builtin_inner(attr: BuiltinAttrs, fun: ItemFn) -> syn::Result<TokenStream> {209 let result = match fun.sig.output {210 ReturnType::Default => {211 return Err(Error::new(212 fun.sig.span(),213 "builtin should return something",214 ))215 }216 ReturnType::Type(_, ref ty) => ty.clone(),217 };218 let result_inner = if let Some(args) = type_is_path(&result, "Result") {219 let generic_arg = match args {220 PathArguments::AngleBracketed(params) => params.args.iter().next().unwrap(),221 _ => return Err(Error::new(args.span(), "missing result generic")),222 };223 // This argument must be a type:224 match generic_arg {225 GenericArgument::Type(ty) => ty,226 _ => {227 return Err(Error::new(228 generic_arg.span(),229 "option generic should be a type",230 ))231 }232 }233 } else {234 return Err(Error::new(result.span(), "return value should be result"));235 };236237 let name = fun.sig.ident.to_string();238 let args = fun239 .sig240 .inputs241 .iter()242 .map(|arg| ArgInfo::parse(&name, arg))243 .collect::<Result<Vec<_>>>()?;244245 let params_desc = args.iter().flat_map(|a| match a {246 ArgInfo::Normal {247 is_option,248 name,249 cfg_attrs,250 ..251 } => Some(quote! {252 #(#cfg_attrs)*253 BuiltinParam {254 name: std::borrow::Cow::Borrowed(#name),255 has_default: #is_option,256 },257 }),258 ArgInfo::Lazy { is_option, name } => Some(quote! {259 BuiltinParam {260 name: std::borrow::Cow::Borrowed(#name),261 has_default: #is_option,262 },263 }),264 ArgInfo::State => None,265 ArgInfo::Location => None,266 ArgInfo::This => None,267 });268269 let pass = args.iter().map(|a| match a {270 ArgInfo::Normal {271 ty,272 is_option,273 name,274 cfg_attrs,275 } => {276 let eval = quote! {s.push_description(277 || format!("argument <{}> evaluation", #name),278 || <#ty>::from_untyped(value.evaluate(s.clone())?, s.clone()),279 )?};280 let value = if *is_option {281 quote! {if let Some(value) = parsed.get(#name) {282 Some(#eval)283 } else {284 None285 },}286 } else {287 quote! {{288 let value = parsed.get(#name).expect("args shape is checked");289 #eval290 },}291 };292 quote! {293 #(#cfg_attrs)*294 #value295 }296 }297 ArgInfo::Lazy { is_option, name } => {298 if *is_option {299 quote! {if let Some(value) = parsed.get(#name) {300 Some(value.clone())301 } else {302 None303 }}304 } else {305 quote! {306 parsed.get(#name).expect("args shape is correct").clone(),307 }308 }309 }310 ArgInfo::State => quote! {s.clone(),},311 ArgInfo::Location => quote! {location,},312 ArgInfo::This => quote! {self,},313 });314315 let fields = attr.fields.iter().map(|field| {316 let name = &field.name;317 let ty = &field.ty;318 quote! {319 pub #name: #ty,320 }321 });322323 let name = &fun.sig.ident;324 let vis = &fun.vis;325 let static_ext = if attr.fields.is_empty() {326 quote! {327 impl #name {328 pub const INST: &'static dyn StaticBuiltin = &#name {};329 }330 impl StaticBuiltin for #name {}331 }332 } else {333 quote! {}334 };335 let static_derive_copy = if attr.fields.is_empty() {336 quote! {, Copy}337 } else {338 quote! {}339 };340341 Ok(quote! {342 #fun343 #[doc(hidden)]344 #[allow(non_camel_case_types)]345 #[derive(Clone, gcmodule::Trace #static_derive_copy)]346 #vis struct #name {347 #(#fields)*348 }349 const _: () = {350 use ::jrsonnet_evaluator::{351 State, Val,352 function::{Builtin, CallLocation, StaticBuiltin, BuiltinParam, ArgsLike, parse_builtin_call},353 error::Result, Context, typed::Typed,354 parser::ExprLocation,355 };356 const PARAMS: &'static [BuiltinParam] = &[357 #(#params_desc)*358 ];359360 #static_ext361 impl Builtin for #name362 where363 Self: 'static364 {365 fn name(&self) -> &str {366 stringify!(#name)367 }368 fn params(&self) -> &[BuiltinParam] {369 PARAMS370 }371 fn call(&self, s: State, ctx: Context, location: CallLocation, args: &dyn ArgsLike) -> Result<Val> {372 let parsed = parse_builtin_call(s.clone(), ctx, &PARAMS, args, false)?;373374 let result: #result = #name(#(#pass)*);375 let result = result?;376 <#result_inner>::into_untyped(result, s)377 }378 }379 };380 })381}382383#[derive(Default)]384struct TypedAttr {385 rename: Option<String>,386 flatten: bool,387 /// flatten(ok) strategy for flattened optionals388 /// field would be None in case of any parsing error (as in serde)389 flatten_ok: bool,390}391impl Parse for TypedAttr {392 fn parse(input: ParseStream) -> syn::Result<Self> {393 let mut out = Self::default();394 loop {395 let lookahead = input.lookahead1();396 if lookahead.peek(kw::rename) {397 input.parse::<kw::rename>()?;398 input.parse::<Token![=]>()?;399 let name = input.parse::<LitStr>()?;400 if out.rename.is_some() {401 return Err(Error::new(402 name.span(),403 "rename attribute may only be specified once",404 ));405 }406 out.rename = Some(name.value());407 } else if lookahead.peek(kw::flatten) {408 input.parse::<kw::flatten>()?;409 out.flatten = true;410 if input.peek(token::Paren) {411 let content;412 parenthesized!(content in input);413 let lookahead = content.lookahead1();414 if lookahead.peek(kw::ok) {415 content.parse::<kw::ok>()?;416 out.flatten_ok = true;417 } else {418 return Err(lookahead.error());419 }420 }421 } else if input.is_empty() {422 break;423 } else {424 return Err(lookahead.error());425 }426 if input.peek(Token![,]) {427 input.parse::<Token![,]>()?;428 } else {429 break;430 }431 }432 // input.parse::<kw::rename>()?;433 // input.parse::<Token![=]>()?;434 // let rename = input.parse::<LitStr>()?.value();435 Ok(out)436 }437}438439struct TypedField {440 attr: TypedAttr,441 ident: Ident,442 ty: Type,443 is_option: bool,444}445impl TypedField {446 fn parse(field: &syn::Field) -> Result<Self> {447 let attr = parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_default();448 let ident = if let Some(ident) = field.ident.clone() {449 ident450 } else {451 return Err(Error::new(452 field.span(),453 "this field should appear in output object, but it has no visible name",454 ));455 };456 let (is_option, ty) = if let Some(ty) = extract_type_from_option(&field.ty)? {457 (true, ty.clone())458 } else {459 (false, field.ty.clone())460 };461 if is_option && attr.flatten {462 if !attr.flatten_ok {463 return Err(Error::new(464 field.span(),465 "strategy should be set when flattening Option",466 ));467 }468 } else if attr.flatten_ok {469 return Err(Error::new(470 field.span(),471 "flatten(ok) is only useable on optional fields",472 ));473 }474475 Ok(Self {476 attr,477 ident,478 ty,479 is_option,480 })481 }482 /// None if this field is flattened in jsonnet output483 fn name(&self) -> Option<String> {484 if self.attr.flatten {485 return None;486 }487 Some(488 self.attr489 .rename490 .clone()491 .unwrap_or_else(|| self.ident.to_string()),492 )493 }494495 fn expand_field(&self) -> Option<TokenStream> {496 if self.is_option {497 return None;498 }499 let name = self.name()?;500 let ty = &self.ty;501 Some(quote! {502 (#name, <#ty>::TYPE)503 })504 }505 fn expand_parse(&self) -> TokenStream {506 let ident = &self.ident;507 let ty = &self.ty;508 if self.attr.flatten {509 // optional flatten is handled in same way as serde510 return if self.is_option {511 quote! {512 #ident: <#ty>::parse(&obj, s.clone()).ok(),513 }514 } else {515 quote! {516 #ident: <#ty>::parse(&obj, s.clone())?,517 }518 };519 };520521 let name = self.name().unwrap();522 let value = if self.is_option {523 quote! {524 if let Some(value) = obj.get(s.clone(), #name.into())? {525 Some(<#ty>::from_untyped(value, s.clone())?)526 } else {527 None528 }529 }530 } else {531 quote! {532 <#ty>::from_untyped(obj.get(s.clone(), #name.into())?.ok_or_else(|| Error::NoSuchField(#name.into()))?, s.clone())?533 }534 };535536 quote! {537 #ident: #value,538 }539 }540 fn expand_serialize(&self) -> Result<TokenStream> {541 let ident = &self.ident;542 let ty = &self.ty;543 Ok(if let Some(name) = self.name() {544 if self.is_option {545 quote! {546 if let Some(value) = self.#ident {547 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(value, s.clone())?)?;548 }549 }550 } else {551 quote! {552 out.member(#name.into()).value(s.clone(), <#ty>::into_untyped(self.#ident, s.clone())?)?;553 }554 }555 } else if self.is_option {556 quote! {557 if let Some(value) = self.#ident {558 value.serialize(s.clone(), out)?;559 }560 }561 } else {562 quote! {563 self.#ident.serialize(s.clone(), out)?;564 }565 })566 }567}568569#[proc_macro_derive(Typed, attributes(typed))]570pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {571 let input = parse_macro_input!(item as DeriveInput);572573 match derive_typed_inner(input) {574 Ok(v) => v.into(),575 Err(e) => e.to_compile_error().into(),576 }577}578579fn derive_typed_inner(input: DeriveInput) -> Result<TokenStream> {580 let data = match &input.data {581 syn::Data::Struct(s) => s,582 _ => return Err(Error::new(input.span(), "only structs supported")),583 };584585 let ident = &input.ident;586 let fields = data587 .fields588 .iter()589 .map(TypedField::parse)590 .collect::<Result<Vec<_>>>()?;591592 let typed = {593 let fields = fields594 .iter()595 .flat_map(TypedField::expand_field)596 .collect::<Vec<_>>();597 let len = fields.len();598 quote! {599 const ITEMS: [(&'static str, &'static ComplexValType); #len] = [600 #(#fields,)*601 ];602 impl Typed for #ident {603 const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);604605 fn from_untyped(value: Val, s: State) -> Result<Self> {606 let obj = value.as_obj().expect("shape is correct");607 Self::parse(&obj, s)608 }609610 fn into_untyped(value: Self, s: State) -> Result<Val> {611 let mut out = ObjValueBuilder::new();612 value.serialize(s, &mut out)?;613 Ok(Val::Obj(out.build()))614 }615616 }617 }618 };619620 let fields_parse = fields.iter().map(TypedField::expand_parse);621 let fields_serialize = fields622 .iter()623 .map(TypedField::expand_serialize)624 .collect::<Result<Vec<_>>>()?;625626 Ok(quote! {627 const _: () = {628 use ::jrsonnet_evaluator::{629 typed::{ComplexValType, Typed, TypedObj, CheckType},630 Val, State,631 error::{LocError, Error, Result},632 ObjValueBuilder, ObjValue,633 };634635 #typed636637 impl TypedObj for #ident {638 fn serialize(self, s: State, out: &mut ObjValueBuilder) -> Result<(), LocError> {639 #(#fields_serialize)*640641 Ok(())642 }643 fn parse(obj: &ObjValue, s: State) -> Result<Self, LocError> {644 Ok(Self {645 #(#fields_parse)*646 })647 }648 }649 };650 })651}