cedar_policy_core::ast

Struct 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>

Source

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.

Source

pub fn into_expr_kind(self) -> ExprKind<T>

Access the inner ExprKind, taking ownership and consuming the Expr.

Source

pub fn data(&self) -> &T

Access the data stored on the Expr.

Source

pub fn into_data(self) -> T

Access the data stored on the Expr, taking ownership and consuming the Expr.

Source

pub fn source_loc(&self) -> Option<&Loc>

Access the Loc stored on the Expr.

Source

pub fn with_maybe_source_loc(self, source_loc: Option<Loc>) -> Self

Return the Expr, but with the new source_loc (or None).

Source

pub fn set_data(&mut self, data: T)

Update the data for this Expr. A convenient function used by the Validator in one place.

Source

pub fn is_ref(&self) -> bool

Check whether this expression is an entity reference

This is used for policy scopes, where some syntax is required to be an entity reference.

Source

pub fn is_slot(&self) -> bool

Check whether this expression is a slot.

Source

pub fn is_ref_set(&self) -> bool

Check whether this expression is a set of entity references

This is used for policy scopes, where some syntax is required to be an entity reference set.

Source

pub fn subexpressions(&self) -> impl Iterator<Item = &Self>

Iterate over all sub-expressions in this expression

Source

pub fn slots(&self) -> impl Iterator<Item = Slot> + '_

Iterate over all of the slots in this policy AST

Source

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

