taitan_orm_parser/template_parser/structs/operators/
op.rs1use crate::template_parser::structs::operators::arithmetic::{ArithmeticOp, ArithmeticUnaryOp};
2use crate::template_parser::structs::operators::comparison_op::CompareOp;
3use crate::template_parser::structs::operators::connect::ConnectOp;
4use crate::template_parser::structs::operators::list_op::ListInOp;
5use crate::template_parser::structs::operators::logic_op::LogicOp;
6use crate::template_parser::structs::operators::paren::Paren;
7use nom::branch::alt;
8use nom::combinator::map;
9use nom::IResult;
10use std::fmt::Display;
11use crate::VariableChain;
12
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Operator {
16 Compare(CompareOp),
17 Arithmetic(ArithmeticOp),
18 ArithmeticUnary(ArithmeticUnaryOp), Logic(LogicOp),
20 ListInOp(ListInOp),
21 Paren(Paren),
22 Connect(ConnectOp),
23 FnCall(VariableChain)
24}
25
26impl Operator {
27 pub fn parse(input: &str) -> IResult<&str, Operator> {
28 alt((
29 map(CompareOp::parse, Operator::Compare),
30 map(ArithmeticOp::parse, Operator::Arithmetic),
31 map(LogicOp::parse, Operator::Logic),
32 map(ListInOp::parse, Operator::ListInOp),
33 map(Paren::parse, Operator::Paren),
34 map(ConnectOp::parse, Operator::Connect),
35 ))(input)
36 }
37 pub fn extract_and(&self) -> Option<Operator> {
38 if let Operator::Logic(logic_op) = self {
39 if logic_op.to_string() == "AND" {
40 return Some(Operator::Logic(LogicOp::And));
41 }
42 }
43 None
44 }
45 pub fn extract_or(&self) -> Option<Operator> {
46 if let Operator::Logic(logic_op) = self {
47 if logic_op.to_string() == "OR" {
48 return Some(Operator::Logic(LogicOp::And));
49 }
50 }
51 None
52 }
53}
54
55impl Display for Operator {
56 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 match self {
58 Operator::Arithmetic(a) => a.fmt(fmt),
59 Operator::Logic(l) => l.fmt(fmt),
60 Operator::Compare(c) => c.fmt(fmt),
61 Operator::ListInOp(l) => l.fmt(fmt),
62 Operator::Paren(p) => p.fmt(fmt),
63 Operator::Connect(c) => c.fmt(fmt),
64 Operator::FnCall(f) => f.fmt(fmt),
65 Operator::ArithmeticUnary(a) => a.fmt(fmt),
66 }
67 }
68}