difftreelog
style fix rustfmt and clippy warnings
in: master
4 files changed
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/evaluate.rs
+++ b/crates/jsonnet-evaluator/src/evaluate.rs
@@ -152,7 +152,7 @@
context.clone().extend(
new_bindings.clone().unwrap(),
context.clone().dollar().clone().or_else(||this.clone()),
- Some(this.clone().unwrap()),
+ Some(this.unwrap()),
super_obj
)
})
@@ -347,7 +347,7 @@
}
("std", "codepoint") => {
assert_eq!(args.len(), 1);
- if let Val::Str(s) = evaluate(context.clone(), &args[0].1) {
+ if let Val::Str(s) = evaluate(context, &args[0].1) {
assert!(
s.chars().count() == 1,
"std.codepoint should receive single char string"
crates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/obj.rs
+++ b/crates/jsonnet-evaluator/src/obj.rs
@@ -79,10 +79,7 @@
self.0.super_obj.clone(),
)),
(Some(k), Some(s)) => {
- let our = k.invoke.0(
- Some(real_this.clone()),
- self.0.super_obj.clone(),
- );
+ let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone());
if k.add {
s.get_raw(key, real_this).map_or(Some(our.clone()), |v| {
Some(evaluate_binary_op(&v, BinaryOpType::Add, &our))
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth1use std::fmt::Display;23#[derive(Debug, Clone, PartialEq)]4pub enum FieldName {5 /// {fixed: 2}6 Fixed(String),7 /// {["dyn"+"amic"]: 3}8 Dyn(Box<Expr>),9}1011#[derive(Debug, Clone, PartialEq)]12pub enum Visibility {13 /// :14 Normal,15 /// ::16 Hidden,17 /// :::18 Unhide,19}2021#[derive(Debug, Clone, PartialEq)]22pub struct AssertStmt(pub Box<Expr>, pub Option<Box<Expr>>);2324#[derive(Debug, Clone, PartialEq)]25pub struct FieldMember {26 pub name: FieldName,27 pub plus: bool,28 pub params: Option<ParamsDesc>,29 pub visibility: Visibility,30 pub value: Expr,31}3233#[derive(Debug, Clone, PartialEq)]34pub enum Member {35 Field(FieldMember),36 BindStmt(BindSpec),37 AssertStmt(AssertStmt),38}3940#[derive(Debug, Clone, Copy, PartialEq)]41pub enum UnaryOpType {42 Plus,43 Minus,44 BitNot,45 Not,46}4748#[derive(Debug, Clone, Copy, PartialEq)]49pub enum BinaryOpType {50 Mul,51 Div,52 Mod,5354 Add,55 Sub,5657 Lhs,58 Rhs,5960 Lt,61 Gt,62 Lte,63 Gte,6465 In,6667 Eq,68 Ne,6970 BitAnd,71 BitOr,72 BitXor,7374 And,75 Or,76}7778/// name, default value79#[derive(Debug, Clone, PartialEq)]80pub struct Param(pub String, pub Option<Box<Expr>>);81/// Defined function parameters82#[derive(Debug, Clone, PartialEq)]83pub struct ParamsDesc(pub Vec<Param>);84impl ParamsDesc {85 pub fn with_defaults(&self) -> Vec<Param> {86 self.087 .iter()88 .filter(|e| e.1.is_some())89 .map(|e| e.clone())90 .collect()91 }92}9394#[derive(Debug, Clone, PartialEq)]95pub struct Arg(pub Option<String>, pub Box<Expr>);96#[derive(Debug, Clone, PartialEq)]97pub struct ArgsDesc(pub Vec<Arg>);9899#[derive(Debug, Clone, PartialEq)]100pub struct BindSpec {101 pub name: String,102 pub params: Option<ParamsDesc>,103 pub value: Box<Expr>,104}105106#[derive(Debug, Clone, PartialEq)]107pub struct IfSpecData(pub Box<Expr>);108#[derive(Debug, Clone, PartialEq)]109pub struct ForSpecData(pub String, pub Box<Expr>);110111#[derive(Debug, Clone, PartialEq)]112pub enum CompSpec {113 IfSpec(IfSpecData),114 ForSpec(ForSpecData),115}116117#[derive(Debug, Clone, PartialEq)]118pub enum ObjBody {119 MemberList(Vec<Member>),120 ObjComp {121 pre_locals: Vec<BindSpec>,122 key: Box<Expr>,123 value: Box<Expr>,124 post_locals: Vec<BindSpec>,125 first: ForSpecData,126 rest: Vec<CompSpec>,127 },128}129130#[derive(Debug, Clone, PartialEq)]131pub enum LiteralType {132 This,133 Super,134 Dollar,135 Null,136 True,137 False,138}139impl Display for LiteralType {140 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {141 use LiteralType::*;142 match self {143 This => write!(f, "this"),144 Null => write!(f, "null"),145 True => write!(f, "true"),146 False => write!(f, "false"),147 _ => panic!("non printable item"),148 }149 }150}151152#[derive(Debug, Clone, PartialEq)]153pub struct SliceDesc {154 pub start: Option<Box<Expr>>,155 pub end: Option<Box<Expr>>,156 pub step: Option<Box<Expr>>,157}158159/// Syntax base160#[derive(Debug, Clone, PartialEq)]161pub enum Expr {162 Literal(LiteralType),163164 /// String value: "hello"165 Str(String),166 /// Number: 1, 2.0, 2e+20167 Num(f64),168 /// Variable name: test169 Var(String),170171 /// Array of expressions: [1, 2, "Hello"]172 Arr(Vec<Expr>),173 /// Array comprehension:174 /// ```jsonnet175 /// ingredients: [176 /// { kind: kind, qty: 4 / 3 }177 /// for kind in [178 /// 'Honey Syrup',179 /// 'Lemon Juice',180 /// 'Farmers Gin',181 /// ]182 /// ],183 /// ```184 ArrComp(Box<Expr>, ForSpecData, Vec<CompSpec>),185186 /// Object: {a: 2}187 Obj(ObjBody),188 /// Object extension: var1 {b: 2}189 ObjExtend(Box<Expr>, ObjBody),190191 /// (obj)192 Parened(Box<Expr>),193194 /// Params in function definition195 /// hello, world, test = 2196 Params(ParamsDesc),197 /// Args in function call198 /// 2 + 2, 3, named = 6199 Args(ArgsDesc),200201 /// -2202 UnaryOp(UnaryOpType, Box<Expr>),203 /// 2 - 2204 BinaryOp(Box<Expr>, BinaryOpType, Box<Expr>),205 /// assert 2 == 2 : "Math is broken"206 AssertExpr(AssertStmt, Box<Expr>),207 /// local a = 2; { b: a }208 LocalExpr(Vec<BindSpec>, Box<Expr>),209210 /// a = 3211 Bind(BindSpec),212 /// import "hello"213 Import(String),214 /// importStr "file.txt"215 ImportStr(String),216 /// error "I'm broken"217 Error(Box<Expr>),218 /// a(b, c)219 Apply(Box<Expr>, ArgsDesc),220 ///221 Select(Box<Expr>, String),222 /// a[b]223 Index(Box<Expr>, Box<Expr>),224 /// a[1::2]225 Slice(Box<Expr>, SliceDesc),226 /// function(x) x227 Function(ParamsDesc, Box<Expr>),228 /// if true == false then 1 else 2229 IfElse {230 cond: IfSpecData,231 cond_then: Box<Expr>,232 cond_else: Option<Box<Expr>>,233 },234 /// if 2 = 3235 IfSpec(IfSpecData),236 /// for elem in array237 ForSpec(ForSpecData),238}1use std::fmt::Display;23#[derive(Debug, Clone, PartialEq)]4pub enum FieldName {5 /// {fixed: 2}6 Fixed(String),7 /// {["dyn"+"amic"]: 3}8 Dyn(Box<Expr>),9}1011#[derive(Debug, Clone, PartialEq)]12pub enum Visibility {13 /// :14 Normal,15 /// ::16 Hidden,17 /// :::18 Unhide,19}2021#[derive(Debug, Clone, PartialEq)]22pub struct AssertStmt(pub Box<Expr>, pub Option<Box<Expr>>);2324#[derive(Debug, Clone, PartialEq)]25pub struct FieldMember {26 pub name: FieldName,27 pub plus: bool,28 pub params: Option<ParamsDesc>,29 pub visibility: Visibility,30 pub value: Expr,31}3233#[derive(Debug, Clone, PartialEq)]34pub enum Member {35 Field(FieldMember),36 BindStmt(BindSpec),37 AssertStmt(AssertStmt),38}3940#[derive(Debug, Clone, Copy, PartialEq)]41pub enum UnaryOpType {42 Plus,43 Minus,44 BitNot,45 Not,46}4748#[derive(Debug, Clone, Copy, PartialEq)]49pub enum BinaryOpType {50 Mul,51 Div,52 Mod,5354 Add,55 Sub,5657 Lhs,58 Rhs,5960 Lt,61 Gt,62 Lte,63 Gte,6465 In,6667 Eq,68 Ne,6970 BitAnd,71 BitOr,72 BitXor,7374 And,75 Or,76}7778/// name, default value79#[derive(Debug, Clone, PartialEq)]80pub struct Param(pub String, pub Option<Box<Expr>>);81/// Defined function parameters82#[derive(Debug, Clone, PartialEq)]83pub struct ParamsDesc(pub Vec<Param>);84impl ParamsDesc {85 pub fn with_defaults(&self) -> Vec<Param> {86 self.0.iter().filter(|e| e.1.is_some()).cloned().collect()87 }88}8990#[derive(Debug, Clone, PartialEq)]91pub struct Arg(pub Option<String>, pub Box<Expr>);92#[derive(Debug, Clone, PartialEq)]93pub struct ArgsDesc(pub Vec<Arg>);9495#[derive(Debug, Clone, PartialEq)]96pub struct BindSpec {97 pub name: String,98 pub params: Option<ParamsDesc>,99 pub value: Box<Expr>,100}101102#[derive(Debug, Clone, PartialEq)]103pub struct IfSpecData(pub Box<Expr>);104#[derive(Debug, Clone, PartialEq)]105pub struct ForSpecData(pub String, pub Box<Expr>);106107#[derive(Debug, Clone, PartialEq)]108pub enum CompSpec {109 IfSpec(IfSpecData),110 ForSpec(ForSpecData),111}112113#[derive(Debug, Clone, PartialEq)]114pub enum ObjBody {115 MemberList(Vec<Member>),116 ObjComp {117 pre_locals: Vec<BindSpec>,118 key: Box<Expr>,119 value: Box<Expr>,120 post_locals: Vec<BindSpec>,121 first: ForSpecData,122 rest: Vec<CompSpec>,123 },124}125126#[derive(Debug, Clone, PartialEq)]127pub enum LiteralType {128 This,129 Super,130 Dollar,131 Null,132 True,133 False,134}135impl Display for LiteralType {136 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {137 use LiteralType::*;138 match self {139 This => write!(f, "this"),140 Null => write!(f, "null"),141 True => write!(f, "true"),142 False => write!(f, "false"),143 _ => panic!("non printable item"),144 }145 }146}147148#[derive(Debug, Clone, PartialEq)]149pub struct SliceDesc {150 pub start: Option<Box<Expr>>,151 pub end: Option<Box<Expr>>,152 pub step: Option<Box<Expr>>,153}154155/// Syntax base156#[derive(Debug, Clone, PartialEq)]157pub enum Expr {158 Literal(LiteralType),159160 /// String value: "hello"161 Str(String),162 /// Number: 1, 2.0, 2e+20163 Num(f64),164 /// Variable name: test165 Var(String),166167 /// Array of expressions: [1, 2, "Hello"]168 Arr(Vec<Expr>),169 /// Array comprehension:170 /// ```jsonnet171 /// ingredients: [172 /// { kind: kind, qty: 4 / 3 }173 /// for kind in [174 /// 'Honey Syrup',175 /// 'Lemon Juice',176 /// 'Farmers Gin',177 /// ]178 /// ],179 /// ```180 ArrComp(Box<Expr>, ForSpecData, Vec<CompSpec>),181182 /// Object: {a: 2}183 Obj(ObjBody),184 /// Object extension: var1 {b: 2}185 ObjExtend(Box<Expr>, ObjBody),186187 /// (obj)188 Parened(Box<Expr>),189190 /// Params in function definition191 /// hello, world, test = 2192 Params(ParamsDesc),193 /// Args in function call194 /// 2 + 2, 3, named = 6195 Args(ArgsDesc),196197 /// -2198 UnaryOp(UnaryOpType, Box<Expr>),199 /// 2 - 2200 BinaryOp(Box<Expr>, BinaryOpType, Box<Expr>),201 /// assert 2 == 2 : "Math is broken"202 AssertExpr(AssertStmt, Box<Expr>),203 /// local a = 2; { b: a }204 LocalExpr(Vec<BindSpec>, Box<Expr>),205206 /// a = 3207 Bind(BindSpec),208 /// import "hello"209 Import(String),210 /// importStr "file.txt"211 ImportStr(String),212 /// error "I'm broken"213 Error(Box<Expr>),214 /// a(b, c)215 Apply(Box<Expr>, ArgsDesc),216 ///217 Select(Box<Expr>, String),218 /// a[b]219 Index(Box<Expr>, Box<Expr>),220 /// a[1::2]221 Slice(Box<Expr>, SliceDesc),222 /// function(x) x223 Function(ParamsDesc, Box<Expr>),224 /// if true == false then 1 else 2225 IfElse {226 cond: IfSpecData,227 cond_then: Box<Expr>,228 cond_else: Option<Box<Expr>>,229 },230 /// if 2 = 3231 IfSpec(IfSpecData),232 /// for elem in array233 ForSpec(ForSpecData),234}crates/jsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -23,8 +23,8 @@
/// For comma-delimited elements
rule comma() = quiet!{_ "," _} / expected!("<comma>")
- rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().nth(0).unwrap()}
- rule digit() -> char = d:$(['0'..='9']) {d.chars().nth(0).unwrap()}
+ rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}
+ rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}
rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']
/// Sequence of digits
rule uint() -> u32 = a:$(digit()+) { a.parse().unwrap() }
@@ -106,7 +106,7 @@
value,
post_locals,
first,
- rest: rest.unwrap_or(Vec::new()),
+ rest: rest.unwrap_or_default(),
}
}
/ members:(member() ** comma()) comma()? {expr::ObjBody::MemberList(members)}
@@ -119,7 +119,7 @@
pub rule parened_expr() -> Expr = "(" e:boxed_expr() ")" {Expr::Parened(e)}
pub rule obj_expr() -> Expr = "{" _ body:objinside() _ "}" {Expr::Obj(body)}
pub rule array_expr() -> Expr = "[" _ elems:(expr() ** comma()) _ comma()? "]" {Expr::Arr(elems)}
- pub rule array_comp_expr() -> Expr = "[" _ expr:boxed_expr() _ comma()? _ forspec:forspec() _ others:(others: compspec() _ {others})? "]" {Expr::ArrComp(expr, forspec, others.unwrap_or(vec![]))}
+ pub rule array_comp_expr() -> Expr = "[" _ expr:boxed_expr() _ comma()? _ forspec:forspec() _ others:(others: compspec() _ {others})? "]" {Expr::ArrComp(expr, forspec, others.unwrap_or_default())}
pub rule index_expr() -> Expr
= val:boxed_expr() "." idx:id() {Expr::Index(val, Box::new(Expr::Str(idx)))}
/ val:boxed_expr() "[" key:boxed_expr() "]" {Expr::Index(val, key)}
@@ -390,11 +390,14 @@
#[test]
fn infix_precedence() {
use Expr::*;
- assert_eq!(parse("!a && !b").unwrap(), BinaryOp(
- box UnaryOp(UnaryOpType::Not, box Var("a".to_owned())),
- BinaryOpType::And,
- box UnaryOp(UnaryOpType::Not, box Var("b".to_owned()))
- ));
+ assert_eq!(
+ parse("!a && !b").unwrap(),
+ BinaryOp(
+ box UnaryOp(UnaryOpType::Not, box Var("a".to_owned())),
+ BinaryOpType::And,
+ box UnaryOp(UnaryOpType::Not, box Var("b".to_owned()))
+ )
+ );
}
#[test]