From a6fe813ed115d3b3432a85e1c831644968c9c614 Mon Sep 17 00:00:00 2001 From: elkowar <5300871+elkowar@users.noreply.github.com> Date: Sat, 17 Jul 2021 18:07:25 +0200 Subject: [PATCH] fix clippy lints --- Cargo.toml | 1 - examples/errors.rs | 25 ------------------------ src/ast.rs | 19 ------------------ src/dynval.rs | 48 +++++++++++++++++++--------------------------- src/error.rs | 27 -------------------------- src/eval.rs | 3 ++- src/lib.rs | 5 ++++- src/parser/mod.rs | 2 +- 8 files changed, 27 insertions(+), 103 deletions(-) delete mode 100644 examples/errors.rs diff --git a/Cargo.toml b/Cargo.toml index 2d6c0c2..df354ef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,6 @@ regex = "1" itertools = "0.10" thiserror = "1.0" maplit = "1.0" -codespan-reporting = "0.11" logos = "0.12" serde = {version = "1.0", features = ["derive"]} diff --git a/examples/errors.rs b/examples/errors.rs deleted file mode 100644 index 5b36355..0000000 --- a/examples/errors.rs +++ /dev/null @@ -1,25 +0,0 @@ -use std::collections::HashMap; - -fn main() { - let mut files = codespan_reporting::files::SimpleFiles::new(); - - let input = "12 + foo * 2 < 2 ? bar == true : false"; - - let _ = files.add("foo.eww", input); - let ast = simplexpr::parser::parse_string(input); - - let mut vars = HashMap::new(); - vars.insert("foo".to_string(), "2".into()); - - match ast.and_then(|x| x.eval(&vars).map_err(|e| e.into())) { - Ok(ast) => { - println!("{:?}", ast); - } - Err(err) => { - let diag = err.pretty_diagnostic(); - use codespan_reporting::term; - let mut writer = term::termcolor::StandardStream::stderr(term::termcolor::ColorChoice::Always); - term::emit(&mut writer, &term::Config::default(), &files, &diag).unwrap(); - } - } -} diff --git a/src/ast.rs b/src/ast.rs index 8b70dd9..8b7562c 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -5,20 +5,6 @@ use serde::{Deserialize, Serialize}; #[derive(Eq, PartialEq, Clone, Copy, Serialize, Deserialize)] pub struct Span(pub usize, pub usize); -pub trait MaybeSpanned { - fn try_span(&self) -> Option; -} - -impl MaybeSpanned for Span { - fn try_span(&self) -> Option { - Some(*self) - } -} - -pub fn span_between(a: impl MaybeSpanned, b: impl MaybeSpanned) -> Option { - Some(Span(a.try_span()?.0, b.try_span()?.1)) -} - impl std::fmt::Display for Span { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}..{}", self.0, self.1) @@ -65,11 +51,6 @@ pub enum SimplExpr { JsonAccess(Span, Box, Box), FunctionCall(Span, String, Vec), } -impl MaybeSpanned for SimplExpr { - fn try_span(&self) -> Option { - Some(self.span()) - } -} impl std::fmt::Display for SimplExpr { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/dynval.rs b/src/dynval.rs index dbdd9d3..b3b5b2b 100644 --- a/src/dynval.rs +++ b/src/dynval.rs @@ -1,4 +1,4 @@ -use crate::ast::{MaybeSpanned, Span}; +use crate::ast::Span; use itertools::Itertools; use serde::{Deserialize, Serialize}; use std::{convert::TryFrom, fmt, iter::FromIterator}; @@ -26,12 +26,6 @@ impl ConversionError { #[derive(Clone, Deserialize, Serialize, Default, Eq)] pub struct DynVal(pub String, pub Option); -impl MaybeSpanned for DynVal { - fn try_span(&self) -> Option { - self.1 - } -} - impl From for DynVal { fn from(s: String) -> Self { DynVal(s, None) @@ -98,7 +92,7 @@ impl_try_from!(impl From { for f64 => |x| x.as_f64(); for i32 => |x| x.as_i32(); for bool => |x| x.as_bool(); - for Vec => |x| x.as_vec(); + //for Vec => |x| x.as_vec(); }); impl_primval_from!(bool, i32, u32, f32, u8, f64, &str); @@ -145,26 +139,24 @@ impl DynVal { self.0.parse().map_err(|e| ConversionError::new(self.clone(), "bool", Box::new(e))) } - pub fn as_vec(&self) -> Result> { - match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) { - Some(content) => { - let mut items: Vec = content.split(',').map(|x: &str| x.to_string()).collect(); - let mut removed = 0; - for times_ran in 0..items.len() { - // escapes `,` if there's a `\` before em - if items[times_ran - removed].ends_with('\\') { - items[times_ran - removed].pop(); - let it = items.remove((times_ran + 1) - removed); - items[times_ran - removed] += ","; - items[times_ran - removed] += ⁢ - removed += 1; - } - } - Ok(items) - } - None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }), - } - } + // pub fn as_vec(&self) -> Result> { + // match self.0.strip_prefix('[').and_then(|x| x.strip_suffix(']')) { + // Some(content) => { + // let mut items: Vec = content.split(',').map(|x: &str| x.to_string()).collect(); + // let mut removed = 0; + // for times_ran in 0..items.len() { + //// escapes `,` if there's a `\` before em + // if items[times_ran - removed].ends_with('\\') { + // items[times_ran - removed].pop(); + // let it = items.remove((times_ran + 1) - removed); + // items[times_ran - removed] += ","; + // items[times_ran - removed] += ⁢ + // removed += 1; + //} + // Ok(items) + //} + // None => Err(ConversionError { value: self.clone(), target_type: "vec", source: None }), + //} pub fn as_json_value(&self) -> Result { serde_json::from_str::(&self.0) diff --git a/src/error.rs b/src/error.rs index e66b50c..22b6041 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,6 @@ use crate::{ dynval, parser::lexer::{self, LexicalError}, }; -use codespan_reporting::diagnostic; pub type Result = std::result::Result; #[derive(thiserror::Error, Debug)] @@ -42,32 +41,6 @@ impl Error { _ => None, } } - - pub fn pretty_diagnostic(&self) -> diagnostic::Diagnostic { - let diag = diagnostic::Diagnostic::error().with_message(format!("{}", self)); - if let Some(span) = self.get_span() { - diag.with_labels(vec![diagnostic::Label::primary(0, span.0..span.1)]) - } else { - diag - } - } -} - -pub trait ErrorExt { - fn at(self, span: Span) -> Error; -} -impl ErrorExt for Box { - fn at(self, span: Span) -> Error { - Error::Spanned(span, self) - } -} -pub trait ResultExt { - fn at(self, span: Span) -> std::result::Result; -} -impl ResultExt for std::result::Result { - fn at(self, span: Span) -> std::result::Result { - self.map_err(|x| Error::Spanned(span, Box::new(x))) - } } fn get_parse_error_span(err: &lalrpop_util::ParseError) -> Option { diff --git a/src/eval.rs b/src/eval.rs index 2fcb3ce..01d58bd 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -51,7 +51,7 @@ type VarName = String; pub trait FunctionSource { type Err; - fn run_fn(&self, name: &str, args: &Vec) -> Result; + fn run_fn(&self, name: &str, args: &[DynVal]) -> Result; } impl SimplExpr { @@ -139,6 +139,7 @@ impl SimplExpr { BinOp::Mod => DynVal::from(a.as_f64()? % b.as_f64()?), BinOp::GT => DynVal::from(a.as_f64()? > b.as_f64()?), BinOp::LT => DynVal::from(a.as_f64()? < b.as_f64()?), + #[allow(clippy::useless_conversion)] BinOp::Elvis => DynVal::from(if a.0.is_empty() { b } else { a }), BinOp::RegexMatch => { let regex = regex::Regex::new(&b.as_string()?)?; diff --git a/src/lib.rs b/src/lib.rs index 991b775..2398211 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,10 @@ pub mod parser; use ast::SimplExpr; use lalrpop_util::lalrpop_mod; -lalrpop_mod!(pub simplexpr_parser); +lalrpop_mod!( + #[allow(clippy::all)] + pub simplexpr_parser +); pub fn parse_string(s: &str) -> Result { parser::parse_string(s) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index ab47e58..a867374 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9,7 +9,7 @@ use crate::{ pub fn parse_string(s: &str) -> Result { let lexer = lexer::Lexer::new(s); let parser = crate::simplexpr_parser::ExprParser::new(); - Ok(parser.parse(lexer).map_err(|e| Error::from_parse_error(e))?) + parser.parse(lexer).map_err(Error::from_parse_error) } #[cfg(test)]