pub fn try_type_of(&self, extensions: &Extensions<'_>) -> Option<Type>

Try to compute the runtime type of this expression. This operation may fail (returning None), for example, when asked to get the type of any variables, any attributes of entities or records, or an unknown without an explicitly annotated type.

Also note that this is not typechecking the expression. It does not check that the expression actually evaluates to a value (as opposed to erroring).

Because of these limitations, this function should only be used to obtain a type for use in diagnostics such as error strings.

Source§

impl Expr

Source

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 Integer, a String, etc.

Source

pub fn unknown(u: Unknown) -> Self

Create an Expr that’s just a single Unknown.

Source

pub fn var(v: Var) -> Self

Create an Expr that’s just this literal Var

Source

pub fn slot(s: SlotId) -> Self

Create an Expr that’s just this SlotId

Source

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

Source

pub fn ite_arc( test_expr: Arc<Expr>, then_expr: Arc<Expr>, else_expr: Arc<Expr>, ) -> Self

Create a ternary (if-then-else) Expr. Takes Arcs instead of owned Exprs. test_expr must evaluate to a Bool type

Source

pub fn not(e: Expr) -> Self

Create a ‘not’ expression. e must evaluate to Bool type

Source

pub fn is_eq(e1: Expr, e2: Expr) -> Self

Create a ‘==’ expression

Source

pub fn noteq(e1: Expr, e2: Expr) -> Self

Create a ‘!=’ expression

Source

pub fn and(e1: Expr, e2: Expr) -> Self

Create an ‘and’ expression. Arguments must evaluate to Bool type

Source

pub fn or(e1: Expr, e2: Expr) -> Self

Create an ‘or’ expression. Arguments must evaluate to Bool type

Source

pub fn less(e1: Expr, e2: Expr) -> Self

Create a ‘<’ expression. Arguments must evaluate to Long type

Source

pub fn lesseq(e1: Expr, e2: Expr) -> Self

Create a ‘<=’ expression. Arguments must evaluate to Long type

Source

pub fn greater(e1: Expr, e2: Expr) -> Self

Create a ‘>’ expression. Arguments must evaluate to Long type

Source

pub fn greatereq(e1: Expr, e2: Expr) -> Self

Create a ‘>=’ expression. Arguments must evaluate to Long type

Source

pub fn add(e1: Expr, e2: Expr) -> Self

Create an ‘add’ expression. Arguments must evaluate to Long type

Source

pub fn sub(e1: Expr, e2: Expr) -> Self

Create a ‘sub’ expression. Arguments must evaluate to Long type

Source

pub fn mul(e1: Expr, e2: Expr) -> Self

Create a ‘mul’ expression. Arguments must evaluate to Long type

Source

pub fn neg(e: Expr) -> Self

Create a ‘neg’ expression. e must evaluate to Long type.

Source

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.

Source

pub fn contains(e1: Expr, e2: Expr) -> Self

Create a contains expression. First argument must have Set type.

Source

pub fn contains_all(e1: Expr, e2: Expr) -> Self

Create a containsAll expression. Arguments must evaluate to Set type

Source

pub fn contains_any(e1: Expr, e2: Expr) -> Self

Create a containsAny expression. Arguments must evaluate to Set type

Source

pub fn get_tag(expr: Expr, tag: Expr) -> Self

Create a getTag expression. expr must evaluate to Entity type, tag must evaluate to String type.

Source

pub fn has_tag(expr: Expr, tag: Expr) -> Self

Create a hasTag expression. expr must evaluate to Entity type, tag must evaluate to String type.

Source

pub fn set(exprs: impl IntoIterator<Item = Expr>) -> Self

Create an Expr which evaluates to a Set of the given Exprs

Source

pub fn record( pairs: impl IntoIterator<Item = (SmolStr, Expr)>, ) -> Result<Self, ExpressionConstructionError>

Create an Expr which evaluates to a Record with the given (key, value) pairs.

Source

pub fn record_arc(map: Arc<BTreeMap<SmolStr, Expr>>) -> Self

Create an Expr which evaluates to a Record with the given key-value mapping.

If you have an iterator of pairs, generally prefer calling Expr::record() instead of .collect()-ing yourself and calling this, potentially for efficiency reasons but also because Expr::record() will properly handle duplicate keys but your own .collect() will not (by default).

Source

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

Source

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

Source

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

Source

pub fn get_attr(expr: Expr, attr: SmolStr) -> Self

Create an Expr which gets a given attribute of a given Entity or record.

expr must evaluate to either Entity or Record type

Source

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 record.

expr must evaluate to either Entity or Record type

Source

pub fn like(expr: Expr, pattern: impl IntoIterator<Item = PatternElem>) -> Self

Create a ‘like’ expression.

expr must evaluate to a String type

Source

pub fn is_entity_type(expr: Expr, entity_type: EntityType) -> Self

Create an is expression.

Source

pub fn contains_unknown(&self) -> bool

Check if an expression contains any symbolic unknowns

Source

pub fn unknowns(&self) -> impl Iterator<Item = &Unknown>

Get all unknowns in an expression

Source

pub fn substitute(&self, definitions: &HashMap<SmolStr, Value>) -> Expr

Substitute unknowns with concrete values.

Ignores unmapped unknowns. Ignores type annotations on unknowns.

Source

pub fn substitute_typed( &self, definitions: &HashMap<SmolStr, Value>, ) -> Result<Expr, SubstitutionError>

Substitute unknowns with concrete values.

Ignores unmapped unknowns. Errors if the substituted value does not match the type annotation on the unknown.

Source§

impl<T> Expr<T>

Source

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.

Source

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>

Source§

fn as_ref(&self) -> &'a Expr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Expr> for RestrictedExpr

Source§

fn as_ref(&self) -> &Expr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for Expr<T>

Source§

fn clone(&self) -> Expr<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Expr<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Clone> Display for Expr<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<BorrowedRestrictedExpr<'a>> for &'a Expr

Source§

fn from(r: BorrowedRestrictedExpr<'a>) -> &'a Expr

Converts to this type from the input type.
Source§

impl<T: Clone> From<Expr<T>> for Clause

Source§

fn from(expr: Expr<T>) -> Clause

Converts to this type from the input type.
Source§

impl<T: Clone> From<Expr<T>> for Expr

Source§

fn from(expr: Expr<T>) -> Expr

Converts to this type from the input type.
Source§

impl From<Expr> for PartialValue

Source§

fn from(e: Expr) -> Self

Converts to this type from the input type.
Source§

impl From<ExtensionValueWithArgs> for Expr

Source§

fn from(val: ExtensionValueWithArgs) -> Self

Converts to this type from the input type.
Source§

impl From<PartialValue> for Expr

Source§

fn from(pv: PartialValue) -> Self

Converts to this type from the input type.
Source§

impl From<RestrictedExpr> for Expr

Source§

fn from(r: RestrictedExpr) -> Expr

Converts to this type from the input type.
Source§

impl From<Value> for Expr

Source§

fn from(v: Value) -> Self

Converts to this type from the input type.
Source§

impl From<ValueKind> for Expr

Source§

fn from(v: ValueKind) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Expr

Source§

type Err = ParseErrors

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Expr, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T: Hash> Hash for Expr<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq> PartialEq for Expr<T>

Source§

fn eq(&self, other: &Expr<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for Expr<T>
where T: Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl TryFrom<Expr> for Value

Source§

type Error = NotValue

The type returned in the event of a conversion error.
Source§

fn try_from(expr: Expr) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Expr> for ValueKind

Source§

type Error = NotValue

The type returned in the event of a conversion error.
Source§

fn try_from(expr: Expr) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Eq> Eq for Expr<T>

Source§

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>
where T: Send + Sync,

§

impl<T> Sync for Expr<T>
where T: Sync + Send,

§

impl<T> Unpin for Expr<T>
where T: Unpin,

§

impl<T> UnwindSafe for Expr<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToSmolStr for T
where T: Display + ?Sized,

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,