Skip to content

Commit

Permalink
run cargo clippy --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Apr 18, 2023
1 parent a547caf commit 90ff56e
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 91 deletions.
80 changes: 36 additions & 44 deletions src/expression_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ impl ExpressionParser {
}

pub fn full_expresion_parser<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<FullExpressionEvaluator<'a>> {
let mut evaluator = FullExpressionEvaluator::default();

let value = ExpressionParser::parse_logical_or(&mut lexer);
let value = ExpressionParser::parse_logical_or(lexer);
match value {
Ok(expression) => evaluator.set_expression(expression),
Err(err) => return Err(err),
Expand All @@ -41,10 +41,8 @@ impl ExpressionParser {
Ok(evaluator)
}

fn parse_logical_or<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_logical_and(&mut lexer)?;
fn parse_logical_or<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_logical_and(lexer)?;
if let Some(Token::LogicalOr) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_or(lexer)?;
Expand All @@ -57,10 +55,8 @@ impl ExpressionParser {
Ok(left)
}

fn parse_logical_and<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_logical_compare(&mut lexer)?;
fn parse_logical_and<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_logical_compare(lexer)?;
if let Some(Token::LogicalAnd) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_and(lexer)?;
Expand All @@ -74,9 +70,9 @@ impl ExpressionParser {
}

fn parse_logical_compare<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_string_concat(&mut lexer)?;
let left = ExpressionParser::parse_string_concat(lexer)?;

let binary_op = match lexer.peek() {
Some(Token::Equal) => BinaryOperation::LogicalEq,
Expand All @@ -89,7 +85,7 @@ impl ExpressionParser {
};

lexer.next();
let right = ExpressionParser::parse_string_concat(&mut lexer)?;
let right = ExpressionParser::parse_string_concat(lexer)?;

Ok(Expression::BinaryExpression(
binary_op,
Expand All @@ -98,13 +94,11 @@ impl ExpressionParser {
))
}

fn parse_string_concat<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_math_pow(&mut lexer)?;
fn parse_string_concat<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_math_pow(lexer)?;
if let Some(Token::Tilde) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_and(&mut lexer)?;
let right = ExpressionParser::parse_logical_and(lexer)?;
return Ok(Expression::BinaryExpression(
BinaryOperation::StringConcat,
Box::new(left),
Expand All @@ -114,11 +108,11 @@ impl ExpressionParser {
Ok(left)
}

fn parse_math_pow<'a>(mut lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_math_plus_minus(&mut lexer)?;
fn parse_math_pow<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_math_plus_minus(lexer)?;
if let Some(Token::MulMul) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_math_pow(&mut lexer)?;
let right = ExpressionParser::parse_math_pow(lexer)?;
return Ok(Expression::BinaryExpression(
BinaryOperation::Pow,
Box::new(left),
Expand All @@ -129,27 +123,25 @@ impl ExpressionParser {
}

fn parse_math_plus_minus<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_math_mul_div(&mut lexer)?;
let left = ExpressionParser::parse_math_mul_div(lexer)?;
let binary_op = match lexer.peek() {
Some(Token::Plus) => BinaryOperation::Plus,
Some(Token::Minus) => BinaryOperation::Minus,
_ => return Ok(left),
};
lexer.next();
let right = ExpressionParser::parse_math_plus_minus(&mut lexer)?;
let right = ExpressionParser::parse_math_plus_minus(lexer)?;
Ok(Expression::BinaryExpression(
binary_op,
Box::new(left),
Box::new(right),
))
}

fn parse_math_mul_div<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_unary_plus_min(&mut lexer)?;
fn parse_math_mul_div<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let left = ExpressionParser::parse_unary_plus_min(lexer)?;
let binary_op = match lexer.peek() {
Some(Token::Mul) => BinaryOperation::Mul,
Some(Token::Div) => BinaryOperation::Div,
Expand All @@ -158,7 +150,7 @@ impl ExpressionParser {
_ => return Ok(left),
};
lexer.next();
let right = ExpressionParser::parse_math_mul_div(&mut lexer)?;
let right = ExpressionParser::parse_math_mul_div(lexer)?;

Ok(Expression::BinaryExpression(
binary_op,
Expand All @@ -168,7 +160,7 @@ impl ExpressionParser {
}

fn parse_unary_plus_min<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let unary_op = match lexer.peek() {
Some(Token::Plus) => Some(UnaryOperation::Plus),
Expand All @@ -189,7 +181,7 @@ impl ExpressionParser {

if let Some(Token::Pipe) = lexer.peek() {
lexer.next();
let filter_expression = ExpressionParser::parse_filter_expression(&mut lexer)?;
let filter_expression = ExpressionParser::parse_filter_expression(lexer)?;
Ok(Expression::FilteredExpression(FilteredExpression::new(
Box::new(result),
filter_expression,
Expand Down Expand Up @@ -281,7 +273,7 @@ impl ExpressionParser {
}
}
fn parse_value_expression<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let token = lexer.next();

Expand All @@ -292,12 +284,12 @@ impl ExpressionParser {
Token::False => Expression::Constant(Value::from(false)),
Token::FloatNum(num) => Expression::Constant(Value::from(num)),
Token::String(string) => Expression::Constant(Value::from(string.to_string())),
Token::LBracket => ExpressionParser::parse_braced_expression_or_tuple(&mut lexer)?,
Token::LBracket => ExpressionParser::parse_braced_expression_or_tuple(lexer)?,
Token::Identifier(identifier) => {
Expression::ValueRef(ValueRefExpression::new(identifier.to_string()))
}
Token::LSqBracket => ExpressionParser::parse_tuple(&mut lexer)?,
Token::LCrlBracket => ExpressionParser::parse_dict(&mut lexer)?,
Token::LSqBracket => ExpressionParser::parse_tuple(lexer)?,
Token::LCrlBracket => ExpressionParser::parse_dict(lexer)?,

_ => {
let range = lexer.span();
Expand All @@ -319,7 +311,7 @@ impl ExpressionParser {

let value = match token {
Some(Token::LSqBracket) | Some(Token::Point) => {
ExpressionParser::parse_subscript(&mut lexer, value)?
ExpressionParser::parse_subscript(lexer, value)?
}
Some(Token::LBracket) => todo!(),
_ => value,
Expand All @@ -329,7 +321,7 @@ impl ExpressionParser {
}

fn parse_braced_expression_or_tuple<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
) -> Result<Expression<'a>> {
let mut is_tuple: bool = false;
let mut exprs = vec![];
Expand All @@ -338,7 +330,7 @@ impl ExpressionParser {
lexer.next();
break;
}
let expr = ExpressionParser::parse_logical_or(&mut lexer);
let expr = ExpressionParser::parse_logical_or(lexer);
match expr {
Ok(expr) => exprs.push(expr),
Err(err) => {
Expand Down Expand Up @@ -368,15 +360,15 @@ impl ExpressionParser {
}
}
fn parse_subscript<'a>(
mut lexer: &mut PeekableLexer<'a, Token<'a>>,
lexer: &mut PeekableLexer<'a, Token<'a>>,
expression: Expression<'a>,
) -> Result<Expression<'a>> {
let mut subscript = SubscriptExpression::new(Box::new(expression));
while let Some(token) = lexer.peek() {
match token {
Token::LSqBracket => {
lexer.next();
let expr = ExpressionParser::full_expresion_parser(&mut lexer)?;
let expr = ExpressionParser::full_expresion_parser(lexer)?;
if let Some(Token::RSqBracket) = lexer.next() {
subscript.add_index(Box::new(expr));
} else {
Expand Down Expand Up @@ -408,15 +400,15 @@ impl ExpressionParser {

Ok(Expression::SubscriptExpression(subscript))
}
fn parse_tuple<'a>(mut lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
fn parse_tuple<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let mut tuple = TupleExpression::default();
if let Some(Token::RSqBracket) = lexer.peek() {
lexer.next();
return Ok(Expression::Tuple(tuple));
}

loop {
let expr = ExpressionParser::full_expresion_parser(&mut lexer)?;
let expr = ExpressionParser::full_expresion_parser(lexer)?;
tuple.push(Box::new(expr));
if let Some(Token::Comma) = lexer.peek() {
lexer.next();
Expand All @@ -436,7 +428,7 @@ impl ExpressionParser {
)))
}
}
fn parse_dict<'a>(mut lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
fn parse_dict<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let mut dict = DictionaryExpression::default();
if let Some(Token::RCrlBracket) = lexer.peek() {
lexer.next();
Expand All @@ -446,7 +438,7 @@ impl ExpressionParser {
let key = lexer.next();
if let Some(Token::String(key_string)) = key {
if let Some(Token::Colon) = lexer.next() {
let expr = ExpressionParser::full_expresion_parser(&mut lexer)?;
let expr = ExpressionParser::full_expresion_parser(lexer)?;
dict.push(key_string.to_string(), Box::new(expr));
if let Some(Token::Comma) = lexer.peek() {
lexer.next();
Expand Down
29 changes: 10 additions & 19 deletions src/statement/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,24 @@ pub struct StatementParser;
use std::sync::Arc;

impl StatementParser {
pub fn parse<'a>(
text: &'a str,
mut statementinfo_list: &mut StatementInfoList<'a>,
) -> Result<()> {
pub fn parse<'a>(text: &'a str, statementinfo_list: &mut StatementInfoList<'a>) -> Result<()> {
let lexer: Lexer<'_, Token<'a>> = Token::lexer(text);
let mut lexer = PeekableLexer::new(lexer);
let tok = lexer.next();

match tok {
Some(Token::If) => StatementParser::parse_if(&mut lexer, &mut statementinfo_list),
Some(Token::If) => StatementParser::parse_if(&mut lexer, statementinfo_list),
Some(Token::Else) => {
StatementParser::parse_else(&mut statementinfo_list);
StatementParser::parse_else(statementinfo_list);
Ok(())
}
Some(Token::EndIf) => StatementParser::parse_endif(&mut lexer, &mut statementinfo_list),
Some(Token::ElIf) => StatementParser::parse_elif(&mut lexer, &mut statementinfo_list),
Some(Token::For) => StatementParser::parse_for(&mut lexer, &mut statementinfo_list),
Some(Token::EndFor) => {
StatementParser::parse_endfor(&mut lexer, &mut statementinfo_list)
}
Some(Token::With) => StatementParser::parse_with(&mut lexer, &mut statementinfo_list),
Some(Token::EndWith) => {
StatementParser::parse_endwith(&mut lexer, &mut statementinfo_list)
}
Some(Token::Include) => {
StatementParser::parse_include(&mut lexer, &mut statementinfo_list)
}
Some(Token::EndIf) => StatementParser::parse_endif(&mut lexer, statementinfo_list),
Some(Token::ElIf) => StatementParser::parse_elif(&mut lexer, statementinfo_list),
Some(Token::For) => StatementParser::parse_for(&mut lexer, statementinfo_list),
Some(Token::EndFor) => StatementParser::parse_endfor(&mut lexer, statementinfo_list),
Some(Token::With) => StatementParser::parse_with(&mut lexer, statementinfo_list),
Some(Token::EndWith) => StatementParser::parse_endwith(&mut lexer, statementinfo_list),
Some(Token::Include) => StatementParser::parse_include(&mut lexer, statementinfo_list),
Some(_) => {
let range = lexer.span();
Err(Error::from(ParseError::new(
Expand Down
16 changes: 3 additions & 13 deletions src/template_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ use crate::FileSystemHandler;
use crate::Template;
use std::sync::{Arc, RwLock};

#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
enum Jinja2CompatMode {
#[default]
None,
// Version2_10, // Fix in jinja2cpp
}
impl Default for Jinja2CompatMode {
fn default() -> Jinja2CompatMode {
Jinja2CompatMode::None
}
}

/// Global template environment settings
#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -49,18 +45,12 @@ impl Default for Settings {
}

/// Extensions set which should be supported
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Default)]
struct Extensions {
/// Enable use of `do` statement
do_ext: bool,
}

impl Default for Extensions {
fn default() -> Extensions {
Extensions { do_ext: false }
}
}

pub struct TemplateEnv<'a> {
settings: Settings,
global_values: Arc<RwLock<ValuesMap>>,
Expand Down
2 changes: 1 addition & 1 deletion src/template_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<'a> TemplateParser<'a> {
}

fn rough_parsing(&mut self) -> Result<()> {
let match_begin = self.rough_tokenizer.captures_iter(&self.template_body);
let match_begin = self.rough_tokenizer.captures_iter(self.template_body);

for capture in match_begin {
// This does not seem idiomatic to rust
Expand Down
9 changes: 2 additions & 7 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub enum Value {
#[default]
Empty,
Error,
Boolean(bool),
Expand Down Expand Up @@ -56,12 +57,6 @@ pub type ValuesMap = std::collections::BTreeMap<String, Value>;

pub type ValuesList = Vec<Value>;

impl Default for Value {
fn default() -> Value {
Value::Empty
}
}

mod filters;
mod from;
mod ops;
Expand Down
Loading

0 comments on commit 90ff56e

Please sign in to comment.