Struct cedar_policy_core::ast::RestrictedExpr

source ·
pub struct RestrictedExpr(/* private fields */);
Expand description

A few places in Core use these “restricted expressions” (for lack of a better term) which are in some sense the minimal subset of Expr required to express all possible Values.

Specifically, “restricted” expressions are defined as expressions containing only the following:

  • bool, int, and string literals
  • literal EntityUIDs such as User::“alice”
  • extension function calls, where the arguments must be other things on this list
  • set and record literals, where the values must be other things on this list

That means the following are not allowed in “restricted” expressions:

  • principal, action, resource, context
  • builtin operators and functions, including ., in, has, like, .contains()
  • if-then-else expressions

These restrictions represent the expressions that are allowed to appear as attribute values in Slice and Context.

Implementations§

source§

impl RestrictedExpr

source

pub fn new(expr: Expr) -> Result<Self, RestrictedExpressionError>

Create a new RestrictedExpr from an Expr.

This function is “safe” in the sense that it will verify that the provided expr does indeed qualify as a “restricted” expression, returning an error if not.

Note this check requires recursively walking the AST. For a version of this function that doesn’t perform this check, see new_unchecked() below.

source

pub fn new_unchecked(expr: Expr) -> Self

Create a new RestrictedExpr from an Expr, where the caller is responsible for ensuring that the Expr is a valid “restricted expression”. If it is not, internal invariants will be violated, which may lead to other errors later, panics, or even incorrect results.

For a “safer” version of this function that returns an error for invalid inputs, see new() above.

source

pub fn val(v: impl Into<Literal>) -> Self

Create a RestrictedExpr that’s just a single Literal.

Note that you can pass this a Literal, an i64, a String, etc.

source

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

Create a RestrictedExpr which evaluates to a Set of the given RestrictedExprs

source

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

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

source

pub fn call_extension_fn(function_name: Name, args: Vec<RestrictedExpr>) -> Self

Create a RestrictedExpr which calls the given extension function

source§

impl RestrictedExpr

source

pub fn as_borrowed(&self) -> BorrowedRestrictedExpr<'_>

Turn an &RestrictedExpr into a BorrowedRestrictedExpr

Methods from Deref<Target = Expr>§

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 data(&self) -> &T

Access the data stored on the Expr.

source

pub fn source_info(&self) -> &Option<SourceInfo>

Access the data stored on the Expr.

source

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.

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 headers, 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 = &SlotId>

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 is_unknown(&self) -> bool

Check if an expression contains any symbolic unknowns

source

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

Get all unknowns in an expression

source

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

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 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 AsRef<RestrictedExpr> for Context

source§

fn as_ref(&self) -> &RestrictedExpr

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

impl Clone for RestrictedExpr

source§

fn clone(&self) -> RestrictedExpr

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 Debug for RestrictedExpr

source§

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

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

impl<'de> Deserialize<'de> for RestrictedExpr

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 Display for RestrictedExpr

source§

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

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

impl From<RestrictedExpr> for Expr

source§

fn from(r: RestrictedExpr) -> Expr

Converts to this type from the input type.
source§

impl FromStr for RestrictedExpr

source§

type Err = ParseErrors

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

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

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

impl Hash for RestrictedExpr

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 PartialEq for RestrictedExpr

source§

fn eq(&self, other: &RestrictedExpr) -> 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 Serialize for RestrictedExpr

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 Deref for RestrictedExpr

source§

type Target = Expr

The resulting type after dereferencing.
source§

fn deref(&self) -> &Expr

Dereferences the value.
source§

impl Eq for RestrictedExpr

source§

impl StructuralPartialEq for RestrictedExpr

Auto Trait Implementations§

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