1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2018-2020, Wayfair GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Don't cover this file it's only simple helpers
#![cfg_attr(tarpaulin, skip)]

use super::{
    BinOpKind, EventPath, Invoke, InvokeAggr, InvokeAggrFn, LocalPath, MetadataPath, Segment,
    StatePath, UnaryOpKind,
};
use std::fmt;

impl<'script> fmt::Debug for InvokeAggrFn<'script> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "fn(aggr) {}::{}", self.module, self.fun)
    }
}

impl<'script> PartialEq for InvokeAggrFn<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.module == other.module && self.fun == other.fun && self.args == other.args
    }
}

impl<'script> PartialEq for Segment<'script> {
    fn eq(&self, other: &Self) -> bool {
        use Segment::{Element, Id, Idx, Range};
        match (self, other) {
            (Id { mid: id1, .. }, Id { mid: id2, .. }) => id1 == id2,
            (Idx { idx: idx1, .. }, Idx { idx: idx2, .. }) => idx1 == idx2,
            (Element { expr: expr1, .. }, Element { expr: expr2, .. }) => expr1 == expr2,
            (
                Range {
                    range_start: start1,
                    range_end: end1,
                    ..
                },
                Range {
                    range_start: start2,
                    range_end: end2,
                    ..
                },
            ) => start1 == start2 && end1 == end2,
            _ => false,
        }
    }
}
impl<'script> PartialEq for LocalPath<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.idx == other.idx && self.is_const == other.is_const && self.segments == other.segments
    }
}

impl<'script> PartialEq for MetadataPath<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.segments == other.segments
    }
}

impl<'script> PartialEq for EventPath<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.segments == other.segments
    }
}

impl<'script> PartialEq for StatePath<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.segments == other.segments
    }
}

impl BinOpKind {
    fn operator_name(self) -> &'static str {
        match self {
            Self::Or => "or",
            Self::Xor => "xor",
            Self::And => "and",
            Self::BitOr => "|",
            Self::BitXor => "^",
            Self::BitAnd => "&",
            Self::Eq => "==",
            Self::NotEq => "!=",
            Self::Gte => ">=",
            Self::Gt => ">",
            Self::Lte => "<=",
            Self::Lt => "<",
            Self::RBitShiftSigned => ">>",
            Self::RBitShiftUnsigned => ">>>",
            Self::LBitShift => "<<",
            Self::Add => "+",
            Self::Sub => "-",
            Self::Mul => "*",
            Self::Div => "/",
            Self::Mod => "%",
        }
    }
}

impl fmt::Display for BinOpKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.operator_name())
    }
}

impl UnaryOpKind {
    fn operator_name(self) -> &'static str {
        match self {
            Self::Plus => "+",
            Self::Minus => "-",
            Self::Not => "not",
            Self::BitNot => "!",
        }
    }
}

impl fmt::Display for UnaryOpKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.operator_name())
    }
}

impl<'script> PartialEq for Invoke<'script> {
    fn eq(&self, other: &Self) -> bool {
        self.mid == other.mid && self.module == other.module && self.fun == other.fun
    }
}

impl<'script> fmt::Debug for Invoke<'script> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "fn {}::{}", self.module.join("::"), self.fun)
    }
}

impl PartialEq for InvokeAggr {
    fn eq(&self, other: &Self) -> bool {
        self.mid == other.mid
            && self.module == other.module
            && self.fun == other.fun
            && self.aggr_id == other.aggr_id
    }
}

impl fmt::Debug for InvokeAggr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "fn(aggr) {}::{}", self.module, self.fun)
    }
}