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 Value
s.
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
impl RestrictedExpr
Sourcepub fn new(expr: Expr) -> Result<Self, RestrictedExpressionError>
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.
Sourcepub fn new_unchecked(expr: Expr) -> Self
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.
Sourcepub fn with_maybe_source_loc(self, source_loc: Option<Loc>) -> Self
pub fn with_maybe_source_loc(self, source_loc: Option<Loc>) -> Self
Return the RestrictedExpr
, but with the new source_loc
(or None
).
Sourcepub fn val(v: impl Into<Literal>) -> Self
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 Integer
, a String
, etc.
Sourcepub fn set(exprs: impl IntoIterator<Item = RestrictedExpr>) -> Self
pub fn set(exprs: impl IntoIterator<Item = RestrictedExpr>) -> Self
Create a RestrictedExpr
which evaluates to a Set of the given RestrictedExpr
s
Sourcepub fn record(
pairs: impl IntoIterator<Item = (SmolStr, RestrictedExpr)>,
) -> Result<Self, ExpressionConstructionError>
pub fn record( pairs: impl IntoIterator<Item = (SmolStr, RestrictedExpr)>, ) -> Result<Self, ExpressionConstructionError>
Create a RestrictedExpr
which evaluates to a Record with the given
(key, value) pairs.
Throws an error if any key occurs two or more times.
Sourcepub fn call_extension_fn(
function_name: Name,
args: impl IntoIterator<Item = RestrictedExpr>,
) -> Self
pub fn call_extension_fn( function_name: Name, args: impl IntoIterator<Item = RestrictedExpr>, ) -> Self
Create a RestrictedExpr
which calls the given extension function
Sourcepub fn to_natural_json(&self) -> Result<Value, JsonSerializationError>
pub fn to_natural_json(&self) -> Result<Value, JsonSerializationError>
Write a RestrictedExpr in “natural JSON” format.
Used to output the context as a map from Strings to JSON Values
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
Get the bool
value of this RestrictedExpr
if it’s a boolean, or
None
if it is not a boolean
Sourcepub fn as_long(&self) -> Option<i64>
pub fn as_long(&self) -> Option<i64>
Get the i64
value of this RestrictedExpr
if it’s a long, or None
if it is not a long
Sourcepub fn as_string(&self) -> Option<&SmolStr>
pub fn as_string(&self) -> Option<&SmolStr>
Get the SmolStr
value of this RestrictedExpr
if it’s a string, or
None
if it is not a string
Sourcepub fn as_euid(&self) -> Option<&EntityUID>
pub fn as_euid(&self) -> Option<&EntityUID>
Get the EntityUID
value of this RestrictedExpr
if it’s an entity
reference, or None
if it is not an entity reference
Sourcepub fn as_unknown(&self) -> Option<&Unknown>
pub fn as_unknown(&self) -> Option<&Unknown>
Get Unknown
value of this RestrictedExpr
if it’s an Unknown
, or
None
if it is not an Unknown
Sourcepub fn as_set_elements(
&self,
) -> Option<impl Iterator<Item = BorrowedRestrictedExpr<'_>>>
pub fn as_set_elements( &self, ) -> Option<impl Iterator<Item = BorrowedRestrictedExpr<'_>>>
Iterate over the elements of the set if this RestrictedExpr
is a set,
or None
if it is not a set
Sourcepub fn as_record_pairs(
&self,
) -> Option<impl Iterator<Item = (&SmolStr, BorrowedRestrictedExpr<'_>)>>
pub fn as_record_pairs( &self, ) -> Option<impl Iterator<Item = (&SmolStr, BorrowedRestrictedExpr<'_>)>>
Iterate over the (key, value) pairs of the record if this
RestrictedExpr
is a record, or None
if it is not a record
Sourcepub fn as_extn_fn_call(
&self,
) -> Option<(&Name, impl Iterator<Item = BorrowedRestrictedExpr<'_>>)>
pub fn as_extn_fn_call( &self, ) -> Option<(&Name, impl Iterator<Item = BorrowedRestrictedExpr<'_>>)>
Get the name and args of the called extension function if this
RestrictedExpr
is an extension function call, or None
if it is not
an extension function call
Source§impl RestrictedExpr
impl RestrictedExpr
Sourcepub fn as_borrowed(&self) -> BorrowedRestrictedExpr<'_>
pub fn as_borrowed(&self) -> BorrowedRestrictedExpr<'_>
Turn an &RestrictedExpr
into a BorrowedRestrictedExpr
Methods from Deref<Target = Expr>§
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 source_loc(&self) -> Option<&Loc>
pub fn source_loc(&self) -> Option<&Loc>
Access the Loc
stored on the Expr
.
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 scopes, 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 scopes, 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 = Slot> + '_
pub fn slots(&self) -> impl Iterator<Item = Slot> + '_
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
Sourcepub fn try_type_of(&self, extensions: &Extensions<'_>) -> Option<Type>
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.
Sourcepub fn contains_unknown(&self) -> bool
pub fn contains_unknown(&self) -> bool
Check if an expression contains any symbolic unknowns
Sourcepub fn substitute(&self, definitions: &HashMap<SmolStr, Value>) -> Expr
pub fn substitute(&self, definitions: &HashMap<SmolStr, Value>) -> Expr
Substitute unknowns with concrete values.
Ignores unmapped unknowns. Ignores type annotations on unknowns.
Sourcepub fn substitute_typed(
&self,
definitions: &HashMap<SmolStr, Value>,
) -> Result<Expr, SubstitutionError>
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.
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 AsRef<Expr> for RestrictedExpr
impl AsRef<Expr> for RestrictedExpr
Source§impl Clone for RestrictedExpr
impl Clone for RestrictedExpr
Source§fn clone(&self) -> RestrictedExpr
fn clone(&self) -> RestrictedExpr
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for RestrictedExpr
impl Debug for RestrictedExpr
Source§impl<'de> Deserialize<'de> for RestrictedExpr
impl<'de> Deserialize<'de> for RestrictedExpr
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 Display for RestrictedExpr
impl Display for RestrictedExpr
Source§impl From<Context> for RestrictedExpr
impl From<Context> for RestrictedExpr
Source§impl From<RestrictedExpr> for Expr
impl From<RestrictedExpr> for Expr
Source§fn from(r: RestrictedExpr) -> Expr
fn from(r: RestrictedExpr) -> Expr
Source§impl From<Value> for RestrictedExpr
impl From<Value> for RestrictedExpr
Source§fn from(value: Value) -> RestrictedExpr
fn from(value: Value) -> RestrictedExpr
Source§impl From<ValueKind> for RestrictedExpr
impl From<ValueKind> for RestrictedExpr
Source§fn from(value: ValueKind) -> RestrictedExpr
fn from(value: ValueKind) -> RestrictedExpr
Source§impl FromStr for RestrictedExpr
impl FromStr for RestrictedExpr
Source§impl Hash for RestrictedExpr
impl Hash for RestrictedExpr
Source§impl PartialEq for RestrictedExpr
impl PartialEq for RestrictedExpr
Source§impl Serialize for RestrictedExpr
impl Serialize for RestrictedExpr
Source§impl TryFrom<PartialValue> for RestrictedExpr
impl TryFrom<PartialValue> for RestrictedExpr
Source§type Error = PartialValueToRestrictedExprError
type Error = PartialValueToRestrictedExprError
Source§fn try_from(
pvalue: PartialValue,
) -> Result<RestrictedExpr, PartialValueToRestrictedExprError>
fn try_from( pvalue: PartialValue, ) -> Result<RestrictedExpr, PartialValueToRestrictedExprError>
Source§impl Deref for RestrictedExpr
impl Deref for RestrictedExpr
impl Eq for RestrictedExpr
impl StructuralPartialEq for RestrictedExpr
Auto Trait Implementations§
impl Freeze for RestrictedExpr
impl RefUnwindSafe for RestrictedExpr
impl Send for RestrictedExpr
impl Sync for RestrictedExpr
impl Unpin for RestrictedExpr
impl UnwindSafe for RestrictedExpr
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