Skip to content

Commit

Permalink
Removes some panics (#529)
Browse files Browse the repository at this point in the history
Co-authored-by: Craig Disselkoen <[email protected]>
  • Loading branch information
aaronjeline and cdisselkoen authored Dec 21, 2023
1 parent 4f1ab98 commit 3e238a4
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 163 deletions.
1 change: 1 addition & 0 deletions cedar-policy-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ smol_str = { version = "0.2", features = ["serde"] }
stacker = "0.1.15"
arbitrary = { version = "1", features = ["derive"], optional = true }
miette = { version = "5.9.0", features = ["serde"] }
nonempty = "0.9.0"

# decimal extension requires regex
regex = { version = "1.8", features = ["unicode"], optional = true }
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-core/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@ mod test {
let e = Expr::has_attr(Expr::val("a"), "\0".into());
assert_eq!(format!("{e}"), r#""a" has "\0""#);
// `\`'s escaped form is `\\`
let e = Expr::has_attr(Expr::val("a"), r#"\"#.into());
let e = Expr::has_attr(Expr::val("a"), r"\".into());
assert_eq!(format!("{e}"), r#""a" has "\\""#);
}

Expand Down
23 changes: 8 additions & 15 deletions cedar-policy-core/src/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,14 @@ impl Policy {
id: Option<ast::PolicyID>,
) -> Result<ast::Template, FromJsonError> {
let id = id.unwrap_or(ast::PolicyID::from_string("JSON policy"));
let conditions = match self.conditions.len() {
0 => ast::Expr::val(true),
_ => {
let mut conditions = self
.conditions
.into_iter()
.map(|cond| cond.try_into_ast(id.clone()));
// PANIC SAFETY checked above that `conditions` has at least 1 element
#[allow(clippy::expect_used)]
let first = conditions
.next()
.expect("already checked there is at least 1")?;
ast::ExprBuilder::with_data(())
.and_nary(first, conditions.collect::<Result<Vec<_>, _>>()?)
}
let mut conditions_iter = self
.conditions
.into_iter()
.map(|cond| cond.try_into_ast(id.clone()));
let conditions = match conditions_iter.next() {
None => ast::Expr::val(true),
Some(first) => ast::ExprBuilder::with_data(())
.and_nary(first?, conditions_iter.collect::<Result<Vec<_>, _>>()?),
};
Ok(ast::Template::new(
id,
Expand Down
Loading

0 comments on commit 3e238a4

Please sign in to comment.