cedar_policy_core/ast/restricted_expr.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
/*
* Copyright Cedar Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use super::{
EntityUID, Expr, ExprKind, ExpressionConstructionError, Literal, Name, PartialValue, Type,
Unknown, Value, ValueKind,
};
use crate::entities::json::err::JsonSerializationError;
use crate::extensions::Extensions;
use crate::parser::err::ParseErrors;
use crate::parser::{self, Loc};
use miette::Diagnostic;
use serde::{Deserialize, Serialize};
use smol_str::{SmolStr, ToSmolStr};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::sync::Arc;
use thiserror::Error;
/// 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`.
#[derive(Deserialize, Serialize, Hash, Debug, Clone, PartialEq, Eq)]
#[serde(transparent)]
pub struct RestrictedExpr(Expr);
impl RestrictedExpr {
/// 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.
pub fn new(expr: Expr) -> Result<Self, RestrictedExpressionError> {
is_restricted(&expr)?;
Ok(Self(expr))
}
/// 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.
pub fn new_unchecked(expr: Expr) -> Self {
// in debug builds, this does the check anyway, panicking if it fails
if cfg!(debug_assertions) {
// PANIC SAFETY: We're in debug mode and panicking intentionally
#[allow(clippy::unwrap_used)]
Self::new(expr).unwrap()
} else {
Self(expr)
}
}
/// Return the `RestrictedExpr`, but with the new `source_loc` (or `None`).
pub fn with_maybe_source_loc(self, source_loc: Option<Loc>) -> Self {
Self(self.0.with_maybe_source_loc(source_loc))
}
/// Create a `RestrictedExpr` that's just a single `Literal`.
///
/// Note that you can pass this a `Literal`, an `Integer`, a `String`, etc.
pub fn val(v: impl Into<Literal>) -> Self {
// All literals are valid restricted-exprs
Self::new_unchecked(Expr::val(v))
}
/// Create a `RestrictedExpr` that's just a single `Unknown`.
pub fn unknown(u: Unknown) -> Self {
// All unknowns are valid restricted-exprs
Self::new_unchecked(Expr::unknown(u))
}
/// Create a `RestrictedExpr` which evaluates to a Set of the given `RestrictedExpr`s
pub fn set(exprs: impl IntoIterator<Item = RestrictedExpr>) -> Self {
// Set expressions are valid restricted-exprs if their elements are; and
// we know the elements are because we require `RestrictedExpr`s in the
// parameter
Self::new_unchecked(Expr::set(exprs.into_iter().map(Into::into)))
}
/// 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.
pub fn record(
pairs: impl IntoIterator<Item = (SmolStr, RestrictedExpr)>,
) -> Result<Self, ExpressionConstructionError> {
// Record expressions are valid restricted-exprs if their elements are;
// and we know the elements are because we require `RestrictedExpr`s in
// the parameter
Ok(Self::new_unchecked(Expr::record(
pairs.into_iter().map(|(k, v)| (k, v.into())),
)?))
}
/// Create a `RestrictedExpr` which calls the given extension function
pub fn call_extension_fn(
function_name: Name,
args: impl IntoIterator<Item = RestrictedExpr>,
) -> Self {
// Extension-function calls are valid restricted-exprs if their
// arguments are; and we know the arguments are because we require
// `RestrictedExpr`s in the parameter
Self::new_unchecked(Expr::call_extension_fn(
function_name,
args.into_iter().map(Into::into).collect(),
))
}
/// Write a RestrictedExpr in "natural JSON" format.
///
/// Used to output the context as a map from Strings to JSON Values
pub fn to_natural_json(&self) -> Result<serde_json::Value, JsonSerializationError> {
self.as_borrowed().to_natural_json()
}
/// Get the `bool` value of this `RestrictedExpr` if it's a boolean, or
/// `None` if it is not a boolean
pub fn as_bool(&self) -> Option<bool> {
// the only way a `RestrictedExpr` can be a boolean is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::Bool(b)) => Some(*b),
_ => None,
}
}
/// Get the `i64` value of this `RestrictedExpr` if it's a long, or `None`
/// if it is not a long
pub fn as_long(&self) -> Option<i64> {
// the only way a `RestrictedExpr` can be a long is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::Long(i)) => Some(*i),
_ => None,
}
}
/// Get the `SmolStr` value of this `RestrictedExpr` if it's a string, or
/// `None` if it is not a string
pub fn as_string(&self) -> Option<&SmolStr> {
// the only way a `RestrictedExpr` can be a string is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::String(s)) => Some(s),
_ => None,
}
}
/// Get the `EntityUID` value of this `RestrictedExpr` if it's an entity
/// reference, or `None` if it is not an entity reference
pub fn as_euid(&self) -> Option<&EntityUID> {
// the only way a `RestrictedExpr` can be an entity reference is if it's
// a literal
match self.expr_kind() {
ExprKind::Lit(Literal::EntityUID(e)) => Some(e),
_ => None,
}
}
/// Get `Unknown` value of this `RestrictedExpr` if it's an `Unknown`, or
/// `None` if it is not an `Unknown`
pub fn as_unknown(&self) -> Option<&Unknown> {
match self.expr_kind() {
ExprKind::Unknown(u) => Some(u),
_ => None,
}
}
/// Iterate over the elements of the set if this `RestrictedExpr` is a set,
/// or `None` if it is not a set
pub fn as_set_elements(&self) -> Option<impl Iterator<Item = BorrowedRestrictedExpr<'_>>> {
match self.expr_kind() {
ExprKind::Set(set) => Some(set.iter().map(BorrowedRestrictedExpr::new_unchecked)), // since the RestrictedExpr invariant holds for the input set, it will hold for each element as well
_ => None,
}
}
/// Iterate over the (key, value) pairs of the record if this
/// `RestrictedExpr` is a record, or `None` if it is not a record
pub fn as_record_pairs(
&self,
) -> Option<impl Iterator<Item = (&SmolStr, BorrowedRestrictedExpr<'_>)>> {
match self.expr_kind() {
ExprKind::Record(map) => Some(
map.iter()
.map(|(k, v)| (k, BorrowedRestrictedExpr::new_unchecked(v))),
), // since the RestrictedExpr invariant holds for the input record, it will hold for each attr value as well
_ => None,
}
}
/// 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
pub fn as_extn_fn_call(
&self,
) -> Option<(&Name, impl Iterator<Item = BorrowedRestrictedExpr<'_>>)> {
match self.expr_kind() {
ExprKind::ExtensionFunctionApp { fn_name, args } => Some((
fn_name,
args.iter().map(BorrowedRestrictedExpr::new_unchecked),
)), // since the RestrictedExpr invariant holds for the input call, it will hold for each argument as well
_ => None,
}
}
}
impl From<Value> for RestrictedExpr {
fn from(value: Value) -> RestrictedExpr {
RestrictedExpr::from(value.value).with_maybe_source_loc(value.loc)
}
}
impl From<ValueKind> for RestrictedExpr {
fn from(value: ValueKind) -> RestrictedExpr {
match value {
ValueKind::Lit(lit) => RestrictedExpr::val(lit),
ValueKind::Set(set) => {
RestrictedExpr::set(set.iter().map(|val| RestrictedExpr::from(val.clone())))
}
// PANIC SAFETY: cannot have duplicate key because the input was already a BTreeMap
#[allow(clippy::expect_used)]
ValueKind::Record(record) => RestrictedExpr::record(
Arc::unwrap_or_clone(record)
.into_iter()
.map(|(k, v)| (k, RestrictedExpr::from(v))),
)
.expect("can't have duplicate keys, because the input `map` was already a BTreeMap"),
ValueKind::ExtensionValue(ev) => {
let ev = Arc::unwrap_or_clone(ev);
RestrictedExpr::call_extension_fn(ev.constructor, ev.args)
}
}
}
}
impl TryFrom<PartialValue> for RestrictedExpr {
type Error = PartialValueToRestrictedExprError;
fn try_from(pvalue: PartialValue) -> Result<RestrictedExpr, PartialValueToRestrictedExprError> {
match pvalue {
PartialValue::Value(v) => Ok(RestrictedExpr::from(v)),
PartialValue::Residual(expr) => match RestrictedExpr::new(expr) {
Ok(e) => Ok(e),
Err(RestrictedExpressionError::InvalidRestrictedExpression(
restricted_expr_errors::InvalidRestrictedExpressionError { expr, .. },
)) => Err(PartialValueToRestrictedExprError::NontrivialResidual {
residual: Box::new(expr),
}),
},
}
}
}
/// Errors when converting `PartialValue` to `RestrictedExpr`
#[derive(Debug, PartialEq, Diagnostic, Error)]
pub enum PartialValueToRestrictedExprError {
/// The `PartialValue` contains a nontrivial residual that isn't a valid `RestrictedExpr`
#[error("residual is not a valid restricted expression: `{residual}`")]
NontrivialResidual {
/// Residual that isn't a valid `RestrictedExpr`
residual: Box<Expr>,
},
}
impl std::str::FromStr for RestrictedExpr {
type Err = RestrictedExpressionParseError;
fn from_str(s: &str) -> Result<RestrictedExpr, Self::Err> {
parser::parse_restrictedexpr(s)
}
}
/// While `RestrictedExpr` wraps an _owned_ `Expr`, `BorrowedRestrictedExpr`
/// wraps a _borrowed_ `Expr`, with the same invariants.
///
/// We derive `Copy` for this type because it's just a single reference, and
/// `&T` is `Copy` for all `T`.
#[derive(Serialize, Hash, Debug, Clone, PartialEq, Eq, Copy)]
pub struct BorrowedRestrictedExpr<'a>(&'a Expr);
impl<'a> BorrowedRestrictedExpr<'a> {
/// Create a new `BorrowedRestrictedExpr` 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.
pub fn new(expr: &'a Expr) -> Result<Self, RestrictedExpressionError> {
is_restricted(expr)?;
Ok(Self(expr))
}
/// Create a new `BorrowedRestrictedExpr` 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.
pub fn new_unchecked(expr: &'a Expr) -> Self {
// in debug builds, this does the check anyway, panicking if it fails
if cfg!(debug_assertions) {
// PANIC SAFETY: We're in debug mode and panicking intentionally
#[allow(clippy::unwrap_used)]
Self::new(expr).unwrap()
} else {
Self(expr)
}
}
/// Write a BorrowedRestrictedExpr in "natural JSON" format.
///
/// Used to output the context as a map from Strings to JSON Values
pub fn to_natural_json(self) -> Result<serde_json::Value, JsonSerializationError> {
Ok(serde_json::to_value(
crate::entities::json::CedarValueJson::from_expr(self)?,
)?)
}
/// Convert `BorrowedRestrictedExpr` to `RestrictedExpr`.
/// This has approximately the cost of cloning the `Expr`.
pub fn to_owned(self) -> RestrictedExpr {
RestrictedExpr::new_unchecked(self.0.clone())
}
/// Get the `bool` value of this `RestrictedExpr` if it's a boolean, or
/// `None` if it is not a boolean
pub fn as_bool(&self) -> Option<bool> {
// the only way a `RestrictedExpr` can be a boolean is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::Bool(b)) => Some(*b),
_ => None,
}
}
/// Get the `i64` value of this `RestrictedExpr` if it's a long, or `None`
/// if it is not a long
pub fn as_long(&self) -> Option<i64> {
// the only way a `RestrictedExpr` can be a long is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::Long(i)) => Some(*i),
_ => None,
}
}
/// Get the `SmolStr` value of this `RestrictedExpr` if it's a string, or
/// `None` if it is not a string
pub fn as_string(&self) -> Option<&SmolStr> {
// the only way a `RestrictedExpr` can be a string is if it's a literal
match self.expr_kind() {
ExprKind::Lit(Literal::String(s)) => Some(s),
_ => None,
}
}
/// Get the `EntityUID` value of this `RestrictedExpr` if it's an entity
/// reference, or `None` if it is not an entity reference
pub fn as_euid(&self) -> Option<&EntityUID> {
// the only way a `RestrictedExpr` can be an entity reference is if it's
// a literal
match self.expr_kind() {
ExprKind::Lit(Literal::EntityUID(e)) => Some(e),
_ => None,
}
}
/// Get `Unknown` value of this `RestrictedExpr` if it's an `Unknown`, or
/// `None` if it is not an `Unknown`
pub fn as_unknown(&self) -> Option<&Unknown> {
match self.expr_kind() {
ExprKind::Unknown(u) => Some(u),
_ => None,
}
}
/// Iterate over the elements of the set if this `RestrictedExpr` is a set,
/// or `None` if it is not a set
pub fn as_set_elements(&self) -> Option<impl Iterator<Item = BorrowedRestrictedExpr<'_>>> {
match self.expr_kind() {
ExprKind::Set(set) => Some(set.iter().map(BorrowedRestrictedExpr::new_unchecked)), // since the RestrictedExpr invariant holds for the input set, it will hold for each element as well
_ => None,
}
}
/// Iterate over the (key, value) pairs of the record if this
/// `RestrictedExpr` is a record, or `None` if it is not a record
pub fn as_record_pairs(
&self,
) -> Option<impl Iterator<Item = (&'_ SmolStr, BorrowedRestrictedExpr<'_>)>> {
match self.expr_kind() {
ExprKind::Record(map) => Some(
map.iter()
.map(|(k, v)| (k, BorrowedRestrictedExpr::new_unchecked(v))),
), // since the RestrictedExpr invariant holds for the input record, it will hold for each attr value as well
_ => None,
}
}
/// 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
pub fn as_extn_fn_call(
&self,
) -> Option<(&Name, impl Iterator<Item = BorrowedRestrictedExpr<'_>>)> {
match self.expr_kind() {
ExprKind::ExtensionFunctionApp { fn_name, args } => Some((
fn_name,
args.iter().map(BorrowedRestrictedExpr::new_unchecked),
)), // since the RestrictedExpr invariant holds for the input call, it will hold for each argument as well
_ => None,
}
}
/// Try to compute the runtime type of this expression. See
/// [`Expr::try_type_of`] for exactly what this computes.
///
/// On a restricted expression, there are fewer cases where we might fail to
/// compute the type, but there are still `unknown`s and extension function
/// calls which may cause this function to return `None` .
pub fn try_type_of(&self, extensions: &Extensions<'_>) -> Option<Type> {
self.0.try_type_of(extensions)
}
}
/// Helper function: does the given `Expr` qualify as a "restricted" expression.
///
/// Returns `Ok(())` if yes, or a `RestrictedExpressionError` if no.
fn is_restricted(expr: &Expr) -> Result<(), RestrictedExpressionError> {
match expr.expr_kind() {
ExprKind::Lit(_) => Ok(()),
ExprKind::Unknown(_) => Ok(()),
ExprKind::Var(_) => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "variables".into(),
expr: expr.clone(),
}
.into()),
ExprKind::Slot(_) => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "template slots".into(),
expr: expr.clone(),
}
.into()),
ExprKind::If { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "if-then-else".into(),
expr: expr.clone(),
}
.into()),
ExprKind::And { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "&&".into(),
expr: expr.clone(),
}
.into()),
ExprKind::Or { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "||".into(),
expr: expr.clone(),
}
.into()),
ExprKind::UnaryApp { op, .. } => {
Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: op.to_smolstr(),
expr: expr.clone(),
}
.into())
}
ExprKind::BinaryApp { op, .. } => {
Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: op.to_smolstr(),
expr: expr.clone(),
}
.into())
}
ExprKind::GetAttr { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "attribute accesses".into(),
expr: expr.clone(),
}
.into()),
ExprKind::HasAttr { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "'has'".into(),
expr: expr.clone(),
}
.into()),
ExprKind::Like { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "'like'".into(),
expr: expr.clone(),
}
.into()),
ExprKind::Is { .. } => Err(restricted_expr_errors::InvalidRestrictedExpressionError {
feature: "'is'".into(),
expr: expr.clone(),
}
.into()),
ExprKind::ExtensionFunctionApp { args, .. } => args.iter().try_for_each(is_restricted),
ExprKind::Set(exprs) => exprs.iter().try_for_each(is_restricted),
ExprKind::Record(map) => map.values().try_for_each(is_restricted),
}
}
// converting into Expr is always safe; restricted exprs are always valid Exprs
impl From<RestrictedExpr> for Expr {
fn from(r: RestrictedExpr) -> Expr {
r.0
}
}
impl AsRef<Expr> for RestrictedExpr {
fn as_ref(&self) -> &Expr {
&self.0
}
}
impl Deref for RestrictedExpr {
type Target = Expr;
fn deref(&self) -> &Expr {
self.as_ref()
}
}
impl std::fmt::Display for RestrictedExpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
// converting into Expr is always safe; restricted exprs are always valid Exprs
impl<'a> From<BorrowedRestrictedExpr<'a>> for &'a Expr {
fn from(r: BorrowedRestrictedExpr<'a>) -> &'a Expr {
r.0
}
}
impl<'a> AsRef<Expr> for BorrowedRestrictedExpr<'a> {
fn as_ref(&self) -> &'a Expr {
self.0
}
}
impl RestrictedExpr {
/// Turn an `&RestrictedExpr` into a `BorrowedRestrictedExpr`
pub fn as_borrowed(&self) -> BorrowedRestrictedExpr<'_> {
BorrowedRestrictedExpr::new_unchecked(self.as_ref())
}
}
impl<'a> Deref for BorrowedRestrictedExpr<'a> {
type Target = Expr;
fn deref(&self) -> &'a Expr {
self.0
}
}
impl<'a> std::fmt::Display for BorrowedRestrictedExpr<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
/// Like `ExprShapeOnly`, but for restricted expressions.
///
/// A newtype wrapper around (borrowed) restricted expressions that provides
/// `Eq` and `Hash` implementations that ignore any source information or other
/// generic data used to annotate the expression.
#[derive(Eq, Debug, Clone)]
pub struct RestrictedExprShapeOnly<'a>(BorrowedRestrictedExpr<'a>);
impl<'a> RestrictedExprShapeOnly<'a> {
/// Construct a `RestrictedExprShapeOnly` from a `BorrowedRestrictedExpr`.
/// The `BorrowedRestrictedExpr` is not modified, but any comparisons on the
/// resulting `RestrictedExprShapeOnly` will ignore source information and
/// generic data.
pub fn new(e: BorrowedRestrictedExpr<'a>) -> RestrictedExprShapeOnly<'a> {
RestrictedExprShapeOnly(e)
}
}
impl<'a> PartialEq for RestrictedExprShapeOnly<'a> {
fn eq(&self, other: &Self) -> bool {
self.0.eq_shape(&other.0)
}
}
impl<'a> Hash for RestrictedExprShapeOnly<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash_shape(state);
}
}
/// Error when constructing a restricted expression from unrestricted
/// expression
//
// CAUTION: this type is publicly exported in `cedar-policy`.
// Don't make fields `pub`, don't make breaking changes, and use caution
// when adding public methods.
#[derive(Debug, Clone, PartialEq, Eq, Error, Diagnostic)]
pub enum RestrictedExpressionError {
/// An expression was expected to be a "restricted" expression, but contained
/// a feature that is not allowed in restricted expressions.
#[error(transparent)]
#[diagnostic(transparent)]
InvalidRestrictedExpression(#[from] restricted_expr_errors::InvalidRestrictedExpressionError),
}
/// Error subtypes for [`RestrictedExpressionError`]
pub mod restricted_expr_errors {
use super::Expr;
use crate::impl_diagnostic_from_expr_field;
use miette::Diagnostic;
use smol_str::SmolStr;
use thiserror::Error;
/// An expression was expected to be a "restricted" expression, but contained
/// a feature that is not allowed in restricted expressions.
//
// CAUTION: this type is publicly exported in `cedar-policy`.
// Don't make fields `pub`, don't make breaking changes, and use caution
// when adding public methods.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
#[error("not allowed to use {feature} in a restricted expression: `{expr}`")]
pub struct InvalidRestrictedExpressionError {
/// String description of what disallowed feature appeared in the expression
pub(crate) feature: SmolStr,
/// the (sub-)expression that uses the disallowed feature. This may be a
/// sub-expression of a larger expression.
pub(crate) expr: Expr,
}
// custom impl of `Diagnostic`: take source location from the `expr` field
impl Diagnostic for InvalidRestrictedExpressionError {
impl_diagnostic_from_expr_field!(expr);
}
}
/// Errors possible from `RestrictedExpr::from_str()`
//
// This is NOT a publicly exported error type.
#[derive(Debug, Clone, PartialEq, Eq, Diagnostic, Error)]
pub enum RestrictedExpressionParseError {
/// Failed to parse the expression
#[error(transparent)]
#[diagnostic(transparent)]
Parse(#[from] ParseErrors),
/// Parsed successfully as an expression, but failed to construct a
/// restricted expression, for the reason indicated in the underlying error
#[error(transparent)]
#[diagnostic(transparent)]
InvalidRestrictedExpression(#[from] RestrictedExpressionError),
}
#[cfg(test)]
mod test {
use super::*;
use crate::ast::expression_construction_errors;
use crate::parser::err::{ParseError, ToASTError, ToASTErrorKind};
use crate::parser::Loc;
use std::str::FromStr;
use std::sync::Arc;
#[test]
fn duplicate_key() {
// duplicate key is an error when mapped to values of different types
assert_eq!(
RestrictedExpr::record([
("foo".into(), RestrictedExpr::val(37),),
("foo".into(), RestrictedExpr::val("hello"),),
]),
Err(expression_construction_errors::DuplicateKeyError {
key: "foo".into(),
context: "in record literal",
}
.into())
);
// duplicate key is an error when mapped to different values of same type
assert_eq!(
RestrictedExpr::record([
("foo".into(), RestrictedExpr::val(37),),
("foo".into(), RestrictedExpr::val(101),),
]),
Err(expression_construction_errors::DuplicateKeyError {
key: "foo".into(),
context: "in record literal",
}
.into())
);
// duplicate key is an error when mapped to the same value multiple times
assert_eq!(
RestrictedExpr::record([
("foo".into(), RestrictedExpr::val(37),),
("foo".into(), RestrictedExpr::val(37),),
]),
Err(expression_construction_errors::DuplicateKeyError {
key: "foo".into(),
context: "in record literal",
}
.into())
);
// duplicate key is an error even when other keys appear in between
assert_eq!(
RestrictedExpr::record([
("bar".into(), RestrictedExpr::val(-3),),
("foo".into(), RestrictedExpr::val(37),),
("spam".into(), RestrictedExpr::val("eggs"),),
("foo".into(), RestrictedExpr::val(37),),
("eggs".into(), RestrictedExpr::val("spam"),),
]),
Err(expression_construction_errors::DuplicateKeyError {
key: "foo".into(),
context: "in record literal",
}
.into())
);
// duplicate key is also an error when parsing from string
let str = r#"{ foo: 37, bar: "hi", foo: 101 }"#;
assert_eq!(
RestrictedExpr::from_str(str),
Err(RestrictedExpressionParseError::Parse(
ParseErrors::singleton(ParseError::ToAST(ToASTError::new(
ToASTErrorKind::ExpressionConstructionError(
expression_construction_errors::DuplicateKeyError {
key: "foo".into(),
context: "in record literal",
}
.into()
),
Loc::new(0..32, Arc::from(str))
)))
)),
)
}
}