taitan_orm_parser/template_parser/structs/operators/
arithmetic.rs

1use std::fmt::Display;
2use nom::branch::alt;
3use nom::bytes::complete::tag;
4use nom::character::complete::multispace0;
5use nom::combinator::map;
6use nom::IResult;
7use nom::sequence::preceded;
8
9
10#[derive(Debug, Clone, PartialEq, Eq, Copy)]
11pub enum ArithmeticUnaryOp {
12    Add,
13    Sub,
14}
15
16impl Display for ArithmeticUnaryOp {
17    fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Add => write!(_f, "+"),
20            Self::Sub => write!(_f, "-"),
21        }
22    }
23}
24
25
26
27#[derive(Debug, Clone, PartialEq, Eq, Copy)]
28pub enum ArithmeticOp {
29    Add,
30    Sub,
31    Mul,
32    Div,
33    Mod,
34}
35
36impl ArithmeticOp {
37    pub fn parse(input: &str) -> IResult<&str, ArithmeticOp> {
38        alt((
39            // map(preceded(multispace0, tag("+")), |s: &str| ArithmeticOp::Add),
40            // map(preceded(multispace0, tag("-")), |s: &str| ArithmeticOp::Sub),
41            map(preceded(multispace0, tag("*")), |s: &str| ArithmeticOp::Mul),
42            map(preceded(multispace0, tag("/")), |s: &str| ArithmeticOp::Div),
43            map(preceded(multispace0, tag("%")), |s: &str| ArithmeticOp::Mod),
44        ))(input)
45    }
46}
47
48impl Display for ArithmeticOp {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        match self {
51            ArithmeticOp::Add => f.write_str("+"),
52            ArithmeticOp::Sub => f.write_str("-"),
53            ArithmeticOp::Mul => f.write_str("*"),
54            ArithmeticOp::Div => f.write_str("/"),
55            ArithmeticOp::Mod => f.write_str("%"),
56        }
57    }
58}