|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use alloc::boxed::Box; |
| 5 | +use alloc::string::String; |
| 6 | +use alloc::vec::Vec; |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +use super::literals::{ |
| 10 | + BooleanLiteral, DateTimeLiteral, NullLiteral, NumberLiteral, StringLiteral, TimeLiteral, |
| 11 | +}; |
| 12 | +use super::operators::{ArrayOperator, ConditionOperator}; |
| 13 | +use super::references::AttributeReference; |
| 14 | +use super::span::EmptySpan; |
| 15 | + |
| 16 | +/// ABAC condition expression |
| 17 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 18 | +pub struct ConditionExpression { |
| 19 | + #[serde(skip)] |
| 20 | + pub span: EmptySpan, |
| 21 | + pub raw_expression: String, |
| 22 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 23 | + pub expression: Option<ConditionExpr>, |
| 24 | +} |
| 25 | + |
| 26 | +impl ConditionExpression { |
| 27 | + pub fn new(span: EmptySpan, expression: String) -> Self { |
| 28 | + Self { |
| 29 | + span, |
| 30 | + raw_expression: expression, |
| 31 | + expression: None, |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn with_parsed(span: EmptySpan, raw_expression: String, parsed: ConditionExpr) -> Self { |
| 36 | + Self { |
| 37 | + span, |
| 38 | + raw_expression, |
| 39 | + expression: Some(parsed), |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Condition expression node |
| 45 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 46 | +#[serde(tag = "type")] |
| 47 | +pub enum ConditionExpr { |
| 48 | + Logical(LogicalExpression), |
| 49 | + Unary(UnaryExpression), |
| 50 | + Binary(BinaryExpression), |
| 51 | + FunctionCall(FunctionCallExpression), |
| 52 | + AttributeReference(AttributeReference), |
| 53 | + ArrayExpression(ArrayExpression), |
| 54 | + Identifier(IdentifierExpression), |
| 55 | + VariableReference(VariableReference), |
| 56 | + PropertyAccess(PropertyAccessExpression), |
| 57 | + StringLiteral(StringLiteral), |
| 58 | + NumberLiteral(NumberLiteral), |
| 59 | + BooleanLiteral(BooleanLiteral), |
| 60 | + NullLiteral(NullLiteral), |
| 61 | + DateTimeLiteral(DateTimeLiteral), |
| 62 | + TimeLiteral(TimeLiteral), |
| 63 | + SetLiteral(SetLiteral), |
| 64 | + ListLiteral(ListLiteral), |
| 65 | +} |
| 66 | + |
| 67 | +/// Logical (AND/OR) expression |
| 68 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 69 | +pub struct LogicalExpression { |
| 70 | + #[serde(skip)] |
| 71 | + pub span: EmptySpan, |
| 72 | + pub operator: LogicalOperator, |
| 73 | + pub left: Box<ConditionExpr>, |
| 74 | + pub right: Box<ConditionExpr>, |
| 75 | +} |
| 76 | + |
| 77 | +/// Logical operator kinds |
| 78 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 79 | +pub enum LogicalOperator { |
| 80 | + And, |
| 81 | + Or, |
| 82 | +} |
| 83 | + |
| 84 | +/// Unary expression (e.g., NOT) |
| 85 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 86 | +pub struct UnaryExpression { |
| 87 | + #[serde(skip)] |
| 88 | + pub span: EmptySpan, |
| 89 | + pub operator: UnaryOperator, |
| 90 | + pub operand: Box<ConditionExpr>, |
| 91 | +} |
| 92 | + |
| 93 | +/// Unary operator kinds |
| 94 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 95 | +pub enum UnaryOperator { |
| 96 | + Not, |
| 97 | + Exists, |
| 98 | + NotExists, |
| 99 | +} |
| 100 | + |
| 101 | +/// Binary expression with an operator and two operands |
| 102 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 103 | +pub struct BinaryExpression { |
| 104 | + #[serde(skip)] |
| 105 | + pub span: EmptySpan, |
| 106 | + pub operator: ConditionOperator, |
| 107 | + pub left: Box<ConditionExpr>, |
| 108 | + pub right: Box<ConditionExpr>, |
| 109 | +} |
| 110 | + |
| 111 | +/// Function call expression (e.g. ToLower(expr)) |
| 112 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 113 | +pub struct FunctionCallExpression { |
| 114 | + #[serde(skip)] |
| 115 | + pub span: EmptySpan, |
| 116 | + pub function: String, |
| 117 | + pub arguments: Vec<ConditionExpr>, |
| 118 | +} |
| 119 | + |
| 120 | +/// Array expression with quantifiers (e.g. ANY tag : ...) |
| 121 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 122 | +pub struct ArrayExpression { |
| 123 | + #[serde(skip)] |
| 124 | + pub span: EmptySpan, |
| 125 | + pub operator: ArrayOperator, |
| 126 | + pub array: Box<ConditionExpr>, |
| 127 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 128 | + pub variable: Option<String>, |
| 129 | + pub condition: Box<ConditionExpr>, |
| 130 | +} |
| 131 | + |
| 132 | +/// Set literal value (e.g. {'a', 'b'}) |
| 133 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 134 | +pub struct SetLiteral { |
| 135 | + #[serde(skip)] |
| 136 | + pub span: EmptySpan, |
| 137 | + pub elements: Vec<ConditionExpr>, |
| 138 | +} |
| 139 | + |
| 140 | +/// List literal value (e.g. ['start', 'end']) |
| 141 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 142 | +pub struct ListLiteral { |
| 143 | + #[serde(skip)] |
| 144 | + pub span: EmptySpan, |
| 145 | + pub elements: Vec<ConditionExpr>, |
| 146 | +} |
| 147 | + |
| 148 | +/// Identifier expression (unqualified name) |
| 149 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 150 | +pub struct IdentifierExpression { |
| 151 | + #[serde(skip)] |
| 152 | + pub span: EmptySpan, |
| 153 | + pub name: String, |
| 154 | +} |
| 155 | + |
| 156 | +/// Variable reference (e.g. loop variable in ANY clauses) |
| 157 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 158 | +pub struct VariableReference { |
| 159 | + #[serde(skip)] |
| 160 | + pub span: EmptySpan, |
| 161 | + pub name: String, |
| 162 | +} |
| 163 | + |
| 164 | +/// Property access expression (e.g. tag.key) |
| 165 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 166 | +pub struct PropertyAccessExpression { |
| 167 | + #[serde(skip)] |
| 168 | + pub span: EmptySpan, |
| 169 | + pub object: Box<ConditionExpr>, |
| 170 | + pub property: String, |
| 171 | +} |
0 commit comments