Skip to content

Commit

Permalink
chore: fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
morenol committed Feb 9, 2024
1 parent 364ff2d commit 5803c98
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 54 deletions.
16 changes: 8 additions & 8 deletions src/expression_evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ impl<'a> Evaluate for FilteredExpression<'a> {

pub enum Expression<'a> {
Constant(Value),
BinaryExpression(BinaryOperation, Box<Expression<'a>>, Box<Expression<'a>>),
UnaryExpression(UnaryOperation, Box<Expression<'a>>),
SubscriptExpression(SubscriptExpression<'a>),
Binary(BinaryOperation, Box<Expression<'a>>, Box<Expression<'a>>),
Unary(UnaryOperation, Box<Expression<'a>>),
Subscript(SubscriptExpression<'a>),
ValueRef(ValueRefExpression),
FilteredExpression(FilteredExpression<'a>),
Filtered(FilteredExpression<'a>),
Tuple(TupleExpression<'a>),
Dict(DictionaryExpression<'a>),
}
Expand Down Expand Up @@ -151,24 +151,24 @@ impl<'a> Evaluate for Expression<'a> {
fn evaluate(&self, values: Context<'_>) -> Result<Value> {
let result = match &self {
Expression::Constant(value) => value.clone(),
Expression::BinaryExpression(op, left, right) => {
Expression::Binary(op, left, right) => {
let left_val = left.evaluate(values.clone())?;
let right_val = right.evaluate(values)?;
visitors::BinaryMathOperation::apply(op, left_val, right_val)
}
Expression::UnaryExpression(op, expr) => {
Expression::Unary(op, expr) => {
let expression = expr.evaluate(values)?;
match op {
UnaryOperation::Plus => expression,
UnaryOperation::Minus => -expression,
UnaryOperation::LogicalNot => !expression,
}
}
Expression::SubscriptExpression(sub) => sub.evaluate(values)?,
Expression::Subscript(sub) => sub.evaluate(values)?,
Expression::ValueRef(identifier) => identifier.evaluate(values)?,
Expression::Tuple(tuple) => tuple.evaluate(values)?,
Expression::Dict(dict) => dict.evaluate(values)?,
Expression::FilteredExpression(filter) => filter.evaluate(values)?,
Expression::Filtered(filter) => filter.evaluate(values)?,
};
Ok(result)
}
Expand Down
20 changes: 10 additions & 10 deletions src/expression_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ExpressionParser {
if let Some(Ok(Token::LogicalOr)) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_or(lexer)?;
return Ok(Expression::BinaryExpression(
return Ok(Expression::Binary(
BinaryOperation::LogicalOr,
Box::new(left),
Box::new(right),
Expand All @@ -60,7 +60,7 @@ impl ExpressionParser {
if let Some(Ok(Token::LogicalAnd)) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_and(lexer)?;
return Ok(Expression::BinaryExpression(
return Ok(Expression::Binary(
BinaryOperation::LogicalAnd,
Box::new(left),
Box::new(right),
Expand All @@ -87,7 +87,7 @@ impl ExpressionParser {
lexer.next();
let right = ExpressionParser::parse_string_concat(lexer)?;

Ok(Expression::BinaryExpression(
Ok(Expression::Binary(
binary_op,
Box::new(left),
Box::new(right),
Expand All @@ -99,7 +99,7 @@ impl ExpressionParser {
if let Some(Ok(Token::Tilde)) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_logical_and(lexer)?;
return Ok(Expression::BinaryExpression(
return Ok(Expression::Binary(
BinaryOperation::StringConcat,
Box::new(left),
Box::new(right),
Expand All @@ -113,7 +113,7 @@ impl ExpressionParser {
if let Some(Ok(Token::MulMul)) = lexer.peek() {
lexer.next();
let right = ExpressionParser::parse_math_pow(lexer)?;
return Ok(Expression::BinaryExpression(
return Ok(Expression::Binary(
BinaryOperation::Pow,
Box::new(left),
Box::new(right),
Expand All @@ -133,7 +133,7 @@ impl ExpressionParser {
};
lexer.next();
let right = ExpressionParser::parse_math_plus_minus(lexer)?;
Ok(Expression::BinaryExpression(
Ok(Expression::Binary(
binary_op,
Box::new(left),
Box::new(right),
Expand All @@ -152,7 +152,7 @@ impl ExpressionParser {
lexer.next();
let right = ExpressionParser::parse_math_mul_div(lexer)?;

Ok(Expression::BinaryExpression(
Ok(Expression::Binary(
binary_op,
Box::new(left),
Box::new(right),
Expand All @@ -175,14 +175,14 @@ impl ExpressionParser {
let sub_expr = ExpressionParser::parse_value_expression(lexer)?;

let result = match unary_op {
Some(op) => Expression::UnaryExpression(op, Box::new(sub_expr)),
Some(op) => Expression::Unary(op, Box::new(sub_expr)),
None => sub_expr,
};

if let Some(Ok(Token::Pipe)) = lexer.peek() {
lexer.next();
let filter_expression = ExpressionParser::parse_filter_expression(lexer)?;
Ok(Expression::FilteredExpression(FilteredExpression::new(
Ok(Expression::Filtered(FilteredExpression::new(
Box::new(result),
filter_expression,
)))
Expand Down Expand Up @@ -398,7 +398,7 @@ impl ExpressionParser {
};
}

Ok(Expression::SubscriptExpression(subscript))
Ok(Expression::Subscript(subscript))
}
fn parse_tuple<'a>(lexer: &mut PeekableLexer<'a, Token<'a>>) -> Result<Expression<'a>> {
let mut tuple = TupleExpression::default();
Expand Down
30 changes: 16 additions & 14 deletions src/statement/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use std::io::Write;
use std::rc::Rc;

use crate::context::Context;
use crate::error::Result;
use crate::expression_evaluator::Evaluate;
use crate::lexer::Token;
use crate::renderer::ComposedRenderer;
use crate::renderer::Render;
use crate::value::{Value, ValuesList, ValuesMap};
use std::io::Write;
use std::sync::Arc;

pub mod parser;
pub struct IfStatement<'a> {
expression: Box<dyn Evaluate + 'a>,
body: Option<Arc<ComposedRenderer<'a>>>,
body: Option<Rc<ComposedRenderer<'a>>>,
else_branches: Vec<Statement<'a>>,
}
impl<'a> IfStatement<'a> {
Expand All @@ -21,7 +23,7 @@ impl<'a> IfStatement<'a> {
else_branches: vec![],
}
}
fn set_main_body(&mut self, body: Arc<ComposedRenderer<'a>>) {
fn set_main_body(&mut self, body: Rc<ComposedRenderer<'a>>) {
let if_body = body.clone();
self.body = Some(if_body);
}
Expand Down Expand Up @@ -52,7 +54,7 @@ impl<'a> Render for IfStatement<'a> {

pub struct ElseStatement<'a> {
expression: Option<Box<dyn Evaluate + 'a>>,
body: Option<Arc<ComposedRenderer<'a>>>,
body: Option<Rc<ComposedRenderer<'a>>>,
}

impl<'a> ElseStatement<'a> {
Expand All @@ -62,7 +64,7 @@ impl<'a> ElseStatement<'a> {
body: None,
}
}
fn set_main_body(&mut self, body: Arc<ComposedRenderer<'a>>) {
fn set_main_body(&mut self, body: Rc<ComposedRenderer<'a>>) {
let else_body = body.clone();
self.body = Some(else_body);
}
Expand All @@ -82,7 +84,7 @@ impl<'a> Render for ElseStatement<'a> {
}
pub struct WithStatement<'a> {
scope_vars: Vec<(String, Box<dyn Evaluate + 'a>)>,
body: Option<Arc<ComposedRenderer<'a>>>,
body: Option<Rc<ComposedRenderer<'a>>>,
}
impl<'a> WithStatement<'a> {
pub fn new(scope_vars: Vec<(String, Box<dyn Evaluate + 'a>)>) -> Self {
Expand All @@ -91,7 +93,7 @@ impl<'a> WithStatement<'a> {
body: None,
}
}
fn set_main_body(&mut self, body: Arc<ComposedRenderer<'a>>) {
fn set_main_body(&mut self, body: Rc<ComposedRenderer<'a>>) {
let with_body = body.clone();
self.body = Some(with_body);
}
Expand All @@ -111,7 +113,7 @@ impl<'a> Render for WithStatement<'a> {
pub struct ForStatement<'a> {
vars: Vec<String>,
value: Box<dyn Evaluate + 'a>,
body: Option<Arc<ComposedRenderer<'a>>>,
body: Option<Rc<ComposedRenderer<'a>>>,
}

impl<'a> ForStatement<'a> {
Expand All @@ -122,7 +124,7 @@ impl<'a> ForStatement<'a> {
body: None,
}
}
fn set_main_body(&mut self, body: Arc<ComposedRenderer<'a>>) {
fn set_main_body(&mut self, body: Rc<ComposedRenderer<'a>>) {
let for_body = body.clone();
self.body = Some(for_body);
}
Expand Down Expand Up @@ -224,7 +226,7 @@ pub enum Statement<'a> {
Include(IncludeStatement<'a>),
}
impl<'a> Statement<'a> {
pub fn set_main_body(&mut self, body: Arc<ComposedRenderer<'a>>) {
pub fn set_main_body(&mut self, body: Rc<ComposedRenderer<'a>>) {
match self {
Statement::If(statement) => statement.set_main_body(body),
Statement::Else(statement) => statement.set_main_body(body),
Expand Down Expand Up @@ -255,8 +257,8 @@ impl<'a> Render for Statement<'a> {

pub struct StatementInfo<'a> {
mode: StatementInfoType,
pub current_composition: Arc<ComposedRenderer<'a>>,
compositions: Vec<Arc<ComposedRenderer<'a>>>,
pub current_composition: Rc<ComposedRenderer<'a>>,
compositions: Vec<Rc<ComposedRenderer<'a>>>,
_token: Option<Token<'a>>,
renderer: Option<Statement<'a>>,
}
Expand All @@ -273,7 +275,7 @@ impl<'a> StatementInfo<'a> {
pub fn new(
mode: StatementInfoType,
_token: Option<Token<'a>>,
renderers: Arc<ComposedRenderer<'a>>,
renderers: Rc<ComposedRenderer<'a>>,
) -> Self {
let current_composition = renderers.clone();
let compositions = vec![renderers];
Expand Down
26 changes: 15 additions & 11 deletions src/statement/parser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use super::{
ElseStatement, ForStatement, IfStatement, IncludeStatement, Statement, StatementInfo,
StatementInfoList, StatementInfoType, WithStatement,
};
use std::rc::Rc;

use logos::{Lexer, Logos};

use crate::error::{Error, ParseError, ParseErrorKind, Result};
use crate::expression_parser::ExpressionParser;
use crate::lexer::{PeekableLexer, Token};
use crate::renderer::ComposedRenderer;
use crate::source::SourceLocationInfo;
use crate::statement::Evaluate;
use logos::{Lexer, Logos};

use super::{
ElseStatement, ForStatement, IfStatement, IncludeStatement, Statement, StatementInfo,
StatementInfoList, StatementInfoType, WithStatement,
};

pub struct StatementParser;
use std::sync::Arc;

impl StatementParser {
pub fn parse<'a>(text: &'a str, statementinfo_list: &mut StatementInfoList<'a>) -> Result<()> {
Expand Down Expand Up @@ -52,7 +56,7 @@ impl StatementParser {
statementinfo_list: &mut StatementInfoList<'a>,
) -> Result<()> {
let value = ExpressionParser::full_expresion_parser(lexer)?;
let composed_renderer = Arc::new(ComposedRenderer::new());
let composed_renderer = Rc::new(ComposedRenderer::new());
let renderer = Statement::If(IfStatement::new(Box::new(value)));
let mut statement_info = StatementInfo::new(
StatementInfoType::IfStatement,
Expand All @@ -69,7 +73,7 @@ impl StatementParser {
statementinfo_list: &mut StatementInfoList<'a>,
) -> Result<()> {
let value = ExpressionParser::full_expresion_parser(lexer)?;
let composed_renderer = Arc::new(ComposedRenderer::new());
let composed_renderer = Rc::new(ComposedRenderer::new());
let renderer = Statement::Else(ElseStatement::new(Some(Box::new(value))));
let mut statement_info = StatementInfo::new(
StatementInfoType::ElseIfStatement,
Expand All @@ -82,7 +86,7 @@ impl StatementParser {
}

fn parse_else(statementinfo_list: &mut StatementInfoList<'_>) {
let composed_renderer = Arc::new(ComposedRenderer::new());
let composed_renderer = Rc::new(ComposedRenderer::new());
let renderer = Statement::Else(ElseStatement::new(None));
let mut statement_info = StatementInfo::new(
StatementInfoType::ElseIfStatement,
Expand Down Expand Up @@ -161,7 +165,7 @@ impl StatementParser {
Some(SourceLocationInfo::new_with_range(range.start, range.end)),
)))
} else {
let composed_renderer = Arc::new(ComposedRenderer::new());
let composed_renderer = Rc::new(ComposedRenderer::new());
let renderer = Statement::For(ForStatement::new(vars, Box::new(expression)));
let mut statement_info = StatementInfo::new(
StatementInfoType::ForStatement,
Expand Down Expand Up @@ -244,7 +248,7 @@ impl StatementParser {
Some(SourceLocationInfo::new_with_range(range.start, range.end)),
)));
}
let composed_renderer = Arc::new(ComposedRenderer::new());
let composed_renderer = Rc::new(ComposedRenderer::new());
let renderer = Statement::With(WithStatement::new(vars));
let mut statement_info = StatementInfo::new(
StatementInfoType::WithStatement,
Expand Down
10 changes: 6 additions & 4 deletions src/template_parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::rc::Rc;
use std::sync::RwLock;

use crate::error::{Error, ParseError, ParseErrorKind, Result};
use crate::expression_parser::ExpressionParser;
use crate::keyword::{RegexEnum, ROUGH_TOKENIZER};
Expand All @@ -8,7 +11,6 @@ use crate::statement::parser::StatementParser;
use crate::statement::{StatementInfo, StatementInfoList, StatementInfoType};
use crate::template_env::TemplateEnv;
use regex::Regex;
use std::sync::{Arc, RwLock};

pub struct TemplateParser<'a> {
template_body: &'a str,
Expand Down Expand Up @@ -70,7 +72,7 @@ impl<'a> TemplateParser<'a> {
statement
}
}
fn fine_parsing(&self, renderer: Arc<ComposedRenderer<'a>>) -> Result<()> {
fn fine_parsing(&self, renderer: Rc<ComposedRenderer<'a>>) -> Result<()> {
let mut statements_stack: StatementInfoList<'_> = vec![];
let root = StatementInfo::new(StatementInfoType::TemplateRoot, None, renderer);
statements_stack.push(root);
Expand Down Expand Up @@ -109,9 +111,9 @@ impl<'a> TemplateParser<'a> {
pub fn parse(&mut self) -> Result<ComposedRenderer<'a>> {
match self.rough_parsing() {
Ok(_) => {
let renderer = Arc::new(ComposedRenderer::new());
let renderer = Rc::new(ComposedRenderer::new());
self.fine_parsing(renderer.clone())?;
Ok(Arc::try_unwrap(renderer).unwrap())
Ok(Rc::try_unwrap(renderer).unwrap())
}
Err(error) => Err(error),
}
Expand Down
8 changes: 1 addition & 7 deletions src/value/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,7 @@ impl Value {
}
impl PartialOrd for Value {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
match (self, other) {
(Value::Integer(left), Value::Integer(right)) => left.partial_cmp(right),
(Value::Integer(left), Value::Double(right)) => (*left as f64).partial_cmp(right),
(Value::Double(left), Value::Integer(right)) => left.partial_cmp(&(*right as f64)),
(Value::Double(left), Value::Double(right)) => left.partial_cmp(right),
_ => todo!(),
}
Some(self.cmp(other))
}
}

Expand Down

0 comments on commit 5803c98

Please sign in to comment.