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