cedar_policy_core/ast/
ops.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/// Built-in operators with exactly one argument
18#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
19#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
20pub enum UnaryOp {
21    /// Logical negation
22    ///
23    /// Argument must have Bool type
24    Not,
25    /// Integer negation
26    ///
27    /// Argument must have Long type
28    Neg,
29    /// isEmpty test for sets
30    ///
31    /// Argument must have Set type
32    IsEmpty,
33}
34
35impl std::fmt::Display for UnaryOp {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            UnaryOp::Not => write!(f, "!"),
39            UnaryOp::Neg => write!(f, "-"),
40            UnaryOp::IsEmpty => write!(f, "isEmpty"),
41        }
42    }
43}
44
45/// Built-in operators with exactly two arguments
46#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
47#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
48pub enum BinaryOp {
49    /// Equality
50    ///
51    /// Works on arguments of any type, ie "total equality". If you compare
52    /// things of different types, `Eq` will return `false`, rather than error.
53    Eq,
54
55    /// <
56    ///
57    /// Arguments must have Long type
58    Less,
59
60    /// <=
61    ///
62    /// Arguments must have Long type
63    LessEq,
64
65    /// Integer addition
66    ///
67    /// Arguments must have Long type
68    Add,
69
70    /// Integer subtraction
71    ///
72    /// Arguments must have Long type
73    Sub,
74
75    /// Integer multiplication
76    ///
77    /// Arguments must have Long type
78    Mul,
79
80    /// Hierarchy membership. Specifically, is the first arg a member of the
81    /// second.
82    ///
83    /// First argument must have Entity type.
84    /// Second argument must either have Entity type, or Set type where the
85    /// set elements all have Entity type. If it's a set, the semantics is
86    /// "is the first argument `in` any element of the given set"
87    In,
88
89    /// Set membership.
90    ///
91    /// First argument must have Set type.
92    Contains,
93
94    /// ContainsAll test for sets. Specifically, if the first set contains the second arg.
95    ///
96    /// Arguments must have Set type
97    ContainsAll,
98
99    /// ContainsAny test for sets (is the intersection empty?)
100    ///
101    /// Arguments must have Set type
102    ContainsAny,
103
104    /// Get a tag of an entity.
105    ///
106    /// First argument must have Entity type, second argument must have String type.
107    GetTag,
108
109    /// Does the given `expr` have the given `tag`?
110    ///
111    /// First argument must have Entity type, second argument must have String type.
112    HasTag,
113}
114
115impl std::fmt::Display for BinaryOp {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        match self {
118            BinaryOp::Eq => write!(f, "=="),
119            BinaryOp::Less => write!(f, "<"),
120            BinaryOp::LessEq => write!(f, "<="),
121            BinaryOp::Add => write!(f, "+"),
122            BinaryOp::Sub => write!(f, "-"),
123            BinaryOp::Mul => write!(f, "*"),
124            BinaryOp::In => write!(f, "in"),
125            BinaryOp::Contains => write!(f, "contains"),
126            BinaryOp::ContainsAll => write!(f, "containsAll"),
127            BinaryOp::ContainsAny => write!(f, "containsAny"),
128            BinaryOp::GetTag => write!(f, "getTag"),
129            BinaryOp::HasTag => write!(f, "hasTag"),
130        }
131    }
132}