Struct cedar_policy_core::ast::Expr
source · pub struct Expr<T = ()> { /* private fields */ }
Expand description
Internal AST for expressions used by the policy evaluator.
This structure is a wrapper around an ExprKind
, which is the expression
variant this object contains. It also contains source information about
where the expression was written in policy source code, and some generic
data which is stored on each node of the AST.
Cloning is O(1).
Implementations§
source§impl<T> Expr<T>
impl<T> Expr<T>
sourcepub fn expr_kind(&self) -> &ExprKind<T>
pub fn expr_kind(&self) -> &ExprKind<T>
Access the inner ExprKind
for this Expr
. The ExprKind
is the
enum
which specifies the expression variant, so it must be accessed by
any code matching and recursing on an expression.
sourcepub fn into_expr_kind(self) -> ExprKind<T>
pub fn into_expr_kind(self) -> ExprKind<T>
Access the inner ExprKind
, taking ownership.
sourcepub fn source_info(&self) -> &Option<SourceInfo>
pub fn source_info(&self) -> &Option<SourceInfo>
Access the data stored on the Expr
.
sourcepub fn into_source_info(self) -> Option<SourceInfo>
pub fn into_source_info(self) -> Option<SourceInfo>
Access the data stored on the Expr
, taking ownership.
sourcepub fn set_data(&mut self, data: T)
pub fn set_data(&mut self, data: T)
Update the data for this Expr
. A convenient function used by the
Validator in one place.
sourcepub fn is_ref(&self) -> bool
pub fn is_ref(&self) -> bool
Check whether this expression is an entity reference
This is used for policy headers, where some syntax is required to be an entity reference.
sourcepub fn is_ref_set(&self) -> bool
pub fn is_ref_set(&self) -> bool
Check whether this expression is a set of entity references
This is used for policy headers, where some syntax is required to be an entity reference set.
sourcepub fn subexpressions(&self) -> impl Iterator<Item = &Self>
pub fn subexpressions(&self) -> impl Iterator<Item = &Self>
Iterate over all sub-expressions in this expression
sourcepub fn slots(&self) -> impl Iterator<Item = &SlotId>
pub fn slots(&self) -> impl Iterator<Item = &SlotId>
Iterate over all of the slots in this policy AST
sourcepub fn is_projectable(&self) -> bool
pub fn is_projectable(&self) -> bool
Determine if the expression is projectable under partial evaluation An expression is projectable if it’s guaranteed to never error on evaluation This is true if the expression is entirely composed of values or unknowns
source§impl Expr
impl Expr
sourcepub fn val(v: impl Into<Literal>) -> Self
pub fn val(v: impl Into<Literal>) -> Self
Create an Expr
that’s just a single Literal
.
Note that you can pass this a Literal
, an i64
, a String
, etc.
sourcepub fn unknown_with_type(name: impl Into<SmolStr>, t: Option<Type>) -> Self
pub fn unknown_with_type(name: impl Into<SmolStr>, t: Option<Type>) -> Self
Create an unknown value, with an optional type annotation
sourcepub fn ite(test_expr: Expr, then_expr: Expr, else_expr: Expr) -> Self
pub fn ite(test_expr: Expr, then_expr: Expr, else_expr: Expr) -> Self
Create a ternary (if-then-else) Expr
.
test_expr
must evaluate to a Bool type
sourcepub fn and(e1: Expr, e2: Expr) -> Self
pub fn and(e1: Expr, e2: Expr) -> Self
Create an ‘and’ expression. Arguments must evaluate to Bool type
sourcepub fn or(e1: Expr, e2: Expr) -> Self
pub fn or(e1: Expr, e2: Expr) -> Self
Create an ‘or’ expression. Arguments must evaluate to Bool type
sourcepub fn less(e1: Expr, e2: Expr) -> Self
pub fn less(e1: Expr, e2: Expr) -> Self
Create a ‘<’ expression. Arguments must evaluate to Long type
sourcepub fn lesseq(e1: Expr, e2: Expr) -> Self
pub fn lesseq(e1: Expr, e2: Expr) -> Self
Create a ‘<=’ expression. Arguments must evaluate to Long type
sourcepub fn greater(e1: Expr, e2: Expr) -> Self
pub fn greater(e1: Expr, e2: Expr) -> Self
Create a ‘>’ expression. Arguments must evaluate to Long type
sourcepub fn greatereq(e1: Expr, e2: Expr) -> Self
pub fn greatereq(e1: Expr, e2: Expr) -> Self
Create a ‘>=’ expression. Arguments must evaluate to Long type
sourcepub fn add(e1: Expr, e2: Expr) -> Self
pub fn add(e1: Expr, e2: Expr) -> Self
Create an ‘add’ expression. Arguments must evaluate to Long type
sourcepub fn sub(e1: Expr, e2: Expr) -> Self
pub fn sub(e1: Expr, e2: Expr) -> Self
Create a ‘sub’ expression. Arguments must evaluate to Long type
sourcepub fn mul(e1: Expr, e2: Expr) -> Self
pub fn mul(e1: Expr, e2: Expr) -> Self
Create a ‘mul’ expression. Arguments must evaluate to Long type
sourcepub fn is_in(e1: Expr, e2: Expr) -> Self
pub fn is_in(e1: Expr, e2: Expr) -> Self
Create an ‘in’ expression. First argument must evaluate to Entity type. Second argument must evaluate to either Entity type or Set type where all set elements have Entity type.
sourcepub fn contains(e1: Expr, e2: Expr) -> Self
pub fn contains(e1: Expr, e2: Expr) -> Self
Create a ‘contains’ expression. First argument must have Set type.
sourcepub fn contains_all(e1: Expr, e2: Expr) -> Self
pub fn contains_all(e1: Expr, e2: Expr) -> Self
Create a ‘contains_all’ expression. Arguments must evaluate to Set type
sourcepub fn contains_any(e1: Expr, e2: Expr) -> Self
pub fn contains_any(e1: Expr, e2: Expr) -> Self
Create an ‘contains_any’ expression. Arguments must evaluate to Set type
sourcepub fn set(exprs: impl IntoIterator<Item = Expr>) -> Self
pub fn set(exprs: impl IntoIterator<Item = Expr>) -> Self
Create an Expr
which evaluates to a Set of the given Expr
s
sourcepub fn record(pairs: impl IntoIterator<Item = (SmolStr, Expr)>) -> Self
pub fn record(pairs: impl IntoIterator<Item = (SmolStr, Expr)>) -> Self
Create an Expr
which evaluates to a Record with the given (key, value) pairs.
sourcepub fn call_extension_fn(fn_name: Name, args: Vec<Expr>) -> Self
pub fn call_extension_fn(fn_name: Name, args: Vec<Expr>) -> Self
Create an Expr
which calls the extension function with the given
Name
on args
sourcepub fn unary_app(op: impl Into<UnaryOp>, arg: Expr) -> Self
pub fn unary_app(op: impl Into<UnaryOp>, arg: Expr) -> Self
Create an application Expr
which applies the given built-in unary
operator to the given arg
sourcepub fn binary_app(op: impl Into<BinaryOp>, arg1: Expr, arg2: Expr) -> Self
pub fn binary_app(op: impl Into<BinaryOp>, arg1: Expr, arg2: Expr) -> Self
Create an application Expr
which applies the given built-in binary
operator to arg1
and arg2
sourcepub fn get_attr(expr: Expr, attr: SmolStr) -> Self
pub fn get_attr(expr: Expr, attr: SmolStr) -> Self
Create an Expr
which gets the attribute of some Entity
or the field
of some record.
expr
must evaluate to either Entity or Record type
sourcepub fn has_attr(expr: Expr, attr: SmolStr) -> Self
pub fn has_attr(expr: Expr, attr: SmolStr) -> Self
Create an Expr
which tests for the existence of a given
attribute on a given Entity
, or field on a given record.
expr
must evaluate to either Entity or Record type
sourcepub fn like(expr: Expr, pattern: impl IntoIterator<Item = PatternElem>) -> Self
pub fn like(expr: Expr, pattern: impl IntoIterator<Item = PatternElem>) -> Self
Create a ‘like’ expression.
expr
must evaluate to a String type
sourcepub fn is_unknown(&self) -> bool
pub fn is_unknown(&self) -> bool
Check if an expression contains any symbolic unknowns
sourcepub fn substitute(
&self,
definitions: &HashMap<SmolStr, Value>,
) -> Result<Expr, SubstitutionError>
pub fn substitute( &self, definitions: &HashMap<SmolStr, Value>, ) -> Result<Expr, SubstitutionError>
Substitute unknowns with values If a definition is missing, it will be left as an unknown, and can be filled in later.
source§impl<T> Expr<T>
impl<T> Expr<T>
sourcepub fn eq_shape<U>(&self, other: &Expr<U>) -> bool
pub fn eq_shape<U>(&self, other: &Expr<U>) -> bool
Return true if this expression (recursively) has the same expression
kind as the argument expression. This accounts for the full recursive
shape of the expression, but does not consider source information or any
generic data annotated on expression. This should behave the same as the
default implementation of Eq
before source information and generic
data were added.
sourcepub fn hash_shape<H>(&self, state: &mut H)where
H: Hasher,
pub fn hash_shape<H>(&self, state: &mut H)where
H: Hasher,
Implementation of hashing corresponding to equality as implemented by
eq_shape
. Must satisfy the usual relationship between equality and
hashing.
Trait Implementations§
source§impl<'a> AsRef<Expr> for BorrowedRestrictedExpr<'a>
impl<'a> AsRef<Expr> for BorrowedRestrictedExpr<'a>
source§impl AsRef<Expr> for RestrictedExpr
impl AsRef<Expr> for RestrictedExpr
source§impl<'de, T> Deserialize<'de> for Expr<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Expr<T>where
T: Deserialize<'de>,
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<'a> From<BorrowedRestrictedExpr<'a>> for &'a Expr
impl<'a> From<BorrowedRestrictedExpr<'a>> for &'a Expr
source§fn from(r: BorrowedRestrictedExpr<'a>) -> &'a Expr
fn from(r: BorrowedRestrictedExpr<'a>) -> &'a Expr
source§impl From<Expr> for PartialValue
impl From<Expr> for PartialValue
source§impl From<ExtensionValueWithArgs> for Expr
impl From<ExtensionValueWithArgs> for Expr
source§fn from(val: ExtensionValueWithArgs) -> Self
fn from(val: ExtensionValueWithArgs) -> Self
source§impl From<PartialValue> for Expr
impl From<PartialValue> for Expr
source§fn from(val: PartialValue) -> Self
fn from(val: PartialValue) -> Self
source§impl From<RestrictedExpr> for Expr
impl From<RestrictedExpr> for Expr
source§fn from(r: RestrictedExpr) -> Expr
fn from(r: RestrictedExpr) -> Expr
impl<T: Eq> Eq for Expr<T>
impl<T> StructuralPartialEq for Expr<T>
Auto Trait Implementations§
impl<T> Freeze for Expr<T>where
T: Freeze,
impl<T> RefUnwindSafe for Expr<T>where
T: RefUnwindSafe,
impl<T> Send for Expr<T>
impl<T> Sync for Expr<T>
impl<T> Unpin for Expr<T>where
T: Unpin,
impl<T> UnwindSafe for Expr<T>where
T: UnwindSafe + RefUnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more