Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions cedar-policy-core/src/ast/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1651,8 +1651,8 @@ mod test {
use crate::{
ast::{entity, name, EntityUID},
parser::{
err::{ParseError, ParseErrors, ToASTError},
parse_policy,
err::{ParseError, ParseErrors, ToASTError, ToASTErrorKind},
parse_policy, SourceInfo,
},
};

Expand Down Expand Up @@ -1996,19 +1996,23 @@ mod test {
let ParseErrors(errs) = parse_policy(Some("id".into()), policy_str).err().unwrap();
assert_eq!(
&errs[0],
&ParseError::ToAST(ToASTError::UnexpectedTemplate {
slot: crate::parser::cst::Slot::Principal
})
&ParseError::ToAST(ToASTError::new(
ToASTErrorKind::UnexpectedTemplate {
slot: crate::parser::cst::Slot::Principal
},
SourceInfo(0..50)
))
);
assert_eq!(errs.len(), 1);
let policy_str =
r#"permit(principal == ?principal, action, resource) when { ?principal == 3 } ;"#;
let ParseErrors(errs) = parse_policy(Some("id".into()), policy_str).err().unwrap();
assert!(
errs.contains(&ParseError::ToAST(ToASTError::UnexpectedTemplate {
assert!(errs.contains(&ParseError::ToAST(ToASTError::new(
ToASTErrorKind::UnexpectedTemplate {
slot: crate::parser::cst::Slot::Principal
}))
);
},
SourceInfo(50..74)
))));
assert_eq!(errs.len(), 2);
}
}
10 changes: 8 additions & 2 deletions cedar-policy-core/src/ast/restricted_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,10 @@ pub enum RestrictedExprParseError {
#[cfg(test)]
mod test {
use super::*;
use crate::parser::err::{ParseError, ToASTError};
use crate::parser::{
err::{ParseError, ToASTError, ToASTErrorKind},
SourceInfo,
};
use std::str::FromStr;

#[test]
Expand Down Expand Up @@ -670,7 +673,10 @@ mod test {
assert_eq!(
RestrictedExpr::from_str(r#"{ foo: 37, bar: "hi", foo: 101 }"#),
Err(RestrictedExprParseError::Parse(ParseErrors(vec![
ParseError::ToAST(ToASTError::DuplicateKeyInRecordLiteral { key: "foo".into() })
ParseError::ToAST(ToASTError::new(
ToASTErrorKind::DuplicateKeyInRecordLiteral { key: "foo".into() },
SourceInfo(0..32)
))
]))),
)
}
Expand Down
19 changes: 12 additions & 7 deletions cedar-policy-core/src/est.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod utils;
use crate::ast;
use crate::entities::EntityUidJson;
use crate::parser::cst;
use crate::parser::err::{ParseError, ParseErrors, ToASTError};
use crate::parser::err::{ParseErrors, ToASTError, ToASTErrorKind};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use smol_str::SmolStr;
Expand Down Expand Up @@ -118,9 +118,11 @@ impl TryFrom<cst::Policy> for Policy {
.conds
.into_iter()
.map(|node| {
let (cond, _) = node.into_inner();
let (cond, l) = node.into_inner();
let cond = cond.ok_or_else(|| {
ParseErrors(vec![ParseError::ToAST(ToASTError::EmptyClause(None))])
ParseErrors(vec![
ToASTError::new(ToASTErrorKind::EmptyClause(None), l).into()
])
})?;
cond.try_into()
})
Expand All @@ -134,9 +136,9 @@ impl TryFrom<cst::Policy> for Policy {
match (errs.is_empty(), kv) {
(true, Some((k, v))) => Ok((k, v)),
(false, _) => Err(errs),
(true, None) => {
Err(ParseError::ToAST(ToASTError::AnnotationInvariantViolation).into())
}
(true, None) => Err(node
.to_ast_err(ToASTErrorKind::AnnotationInvariantViolation)
.into()),
}
})
.collect::<Result<_, ParseErrors>>()?;
Expand Down Expand Up @@ -164,7 +166,10 @@ impl TryFrom<cst::Cond> for Clause {
let ident = is_when.map(|is_when| {
cst::Ident::Ident(if is_when { "when" } else { "unless" }.into())
});
Err(ParseError::ToAST(ToASTError::EmptyClause(ident)).into())
Err(cond
.cond
.to_ast_err(ToASTErrorKind::EmptyClause(ident))
.into())
}
Some(ref e) => e.try_into(),
};
Expand Down
127 changes: 85 additions & 42 deletions cedar-policy-core/src/est/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ use crate::entities::{
};
use crate::extensions::Extensions;
use crate::parser::cst::{self, Ident};
use crate::parser::err::{ParseErrors, ToASTError};
use crate::parser::unescape;
use crate::parser::err::{ParseErrors, ToASTError, ToASTErrorKind};
use crate::parser::ASTNode;
use crate::parser::{unescape, SourceInfo};
use crate::{ast, FromNormalizedStr};
use either::Either;
use itertools::Itertools;
Expand Down Expand Up @@ -864,22 +864,22 @@ impl TryFrom<&ASTNode<Option<cst::Relation>>> for Expr {
Ok(field_expr) => {
let field_str = field_expr
.into_string_literal()
.map_err(|_| ToASTError::HasNonLiteralRHS)?;
.map_err(|_| field.to_ast_err(ToASTErrorKind::HasNonLiteralRHS))?;
Ok(Expr::has_attr(target_expr, field_str))
}
Err(_) => match is_add_name(field.ok_or_missing()?) {
Some(name) => Ok(Expr::has_attr(target_expr, name.to_string().into())),
None => Err(ToASTError::HasNonLiteralRHS.into()),
None => Err(field.to_ast_err(ToASTErrorKind::HasNonLiteralRHS).into()),
},
}
}
cst::Relation::Like { target, pattern } => {
let target_expr = target.try_into()?;
let pat_expr: Expr = pattern.try_into()?;
let pat_str = pat_expr.into_string_literal().map_err(|e| {
ToASTError::InvalidPattern(
pattern.to_ast_err(ToASTErrorKind::InvalidPattern(
serde_json::to_string(&e).unwrap_or_else(|_| "<malformed est>".to_string()),
)
))
})?;
Ok(Expr::like(target_expr, pat_str))
}
Expand Down Expand Up @@ -995,8 +995,12 @@ impl TryFrom<&ASTNode<Option<cst::Mult>>> for Expr {
cst::MultOp::Times => {
expr = Expr::mul(expr, rhs);
}
cst::MultOp::Divide => return Err(ToASTError::UnsupportedDivision.into()),
cst::MultOp::Mod => return Err(ToASTError::UnsupportedModulo.into()),
cst::MultOp::Divide => {
return Err(node.to_ast_err(ToASTErrorKind::UnsupportedDivision).into())
}
cst::MultOp::Mod => {
return Err(node.to_ast_err(ToASTErrorKind::UnsupportedModulo).into())
}
}
}
Ok(expr)
Expand Down Expand Up @@ -1055,8 +1059,12 @@ impl TryFrom<&ASTNode<Option<cst::Unary>>> for Expr {
}
}
}
Some(cst::NegOp::OverBang) => Err(ToASTError::UnaryOpLimit(ast::UnaryOp::Not).into()),
Some(cst::NegOp::OverDash) => Err(ToASTError::UnaryOpLimit(ast::UnaryOp::Neg).into()),
Some(cst::NegOp::OverBang) => Err(u
.to_ast_err(ToASTErrorKind::UnaryOpLimit(ast::UnaryOp::Not))
.into()),
Some(cst::NegOp::OverDash) => Err(u
.to_ast_err(ToASTErrorKind::UnaryOpLimit(ast::UnaryOp::Neg))
.into()),
None => Ok(inner),
}
}
Expand Down Expand Up @@ -1091,7 +1099,9 @@ fn interpret_primary(
_ => Err(errs),
}
}
cst::Ref::Ref { .. } => Err(ToASTError::UnsupportedEntityLiterals.into()),
cst::Ref::Ref { .. } => Err(node
.to_ast_err(ToASTErrorKind::UnsupportedEntityLiterals)
.into()),
},
cst::Primary::Name(node) => {
let name = node.ok_or_missing()?;
Expand Down Expand Up @@ -1119,10 +1129,13 @@ fn interpret_primary(
),
(_, _) => (0, 0),
};
Err(ToASTError::InvalidExpression(cst::Name {
path: path.to_vec(),
name: ASTNode::new(Some(id.clone()), l, r),
})
Err(ToASTError::new(
ToASTErrorKind::InvalidExpression(cst::Name {
path: path.to_vec(),
name: ASTNode::new(Some(id.clone()), l, r),
}),
SourceInfo(l..r),
)
.into())
}
}
Expand Down Expand Up @@ -1151,7 +1164,7 @@ fn interpret_primary(
} else {
match s {
Some(s) => Ok((s, v.try_into()?)),
None => Err(ToASTError::MissingNodeData.into()),
None => Err(node.to_ast_err(ToASTErrorKind::MissingNodeData).into()),
}
}
})
Expand All @@ -1172,12 +1185,18 @@ impl TryFrom<&ASTNode<Option<cst::Member>>> for Expr {
cst::Ident::Ident(i) => {
item = match item {
Either::Left(name) => {
return Err(ToASTError::InvalidAccess(name, i.clone()).into())
return Err(node
.to_ast_err(ToASTErrorKind::InvalidAccess(name, i.clone()))
.into())
}
Either::Right(expr) => Either::Right(Expr::get_attr(expr, i.clone())),
};
}
i => return Err(ToASTError::InvalidIdentifier(i.to_string()).into()),
i => {
return Err(node
.to_ast_err(ToASTErrorKind::InvalidIdentifier(i.to_string()))
.into())
}
},
cst::MemAccess::Call(args) => {
// we have item(args). We hope item is either:
Expand All @@ -1204,15 +1223,27 @@ impl TryFrom<&ASTNode<Option<cst::Member>>> for Expr {
match attr.as_str() {
"contains" => Either::Right(Expr::contains(
left,
extract_single_argument(args, "contains()")?,
extract_single_argument(
args,
"contains()",
access.info.clone(),
)?,
)),
"containsAll" => Either::Right(Expr::contains_all(
left,
extract_single_argument(args, "containsAll()")?,
extract_single_argument(
args,
"containsAll()",
access.info.clone(),
)?,
)),
"containsAny" => Either::Right(Expr::contains_any(
left,
extract_single_argument(args, "containsAny()")?,
extract_single_argument(
args,
"containsAny()",
access.info.clone(),
)?,
)),
_ => {
// have to add the "receiver" argument as
Expand All @@ -1223,22 +1254,26 @@ impl TryFrom<&ASTNode<Option<cst::Member>>> for Expr {
}
}
}
_ => return Err(ToASTError::ExpressionCall.into()),
_ => return Err(access.to_ast_err(ToASTErrorKind::ExpressionCall).into()),
};
}
cst::MemAccess::Index(node) => {
let s = Expr::try_from(node)?
.into_string_literal()
.map_err(|_| ToASTError::NonStringIndex)?;
.map_err(|_| node.to_ast_err(ToASTErrorKind::NonStringIndex))?;
item = match item {
Either::Left(name) => return Err(ToASTError::InvalidIndex(name, s).into()),
Either::Left(name) => {
return Err(node
.to_ast_err(ToASTErrorKind::InvalidIndex(name, s))
.into())
}
Either::Right(expr) => Either::Right(Expr::get_attr(expr, s)),
};
}
}
}
match item {
Either::Left(_) => Err(ToASTError::MembershipInvariantViolation)?,
Either::Left(_) => Err(m.to_ast_err(ToASTErrorKind::MembershipInvariantViolation))?,
Either::Right(expr) => Ok(expr),
}
}
Expand All @@ -1247,13 +1282,16 @@ impl TryFrom<&ASTNode<Option<cst::Member>>> for Expr {
fn extract_single_argument(
es: impl ExactSizeIterator<Item = Expr>,
fn_name: &'static str,
info: SourceInfo,
) -> Result<Expr, ParseErrors> {
let mut iter = es.fuse().peekable();
let first = iter.next();
let second = iter.peek();
match (first, second) {
(None, _) => Err(ToASTError::wrong_arity(fn_name, 1, 0).into()),
(Some(_), Some(_)) => Err(ToASTError::wrong_arity(fn_name, 1, iter.len()).into()),
(None, _) => Err(ToASTError::new(ToASTErrorKind::wrong_arity(fn_name, 1, 0), info).into()),
(Some(_), Some(_)) => {
Err(ToASTError::new(ToASTErrorKind::wrong_arity(fn_name, 1, iter.len()), info).into())
}
(Some(first), None) => Ok(first),
}
}
Expand All @@ -1266,13 +1304,13 @@ impl TryFrom<&ASTNode<Option<cst::Literal>>> for Expr {
cst::Literal::False => Ok(Expr::lit(CedarValueJson::Bool(false))),
cst::Literal::Num(n) => Ok(Expr::lit(CedarValueJson::Long(
(*n).try_into()
.map_err(|_| ToASTError::IntegerLiteralTooLarge(*n))?,
.map_err(|_| lit.to_ast_err(ToASTErrorKind::IntegerLiteralTooLarge(*n)))?,
))),
cst::Literal::Str(node) => match node.ok_or_missing()? {
cst::Str::String(s) => Ok(Expr::lit(CedarValueJson::String(s.clone()))),
cst::Str::Invalid(invalid_str) => {
Err(ToASTError::InvalidString(invalid_str.to_string()).into())
}
cst::Str::Invalid(invalid_str) => Err(node
.to_ast_err(ToASTErrorKind::InvalidString(invalid_str.to_string()))
.into()),
},
}
}
Expand All @@ -1296,11 +1334,12 @@ impl TryFrom<&ASTNode<Option<cst::Name>>> for Expr {
),
(_, _) => (0, 0),
};
Err(ToASTError::InvalidExpression(cst::Name {
path: path.to_vec(),
name: ASTNode::new(Some(id.clone()), l, r),
})
.into())
Err(name
.to_ast_err(ToASTErrorKind::InvalidExpression(cst::Name {
path: path.to_vec(),
name: ASTNode::new(Some(id.clone()), l, r),
}))
.into())
}
}
}
Expand Down Expand Up @@ -1337,6 +1376,8 @@ fn ident_to_str_len(i: &Ident) -> usize {
// PANIC SAFETY: Unit Test Code
#[allow(clippy::panic)]
mod test {
use cool_asserts::assert_matches;

use crate::parser::err::ParseError;

use super::*;
Expand All @@ -1354,13 +1395,15 @@ mod test {
Ok(_) => panic!("wrong error"),
Err(e) => {
assert!(e.len() == 1);
match &e[0] {
ParseError::ToAST(ToASTError::InvalidExpression(e)) => {
println!("{:?}", e);
assert_eq!(e.name.info.range_end(), 16);
assert_matches!(&e[0],
ParseError::ToAST(to_ast_error) => {
assert_matches!(to_ast_error.kind(), ToASTErrorKind::InvalidExpression(e) => {
println!("{:?}", e);
assert_eq!(e.name.info.range_end(), 16);
}
)
}
_ => panic!("wrong error"),
}
);
}
}
}
Expand Down
Loading