Skip to content
Closed
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
7 changes: 3 additions & 4 deletions cedar-policy-validator/src/human_schema/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum UserError {
EmptyList,
#[error("Invalid escape codes")]
StringEscape(NonEmpty<UnescapeError>),
Comment on lines 17 to 18
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced by this PR, but it seems like it would be useful to print the UnescapeError content.

#[error("`{0}` is a reserved identifier")]
ReservedIdentifierUsed(SmolStr),
}

pub(crate) type RawLocation = usize;
Expand Down Expand Up @@ -99,10 +101,7 @@ impl Display for ParseError {
} => write!(f, "extra token `{token}`"),
OwnedRawParseError::User {
error: Node { node, .. },
} => match node {
UserError::EmptyList => write!(f, "expected a non-empty list"),
UserError::StringEscape(unescape_errs) => write!(f, "{}", unescape_errs.first()),
},
} => write!(f, "{node}"),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion cedar-policy-validator/src/human_schema/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,13 @@ Ident: Node<Id> = {
<l:@L> STRING <r:@R>
=> Node::with_source_loc("String".parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
<l:@L> <i:IDENTIFIER> <r:@R>
=> Node::with_source_loc(i.parse().unwrap(), Loc::new(l..r, Arc::clone(src))),
=>? Id::from_str(i)
.map(|id : Id| Node::with_source_loc(id, Loc::new(l..r, Arc::clone(src))))
.map_err(|err : cedar_policy_core::parser::err::ParseErrors|
ParseError::User {
error: Node::with_source_loc(UserError::ReservedIdentifierUsed(i.to_smolstr()), Loc::new(l..r, Arc::clone(src)))
}
)
}

STR: Node<SmolStr> = {
Expand Down
52 changes: 52 additions & 0 deletions cedar-policy-validator/src/human_schema/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1422,4 +1422,56 @@ mod translator_tests {
let foo = ns.entity_types.get("Foo").unwrap();
assert_eq!(foo.member_of_types, vec!["String".to_smolstr()]);
}


#[test]
fn entity_named_if() {
let src = r#"
entity if = {};
entity Foo in [if] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_like() {
let src = r#"
entity like = {};
entity Foo in [like] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_true() {
let src = r#"
entity true = {};
entity Foo in [true] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_false() {
let src = r#"
entity false = {};
entity Foo in [false] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

#[test]
fn entity_named_has() {
let src = r#"
entity has = {};
entity Foo in [has] = {};
"#;

assert!(SchemaFragment::from_str_natural(src).is_err());
}

}