difftreelog
style fix clippy warnings
in: master
5 files changed
crates/jrsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/ctx.rs
+++ b/crates/jrsonnet-evaluator/src/ctx.rs
@@ -38,7 +38,7 @@
#[derive(Debug, Clone)]
pub struct Context(Rc<ContextInternals>);
impl Context {
- pub fn new_future() -> FutureWrapper<Context> {
+ pub fn new_future() -> FutureWrapper<Self> {
FutureWrapper::new()
}
@@ -71,7 +71,7 @@
.cloned()
.ok_or(VariableIsNotDefined(name))?)
}
- pub fn into_future(self, ctx: FutureWrapper<Context>) -> Self {
+ pub fn into_future(self, ctx: FutureWrapper<Self>) -> Self {
{
ctx.0.borrow_mut().replace(self);
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1,6 +1,6 @@
#![cfg_attr(feature = "unstable", feature(stmt_expr_attributes))]
-#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
#![warn(clippy::all, clippy::nursery)]
+#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths, clippy::ptr_arg)]
mod builtin;
mod ctx;
@@ -429,7 +429,7 @@
}
pub fn resolve_file(&self, from: &PathBuf, path: &PathBuf) -> Result<Rc<PathBuf>> {
- Ok(self.settings().import_resolver.resolve_file(from, path)?)
+ self.settings().import_resolver.resolve_file(from, path)
}
pub fn load_file_contents(&self, path: &PathBuf) -> Result<IStr> {
self.settings().import_resolver.load_file_contents(path)
crates/jrsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/obj.rs
+++ b/crates/jrsonnet-evaluator/src/obj.rs
@@ -173,7 +173,7 @@
pub fn extend_with_field(self, key: IStr, value: ObjMember) -> Self {
let mut new = FxHashMap::with_capacity_and_hasher(1, BuildHasherDefault::default());
new.insert(key, value);
- ObjValue::new(Some(self), Rc::new(new))
+ Self::new(Some(self), Rc::new(new))
}
pub(crate) fn get_raw(&self, key: IStr, real_this: Option<&Self>) -> Result<Option<Val>> {
crates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -1,3 +1,5 @@
+#![allow(clippy::redundant_closure_call)]
+
use peg::parser;
use std::{path::PathBuf, rc::Rc};
mod expr;
crates/jrsonnet-types/src/lib.rsdiffbeforeafterboth1use std::fmt::Display;23#[macro_export]4macro_rules! ty {5 ((Array<number>)) => {{6 $crate::ComplexValType::ArrayRef(&$crate::ComplexValType::Simple($crate::ValType::Num))7 }};8 (array) => {9 $crate::ComplexValType::Simple($crate::ValType::Arr)10 };11 (boolean) => {12 $crate::ComplexValType::Simple($crate::ValType::Bool)13 };14 (null) => {15 $crate::ComplexValType::Simple($crate::ValType::Null)16 };17 (string) => {18 $crate::ComplexValType::Simple($crate::ValType::Str)19 };20 (char) => {21 $crate::ComplexValType::Char22 };23 (number) => {24 $crate::ComplexValType::Simple($crate::ValType::Num)25 };26 (BoundedNumber<($min:expr), ($max:expr)>) => {{27 $crate::ComplexValType::BoundedNumber($min, $max)28 }};29 (object) => {30 $crate::ComplexValType::Simple($crate::ValType::Obj)31 };32 (any) => {33 $crate::ComplexValType::Any34 };35 (function) => {36 $crate::ComplexValType::Simple($crate::ValType::Func)37 };38 (($($a:tt) |+)) => {{39 static CONTENTS: &'static [$crate::ComplexValType] = &[40 $(ty!($a)),+41 ];42 $crate::ComplexValType::UnionRef(CONTENTS)43 }};44 (($($a:tt) &+)) => {{45 static CONTENTS: &'static [$crate::ComplexValType] = &[46 $(ty!($a)),+47 ];48 $crate::ComplexValType::SumRef(CONTENTS)49 }};50}5152#[test]53fn test() {54 assert_eq!(55 ty!((Array<number>)),56 ComplexValType::ArrayRef(&ComplexValType::Simple(ValType::Num))57 );58 assert_eq!(ty!(array), ComplexValType::Simple(ValType::Arr));59 assert_eq!(ty!(any), ComplexValType::Any);60 assert_eq!(61 ty!((string | number)),62 ComplexValType::UnionRef(&[63 ComplexValType::Simple(ValType::Str),64 ComplexValType::Simple(ValType::Num)65 ])66 );67 assert_eq!(68 format!("{}", ty!(((string & number) | (object & null)))),69 "string & number | object & null"70 );71 assert_eq!(format!("{}", ty!((string | array))), "string | array");72 assert_eq!(73 format!("{}", ty!(((string & number) | array))),74 "string & number | array"75 );76}7778#[derive(Debug, Clone, Copy, PartialEq, Eq)]79pub enum ValType {80 Bool,81 Null,82 Str,83 Num,84 Arr,85 Obj,86 Func,87}8889impl ValType {90 pub const fn name(&self) -> &'static str {91 use ValType::*;92 match self {93 Bool => "boolean",94 Null => "null",95 Str => "string",96 Num => "number",97 Arr => "array",98 Obj => "object",99 Func => "function",100 }101 }102}103104impl Display for ValType {105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {106 write!(f, "{}", self.name())107 }108}109110#[derive(Debug, Clone, PartialEq)]111pub enum ComplexValType {112 Any,113 Char,114 Simple(ValType),115 BoundedNumber(Option<f64>, Option<f64>),116 Array(Box<ComplexValType>),117 ArrayRef(&'static ComplexValType),118 ObjectRef(&'static [(&'static str, ComplexValType)]),119 Union(Vec<ComplexValType>),120 UnionRef(&'static [ComplexValType]),121 Sum(Vec<ComplexValType>),122 SumRef(&'static [ComplexValType]),123}124impl From<ValType> for ComplexValType {125 fn from(s: ValType) -> Self {126 Self::Simple(s)127 }128}129130fn write_union(131 f: &mut std::fmt::Formatter<'_>,132 is_union: bool,133 union: &[ComplexValType],134) -> std::fmt::Result {135 for (i, v) in union.iter().enumerate() {136 let should_add_braces =137 matches!(v, ComplexValType::UnionRef(_) | ComplexValType::Union(_) if !is_union);138 if i != 0 {139 write!(f, " {} ", if is_union { '|' } else { '&' })?;140 }141 if should_add_braces {142 write!(f, "(")?;143 }144 write!(f, "{}", v)?;145 if should_add_braces {146 write!(f, ")")?;147 }148 }149 Ok(())150}151152fn print_array(a: &ComplexValType, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {153 if *a == ComplexValType::Any {154 write!(f, "array")?155 } else {156 write!(f, "Array<{}>", a)?157 }158 Ok(())159}160161impl Display for ComplexValType {162 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {163 match self {164 ComplexValType::Any => write!(f, "any")?,165 ComplexValType::Simple(s) => write!(f, "{}", s)?,166 ComplexValType::Char => write!(f, "char")?,167 ComplexValType::BoundedNumber(a, b) => write!(168 f,169 "BoundedNumber<{}, {}>",170 a.map(|e| e.to_string()).unwrap_or_else(|| "".into()),171 b.map(|e| e.to_string()).unwrap_or_else(|| "".into())172 )?,173 ComplexValType::ArrayRef(a) => print_array(a, f)?,174 ComplexValType::Array(a) => print_array(a, f)?,175 ComplexValType::ObjectRef(fields) => {176 write!(f, "{{")?;177 for (i, (k, v)) in fields.iter().enumerate() {178 if i != 0 {179 write!(f, ", ")?;180 }181 write!(f, "{}: {}", k, v)?;182 }183 write!(f, "}}")?;184 }185 ComplexValType::Union(v) => write_union(f, true, v)?,186 ComplexValType::UnionRef(v) => write_union(f, true, v)?,187 ComplexValType::Sum(v) => write_union(f, false, v)?,188 ComplexValType::SumRef(v) => write_union(f, false, v)?,189 };190 Ok(())191 }192}193194peg::parser! {195pub grammar parser() for str {196 rule number() -> f64197 = n:$(['0'..='9']+) { n.parse().unwrap() }198199 rule any_ty() -> ComplexValType = "any" { ComplexValType::Any }200 rule char_ty() -> ComplexValType = "character" { ComplexValType::Char }201 rule bool_ty() -> ComplexValType = "boolean" { ComplexValType::Simple(ValType::Bool) }202 rule null_ty() -> ComplexValType = "null" { ComplexValType::Simple(ValType::Null) }203 rule str_ty() -> ComplexValType = "string" { ComplexValType::Simple(ValType::Str) }204 rule num_ty() -> ComplexValType = "number" { ComplexValType::Simple(ValType::Num) }205 rule simple_array_ty() -> ComplexValType = "array" { ComplexValType::Simple(ValType::Arr) }206 rule simple_object_ty() -> ComplexValType = "object" { ComplexValType::Simple(ValType::Obj) }207 rule simple_function_ty() -> ComplexValType = "function" { ComplexValType::Simple(ValType::Func) }208209 rule array_ty() -> ComplexValType210 = "Array<" t:ty() ">" { ComplexValType::Array(Box::new(t)) }211212 rule bounded_number_ty() -> ComplexValType213 = "BoundedNumber<" a:number() ", " b:number() ">" { ComplexValType::BoundedNumber(Some(a), Some(b)) }214215 rule ty_basic() -> ComplexValType216 = any_ty()217 / char_ty()218 / bool_ty()219 / null_ty()220 / str_ty()221 / num_ty()222 / simple_array_ty()223 / simple_object_ty()224 / simple_function_ty()225 / array_ty()226 / bounded_number_ty()227228 pub rule ty() -> ComplexValType229 = precedence! {230 a:(@) " | " b:@ {231 match a {232 ComplexValType::Union(mut a) => {233 a.push(b);234 ComplexValType::Union(a)235 }236 _ => ComplexValType::Union(vec![a, b]),237 }238 }239 --240 a:(@) " & " b:@ {241 match a {242 ComplexValType::Sum(mut a) => {243 a.push(b);244 ComplexValType::Sum(a)245 }246 _ => ComplexValType::Sum(vec![a, b]),247 }248 }249 --250 "(" t:ty() ")" { t }251 t:ty_basic() { t }252 }253}254}255256#[cfg(test)]257pub mod tests {258 use super::parser;259260 #[test]261 fn precedence() {262 assert_eq!(263 parser::ty("(any & any) | (any | any) & any")264 .unwrap()265 .to_string(),266 "any & any | (any | any) & any"267 );268 }269270 #[test]271 fn array() {272 assert_eq!(parser::ty("Array<any>").unwrap().to_string(), "array");273 assert_eq!(274 parser::ty("Array<number>").unwrap().to_string(),275 "Array<number>"276 );277 }278 #[test]279 fn bounded_number() {280 assert_eq!(281 parser::ty("BoundedNumber<1, 2>").unwrap().to_string(),282 "BoundedNumber<1, 2>"283 );284 }285}