difftreelog
style fix clippy warnings
in: master
9 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {7 // Applied in manifestification8 Manifest,9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,12 /// No line breaks, used in `obj+''`13 ToString,14 /// Minified json15 Minify,16}1718pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,20 pub mtype: ManifestType,21}2223pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {24 let mut out = String::new();25 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;26 Ok(out)27}28fn manifest_json_ex_buf(29 val: &Val,30 buf: &mut String,31 cur_padding: &mut String,32 options: &ManifestJsonOptions<'_>,33) -> Result<()> {34 use std::fmt::Write;35 let mtype = options.mtype;36 match val {37 Val::Bool(v) => {38 if *v {39 buf.push_str("true");40 } else {41 buf.push_str("false");42 }43 }44 Val::Null => buf.push_str("null"),45 Val::Str(s) => buf.push_str(&escape_string_json(&s)),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),47 Val::Arr(items) => {48 buf.push('[');49 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');52 }5354 let old_len = cur_padding.len();55 cur_padding.push_str(options.padding);56 for (i, item) in items.iter().enumerate() {57 if i != 0 {58 buf.push(',');59 if mtype == ManifestType::ToString {60 buf.push(' ');61 } else if mtype != ManifestType::Minify {62 buf.push('\n');63 }64 }65 buf.push_str(cur_padding);66 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;67 }68 cur_padding.truncate(old_len);6970 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');72 buf.push_str(cur_padding);73 }74 } else if mtype == ManifestType::Std {75 buf.push_str("\n\n");76 buf.push_str(cur_padding);77 } else if mtype == ManifestType::ToString {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 buf.push('{');84 let fields = obj.visible_fields();85 if !fields.is_empty() {86 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {87 buf.push('\n');88 }8990 let old_len = cur_padding.len();91 cur_padding.push_str(options.padding);92 for (i, field) in fields.into_iter().enumerate() {93 if i != 0 {94 buf.push(',');95 if mtype == ManifestType::ToString {96 buf.push(' ');97 } else if mtype != ManifestType::Minify {98 buf.push('\n');99 }100 }101 buf.push_str(cur_padding);102 buf.push_str(&escape_string_json(&field));103 buf.push_str(": ");104 manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, cur_padding, options)?;105 }106 cur_padding.truncate(old_len);107108 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {109 buf.push('\n');110 buf.push_str(cur_padding);111 }112 } else if mtype == ManifestType::Std {113 buf.push_str("\n\n");114 buf.push_str(cur_padding);115 } else if mtype == ManifestType::ToString {116 buf.push(' ');117 }118 buf.push('}');119 }120 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),121 };122 Ok(())123}124pub fn escape_string_json(s: &str) -> String {125 use std::fmt::Write;126 let mut out = String::new();127 out.push('"');128 for c in s.chars() {129 match c {130 '"' => out.push_str("\\\""),131 '\\' => out.push_str("\\\\"),132 '\u{0008}' => out.push_str("\\b"),133 '\u{000c}' => out.push_str("\\f"),134 '\n' => out.push_str("\\n"),135 '\r' => out.push_str("\\r"),136 '\t' => out.push_str("\\t"),137 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {138 write!(out, "\\u{:04x}", c as u32).unwrap()139 }140 c => out.push(c),141 }142 }143 out.push('"');144 out145}146147#[test]148fn json_test() {149 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")150}1use crate::error::Error::*;2use crate::error::Result;3use crate::{throw, Val};45#[derive(PartialEq, Clone, Copy)]6pub enum ManifestType {7 // Applied in manifestification8 Manifest,9 /// Used for std.manifestJson10 /// Empty array/objects extends to "[\n\n]" instead of "[ ]" as in manifest11 Std,12 /// No line breaks, used in `obj+''`13 ToString,14 /// Minified json15 Minify,16}1718pub struct ManifestJsonOptions<'s> {19 pub padding: &'s str,20 pub mtype: ManifestType,21}2223pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {24 let mut out = String::new();25 manifest_json_ex_buf(val, &mut out, &mut String::new(), options)?;26 Ok(out)27}28fn manifest_json_ex_buf(29 val: &Val,30 buf: &mut String,31 cur_padding: &mut String,32 options: &ManifestJsonOptions<'_>,33) -> Result<()> {34 use std::fmt::Write;35 let mtype = options.mtype;36 match val {37 Val::Bool(v) => {38 if *v {39 buf.push_str("true");40 } else {41 buf.push_str("false");42 }43 }44 Val::Null => buf.push_str("null"),45 Val::Str(s) => buf.push_str(&escape_string_json(s)),46 Val::Num(n) => write!(buf, "{}", n).unwrap(),47 Val::Arr(items) => {48 buf.push('[');49 if !items.is_empty() {50 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {51 buf.push('\n');52 }5354 let old_len = cur_padding.len();55 cur_padding.push_str(options.padding);56 for (i, item) in items.iter().enumerate() {57 if i != 0 {58 buf.push(',');59 if mtype == ManifestType::ToString {60 buf.push(' ');61 } else if mtype != ManifestType::Minify {62 buf.push('\n');63 }64 }65 buf.push_str(cur_padding);66 manifest_json_ex_buf(&item?, buf, cur_padding, options)?;67 }68 cur_padding.truncate(old_len);6970 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {71 buf.push('\n');72 buf.push_str(cur_padding);73 }74 } else if mtype == ManifestType::Std {75 buf.push_str("\n\n");76 buf.push_str(cur_padding);77 } else if mtype == ManifestType::ToString {78 buf.push(' ');79 }80 buf.push(']');81 }82 Val::Obj(obj) => {83 buf.push('{');84 let fields = obj.visible_fields();85 if !fields.is_empty() {86 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {87 buf.push('\n');88 }8990 let old_len = cur_padding.len();91 cur_padding.push_str(options.padding);92 for (i, field) in fields.into_iter().enumerate() {93 if i != 0 {94 buf.push(',');95 if mtype == ManifestType::ToString {96 buf.push(' ');97 } else if mtype != ManifestType::Minify {98 buf.push('\n');99 }100 }101 buf.push_str(cur_padding);102 buf.push_str(&escape_string_json(&field));103 buf.push_str(": ");104 manifest_json_ex_buf(&obj.get(field)?.unwrap(), buf, cur_padding, options)?;105 }106 cur_padding.truncate(old_len);107108 if mtype != ManifestType::ToString && mtype != ManifestType::Minify {109 buf.push('\n');110 buf.push_str(cur_padding);111 }112 } else if mtype == ManifestType::Std {113 buf.push_str("\n\n");114 buf.push_str(cur_padding);115 } else if mtype == ManifestType::ToString {116 buf.push(' ');117 }118 buf.push('}');119 }120 Val::Func(_) => throw!(RuntimeError("tried to manifest function".into())),121 };122 Ok(())123}124pub fn escape_string_json(s: &str) -> String {125 use std::fmt::Write;126 let mut out = String::new();127 out.push('"');128 for c in s.chars() {129 match c {130 '"' => out.push_str("\\\""),131 '\\' => out.push_str("\\\\"),132 '\u{0008}' => out.push_str("\\b"),133 '\u{000c}' => out.push_str("\\f"),134 '\n' => out.push_str("\\n"),135 '\r' => out.push_str("\\r"),136 '\t' => out.push_str("\\t"),137 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {138 write!(out, "\\u{:04x}", c as u32).unwrap()139 }140 c => out.push(c),141 }142 }143 out.push('"');144 out145}146147#[test]148fn json_test() {149 assert_eq!(escape_string_json("\u{001f}"), "\"\\u001f\"")150}crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -445,7 +445,7 @@
},
Val::Arr(a) => {
base64::encode(a.iter().map(|v| {
- Ok(v?.clone().unwrap_num()? as u8)
+ Ok(v?.unwrap_num()? as u8)
}).collect::<Result<Vec<_>>>()?).into()
},
_ => unreachable!()
@@ -589,7 +589,7 @@
name: &str,
args: &ArgsDesc,
) -> Result<Val> {
- if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).map(|f| *f)) {
+ if let Some(f) = BUILTINS.with(|builtins| builtins.get(name).copied()) {
return Ok(f(context, loc, args)?);
}
throw!(IntrinsicNotFound(name.into()))
crates/jrsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate.rs
@@ -473,7 +473,6 @@
}
v.get(n as usize)?
.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
- .clone()
}
(Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
crates/jrsonnet-evaluator/src/native.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/native.rs
+++ b/crates/jrsonnet-evaluator/src/native.rs
@@ -1,3 +1,5 @@
+#![allow(clippy::type_complexity)]
+
use crate::{error::Result, Val};
use jrsonnet_parser::ParamsDesc;
use std::fmt::Debug;
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -34,7 +34,7 @@
}
let mut debug = f.debug_struct("ObjValue");
for (name, member) in self.0.this_entries.iter() {
- debug.field(&name, member);
+ debug.field(name, member);
}
#[cfg(feature = "unstable")]
{
@@ -140,7 +140,7 @@
.evaluate()?)
}
- pub fn ptr_eq(a: &ObjValue, b: &ObjValue) -> bool {
+ pub fn ptr_eq(a: &Self, b: &Self) -> bool {
Rc::ptr_eq(&a.0, &b.0)
}
}
crates/jrsonnet-evaluator/src/typed.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed.rs
+++ b/crates/jrsonnet-evaluator/src/typed.rs
@@ -29,7 +29,7 @@
pub struct TypeLocError(Box<TypeError>, ValuePathStack);
impl From<TypeError> for TypeLocError {
fn from(e: TypeError) -> Self {
- TypeLocError(Box::new(e), ValuePathStack(Vec::new()))
+ Self(Box::new(e), ValuePathStack(Vec::new()))
}
}
impl From<TypeLocError> for LocError {
@@ -61,7 +61,7 @@
write!(out, "{}", err)?;
for (i, line) in out.lines().enumerate() {
- if line.trim().len() == 0 {
+ if line.trim().is_empty() {
continue;
}
if i != 0 {
@@ -118,8 +118,8 @@
impl Display for ValuePathItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
- ValuePathItem::Field(name) => write!(f, ".{}", name)?,
- ValuePathItem::Index(idx) => write!(f, "[{}]", idx)?,
+ Self::Field(name) => write!(f, ".{}", name)?,
+ Self::Index(idx) => write!(f, "[{}]", idx)?,
}
Ok(())
}
@@ -140,25 +140,25 @@
impl CheckType for ComplexValType {
fn check(&self, value: &Val) -> Result<()> {
match self {
- ComplexValType::Any => Ok(()),
- ComplexValType::Simple(s) => s.check(value),
- ComplexValType::Char => match value {
+ Self::Any => Ok(()),
+ Self::Simple(s) => s.check(value),
+ Self::Char => match value {
Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),
v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
- ComplexValType::BoundedNumber(from, to) => {
+ Self::BoundedNumber(from, to) => {
if let Val::Num(n) = value {
if from.map(|from| from > *n).unwrap_or(false)
|| to.map(|to| to <= *n).unwrap_or(false)
{
- return Err(TypeError::BoundsFailed(*n, from.clone(), to.clone()).into());
+ return Err(TypeError::BoundsFailed(*n, *from, *to).into());
}
Ok(())
} else {
Err(TypeError::ExpectedGot(self.clone(), value.value_type()).into())
}
}
- ComplexValType::Array(elem_type) => match value {
+ Self::Array(elem_type) => match value {
Val::Arr(a) => {
for (i, item) in a.iter().enumerate() {
push_type(
@@ -170,9 +170,9 @@
}
Ok(())
}
- v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
+ v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
- ComplexValType::ArrayRef(elem_type) => match value {
+ Self::ArrayRef(elem_type) => match value {
Val::Arr(a) => {
for (i, item) in a.iter().enumerate() {
push_type(
@@ -184,9 +184,9 @@
}
Ok(())
}
- v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
+ v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
- ComplexValType::ObjectRef(elems) => match value {
+ Self::ObjectRef(elems) => match value {
Val::Obj(obj) => {
for (k, v) in elems.iter() {
if let Some(got_v) = obj.get((*k).into())? {
@@ -202,11 +202,11 @@
);
}
}
- return Ok(());
+ Ok(())
}
- v => return Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
+ v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
- ComplexValType::Union(types) => {
+ Self::Union(types) => {
let mut errors = Vec::new();
for ty in types.iter() {
match ty.check(value) {
@@ -219,9 +219,9 @@
},
}
}
- return Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into());
+ Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())
}
- ComplexValType::UnionRef(types) => {
+ Self::UnionRef(types) => {
let mut errors = Vec::new();
for ty in types.iter() {
match ty.check(value) {
@@ -234,15 +234,15 @@
},
}
}
- return Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into());
+ Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())
}
- ComplexValType::Sum(types) => {
+ Self::Sum(types) => {
for ty in types.iter() {
ty.check(value)?
}
Ok(())
}
- ComplexValType::SumRef(types) => {
+ Self::SumRef(types) => {
for ty in types.iter() {
ty.check(value)?
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -176,14 +176,14 @@
pub enum ArrValue {
Lazy(Rc<Vec<LazyVal>>),
Eager(Rc<Vec<Val>>),
- Extended(Box<(ArrValue, ArrValue)>),
+ Extended(Box<(Self, Self)>),
}
impl ArrValue {
pub fn len(&self) -> usize {
match self {
- ArrValue::Lazy(l) => l.len(),
- ArrValue::Eager(e) => e.len(),
- ArrValue::Extended(v) => v.0.len() + v.1.len(),
+ Self::Lazy(l) => l.len(),
+ Self::Eager(e) => e.len(),
+ Self::Extended(v) => v.0.len() + v.1.len(),
}
}
@@ -193,15 +193,15 @@
pub fn get(&self, index: usize) -> Result<Option<Val>> {
match self {
- ArrValue::Lazy(vec) => {
+ Self::Lazy(vec) => {
if let Some(v) = vec.get(index) {
Ok(Some(v.evaluate()?))
} else {
Ok(None)
}
}
- ArrValue::Eager(vec) => Ok(vec.get(index).cloned()),
- ArrValue::Extended(v) => {
+ Self::Eager(vec) => Ok(vec.get(index).cloned()),
+ Self::Extended(v) => {
let a_len = v.0.len();
if a_len > index {
v.0.get(index)
@@ -214,12 +214,9 @@
pub fn get_lazy(&self, index: usize) -> Option<LazyVal> {
match self {
- ArrValue::Lazy(vec) => vec.get(index).cloned(),
- ArrValue::Eager(vec) => vec
- .get(index)
- .cloned()
- .map(|val| LazyVal::new_resolved(val)),
- ArrValue::Extended(v) => {
+ Self::Lazy(vec) => vec.get(index).cloned(),
+ Self::Eager(vec) => vec.get(index).cloned().map(LazyVal::new_resolved),
+ Self::Extended(v) => {
let a_len = v.0.len();
if a_len > index {
v.0.get_lazy(index)
@@ -232,15 +229,15 @@
pub fn evaluated(&self) -> Result<Rc<Vec<Val>>> {
Ok(match self {
- ArrValue::Lazy(vec) => {
+ Self::Lazy(vec) => {
let mut out = Vec::with_capacity(vec.len());
for item in vec.iter() {
out.push(item.evaluate()?);
}
Rc::new(out)
}
- ArrValue::Eager(vec) => vec.clone(),
- ArrValue::Extended(v) => {
+ Self::Eager(vec) => vec.clone(),
+ Self::Extended(_v) => {
let mut out = Vec::with_capacity(self.len());
for item in self.iter() {
out.push(item?);
@@ -252,40 +249,40 @@
pub fn iter(&self) -> impl DoubleEndedIterator<Item = Result<Val>> + '_ {
(0..self.len()).map(move |idx| match self {
- ArrValue::Lazy(l) => l[idx].evaluate(),
- ArrValue::Eager(e) => Ok(e[idx].clone()),
- ArrValue::Extended(_) => self.get(idx).map(|e| e.unwrap()),
+ Self::Lazy(l) => l[idx].evaluate(),
+ Self::Eager(e) => Ok(e[idx].clone()),
+ Self::Extended(_) => self.get(idx).map(|e| e.unwrap()),
})
}
pub fn iter_lazy(&self) -> impl DoubleEndedIterator<Item = LazyVal> + '_ {
(0..self.len()).map(move |idx| match self {
- ArrValue::Lazy(l) => l[idx].clone(),
- ArrValue::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
- ArrValue::Extended(_) => self.get_lazy(idx).unwrap(),
+ Self::Lazy(l) => l[idx].clone(),
+ Self::Eager(e) => LazyVal::new_resolved(e[idx].clone()),
+ Self::Extended(_) => self.get_lazy(idx).unwrap(),
})
}
pub fn reversed(self) -> Self {
match self {
- ArrValue::Lazy(vec) => {
+ Self::Lazy(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
Self::Lazy(Rc::new(out))
}
- ArrValue::Eager(vec) => {
+ Self::Eager(vec) => {
let mut out = (&vec as &Vec<_>).clone();
out.reverse();
Self::Eager(Rc::new(out))
}
- ArrValue::Extended(b) => ArrValue::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
+ Self::Extended(b) => Self::Extended(Box::new((b.1.reversed(), b.0.reversed()))),
}
}
- pub fn ptr_eq(a: &ArrValue, b: &ArrValue) -> bool {
+ pub fn ptr_eq(a: &Self, b: &Self) -> bool {
match (a, b) {
- (ArrValue::Lazy(a), ArrValue::Lazy(b)) => Rc::ptr_eq(a, b),
- (ArrValue::Eager(a), ArrValue::Eager(b)) => Rc::ptr_eq(a, b),
+ (Self::Lazy(a), Self::Lazy(b)) => Rc::ptr_eq(a, b),
+ (Self::Eager(a), Self::Eager(b)) => Rc::ptr_eq(a, b),
_ => false,
}
}
@@ -359,7 +356,7 @@
self.assert_type(context, ValType::Num)?;
self.unwrap_num()
}
- pub fn value_type(&self) -> ValType {
+ pub const fn value_type(&self) -> ValType {
match self {
Self::Str(..) => ValType::Str,
Self::Num(..) => ValType::Num,
@@ -378,7 +375,7 @@
Self::Null => "null".into(),
Self::Str(s) => s.clone(),
v => manifest_json_ex(
- &v,
+ v,
&ManifestJsonOptions {
padding: "",
mtype: ManifestType::ToString,
@@ -556,7 +553,7 @@
(Val::Obj(_), Val::Obj(_)) => throw!(RuntimeError(
"primitiveEquals operates on primitive types, got object".into(),
)),
- (a, b) if is_function_like(&a) && is_function_like(&b) => {
+ (a, b) if is_function_like(a) && is_function_like(b) => {
throw!(RuntimeError("cannot test equality of functions".into()))
}
(_, _) => false,
@@ -598,6 +595,6 @@
}
Ok(true)
}
- (a, b) => Ok(primitive_equals(&a, &b)?),
+ (a, b) => Ok(primitive_equals(a, b)?),
}
}
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -68,7 +68,7 @@
IStr(STR_POOL.with(|pool| {
let mut pool = pool.borrow_mut();
if let Some((k, _)) = pool.get_key_value(str) {
- return k.clone();
+ k.clone()
} else {
let rc: Rc<str> = str.into();
pool.insert(rc.clone(), ());
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-types/src/lib.rs
+++ b/crates/jrsonnet-types/src/lib.rs
@@ -133,10 +133,8 @@
union: &[ComplexValType],
) -> std::fmt::Result {
for (i, v) in union.iter().enumerate() {
- let should_add_braces = match v {
- ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union => true,
- _ => false,
- };
+ let should_add_braces =
+ matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);
if i != 0 {
write!(f, " {} ", if is_union { '|' } else { '&' })?;
}