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
17use serde::{Deserialize, Serialize};
18
19/// Built-in operators with exactly one argument
20#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, Hash)]
21#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
22pub enum UnaryOp {
23    /// Logical negation
24    ///
25    /// Argument must have Bool type
26    Not,
27    /// Integer negation
28    ///
29    /// Argument must have Long type
30    Neg,
31    /// isEmpty test for sets
32    ///
33    /// Argument must have Set type
34    IsEmpty,
35}
36
37impl std::fmt::Display for UnaryOp {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        match self {
40            UnaryOp::Not => write!(f, "!"),
41            UnaryOp::Neg => write!(f, "-"),
42            UnaryOp::IsEmpty => write!(f, "isEmpty"),
43        }
44    }
45}
46
47/// Built-in operators with exactly two arguments
48#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, Hash)]
49#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
50pub enum BinaryOp {
51    /// Equality
52    ///
53    /// Works on arguments of any type, ie "total equality". If you compare
54    /// things of different types, `Eq` will return `false`, rather than error.
55    Eq,
56
57    /// <
58    ///
59    /// Arguments must have Long type
60    Less,
61
62    /// <=
63    ///
64    /// Arguments must have Long type
65    LessEq,
66
67    /// Integer addition
68    ///
69    /// Arguments must have Long type
70    Add,
71
72    /// Integer subtraction
73    ///
74    /// Arguments must have Long type
75    Sub,
76
77    /// Integer multiplication
78    ///
79    /// Arguments must have Long type
80    Mul,
81
82    /// Hierarchy membership. Specifically, is the first arg a member of the
83    /// second.
84    ///
85    /// First argument must have Entity type.
86    /// Second argument must either have Entity type, or Set type where the
87    /// set elements all have Entity type. If it's a set, the semantics is
88    /// "is the first argument `in` any element of the given set"
89    In,
90
91    /// Set membership.
92    ///
93    /// First argument must have Set type.
94    Contains,
95
96    /// ContainsAll test for sets. Specifically, if the first set contains the second arg.
97    ///
98    /// Arguments must have Set type
99    ContainsAll,
100
101    /// ContainsAny test for sets (is the intersection empty?)
102    ///
103    /// Arguments must have Set type
104    ContainsAny,
105
106    /// Get a tag of an entity.
107    ///
108    /// First argument must have Entity type, second argument must have String type.
109    GetTag,
110
111    /// Does the given `expr` have the given `tag`?
112    ///
113    /// First argument must have Entity type, second argument must have String type.
114    HasTag,
115}
116
117impl std::fmt::Display for BinaryOp {
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        match self {
120            BinaryOp::Eq => write!(f, "=="),
121            BinaryOp::Less => write!(f, "<"),
122            BinaryOp::LessEq => write!(f, "<="),
123            BinaryOp::Add => write!(f, "+"),
124            BinaryOp::Sub => write!(f, "-"),
125            BinaryOp::Mul => write!(f, "*"),
126            BinaryOp::In => write!(f, "in"),
127            BinaryOp::Contains => write!(f, "contains"),
128            BinaryOp::ContainsAll => write!(f, "containsAll"),
129            BinaryOp::ContainsAny => write!(f, "containsAny"),
130            BinaryOp::GetTag => write!(f, "getTag"),
131            BinaryOp::HasTag => write!(f, "hasTag"),
132        }
133    }
134}