Skip to content

Commit bd3d5f7

Browse files
authored
Revert "implement schema-format parsers for RFC 68 (#1136)" (#1202)
Signed-off-by: Craig Disselkoen <[email protected]>
1 parent b7cdb81 commit bd3d5f7

File tree

15 files changed

+441
-1980
lines changed

15 files changed

+441
-1980
lines changed

cedar-policy-validator/src/cedar_schema/ast.rs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -251,28 +251,15 @@ impl<N> From<PrimitiveType> for json_schema::TypeVariant<N> {
251251
}
252252

253253
/// Attribute declarations, used in records and entity types.
254-
/// One [`AttrDecl`] is one key-value pair, or an embedded attribute map pair `?: value_ty`.
255-
///
256-
/// The data structures in this file, and the Cedar schema format parser in
257-
/// general, permissively allow `EAMap`s to appear anywhere an [`AttrDecl`] is
258-
/// expected (including inside other `EAMap`s), in order to provide better error
259-
/// messages when `EAMap`s are encountered in illegal but plausible positions
254+
/// One [`AttrDecl`] is one key-value pair.
260255
#[derive(Debug, Clone)]
261-
pub enum AttrDecl {
262-
/// A normal attribute declaration `name: ty` or `name?: ty`
263-
Concrete {
264-
/// Name of this attribute
265-
name: Node<SmolStr>,
266-
/// Whether or not it is a required attribute (default `true`)
267-
required: bool,
268-
/// The type of this attribute
269-
ty: Node<Type>,
270-
},
271-
/// An `EAMap` declaration `?: ty`
272-
EAMap {
273-
/// Value type of the `EAMap`
274-
value_ty: Node<Type>,
275-
},
256+
pub struct AttrDecl {
257+
/// Name of this attribute
258+
pub name: Node<SmolStr>,
259+
/// Whether or not it is a required attribute (default `true`)
260+
pub required: bool,
261+
/// The type of this attribute
262+
pub ty: Node<Type>,
276263
}
277264

278265
/// The target of a [`PRAppDecl`]

cedar-policy-validator/src/cedar_schema/err.rs

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,6 @@ impl From<ToJsonSchemaError> for ToJsonSchemaErrors {
298298
}
299299
}
300300

301-
impl From<EAMapError> for ToJsonSchemaErrors {
302-
fn from(e: EAMapError) -> Self {
303-
Self::from(ToJsonSchemaError::from(e))
304-
}
305-
}
306-
307301
impl Display for ToJsonSchemaErrors {
308302
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
309303
write!(f, "{}", self.0.first()) // intentionally showing only the first error; see #326 for discussion on a similar error type
@@ -405,10 +399,6 @@ pub enum ToJsonSchemaError {
405399
#[error(transparent)]
406400
#[diagnostic(transparent)]
407401
ReservedSchemaKeyword(#[from] ReservedSchemaKeyword),
408-
/// Errors relating to embedded attribute maps
409-
#[error(transparent)]
410-
#[diagnostic(transparent)]
411-
EAMap(#[from] EAMapError),
412402
}
413403

414404
impl ToJsonSchemaError {
@@ -521,86 +511,6 @@ impl Diagnostic for ReservedName {
521511
}
522512
}
523513

524-
/// An embedded attribute map (RFC 68) was encountered where one is not allowed
525-
#[derive(Debug, Clone, Error, PartialEq, Eq)]
526-
#[error("found an embedded attribute map type, but embedded attribute maps are not allowed in this position")]
527-
pub struct EAMapNotAllowedHereError {
528-
/// Source location of the `EAMap`
529-
pub(crate) source_loc: Loc,
530-
/// Context-dependent help text
531-
pub(crate) help: Option<EAMapNotAllowedHereHelp>,
532-
}
533-
534-
impl Diagnostic for EAMapNotAllowedHereError {
535-
impl_diagnostic_from_source_loc_field!(source_loc);
536-
537-
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
538-
self.help.as_ref().map(|help| Box::new(help) as _)
539-
}
540-
}
541-
542-
#[derive(Debug, Clone, Error, PartialEq, Eq)]
543-
pub(crate) enum EAMapNotAllowedHereHelp {
544-
#[error("Embedded attribute maps are not allowed as the top-level descriptor of all attributes of an entity. Try making an entity attribute to hold the embedded attribute map. E.g., `attributes: {{ ?: someType }}`.")]
545-
TopLevelEAMap,
546-
}
547-
548-
/// Encountered a type like `{ foo: Long, ?: String }` that mixes concrete attributes with an `EAMap`.
549-
/// This is currently not allowed.
550-
#[derive(Debug, Clone, Error, PartialEq, Eq)]
551-
#[error("this type contains both concrete attributes and an embedded attribute map (`?:`), which is not allowed")]
552-
pub struct EAMapWithConcreteAttributesError {
553-
/// Source location of the `EAMap` declaration
554-
pub(crate) source_loc: Loc,
555-
}
556-
557-
impl Diagnostic for EAMapWithConcreteAttributesError {
558-
impl_diagnostic_from_source_loc_field!(source_loc);
559-
}
560-
561-
/// Encountered a type like `{ ?: Long, ?: String }` that mixes two or more `EAMap` declarations.
562-
/// This is currently not allowed.
563-
#[derive(Debug, Clone, Error, PartialEq, Eq)]
564-
#[error("this type contains two or more different embedded attribute map declarations (`?:`), which is not allowed")]
565-
pub struct MultipleEAMapDeclarationsError {
566-
/// Source location of the first `EAMap` declaration
567-
pub(crate) source_loc_1: Loc,
568-
/// Source location of the second `EAMap` declaration
569-
pub(crate) source_loc_2: Loc,
570-
}
571-
572-
impl Diagnostic for MultipleEAMapDeclarationsError {
573-
impl_diagnostic_from_two_source_loc_fields!(source_loc_1, source_loc_2);
574-
575-
fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
576-
Some(Box::new(
577-
"try separating this into two different attributes",
578-
))
579-
}
580-
}
581-
582-
/// Errors relating to embedded attribute maps (`EAMap`s)
583-
//
584-
// This is NOT a publicly exported error type.
585-
#[derive(Debug, Clone, Diagnostic, Error, PartialEq, Eq)]
586-
#[non_exhaustive]
587-
pub enum EAMapError {
588-
/// An embedded attribute map (RFC 68) was encountered where one is not allowed
589-
#[error(transparent)]
590-
#[diagnostic(transparent)]
591-
NotAllowedHere(#[from] EAMapNotAllowedHereError),
592-
/// Encountered a type like `{ foo: Long, ?: String }` that mixes concrete attributes with an `EAMap`.
593-
/// This is currently not allowed.
594-
#[error(transparent)]
595-
#[diagnostic(transparent)]
596-
WithConcreteAttributes(#[from] EAMapWithConcreteAttributesError),
597-
/// Encountered a type like `{ ?: Long, ?: String }` that mixes two or more `EAMap` declarations.
598-
/// This is currently not allowed.
599-
#[error(transparent)]
600-
#[diagnostic(transparent)]
601-
MultipleEAMapDeclarations(#[from] MultipleEAMapDeclarationsError),
602-
}
603-
604514
#[derive(Debug, Clone, PartialEq, Eq, Error)]
605515
#[error("unknown type name: `{name}`")]
606516
pub struct UnknownTypeName {

cedar-policy-validator/src/cedar_schema/fmt.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<N: Display> Display for json_schema::Type<N> {
7474
}
7575
}
7676

77-
impl<N: Display> Display for json_schema::RecordType<json_schema::RecordAttributeType<N>> {
77+
impl<N: Display> Display for json_schema::RecordType<N> {
7878
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
7979
write!(f, "{{")?;
8080
for (i, (n, ty)) in self.attributes.iter().enumerate() {
@@ -94,37 +94,6 @@ impl<N: Display> Display for json_schema::RecordType<json_schema::RecordAttribut
9494
}
9595
}
9696

97-
impl<N: Display> Display for json_schema::RecordType<json_schema::EntityAttributeType<N>> {
98-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99-
write!(f, "{{")?;
100-
for (i, (n, ty)) in self.attributes.iter().enumerate() {
101-
write!(
102-
f,
103-
"\"{}\"{}: {}",
104-
n.escape_debug(),
105-
if ty.required { "" } else { "?" },
106-
ty.ty
107-
)?;
108-
if i < (self.attributes.len() - 1) {
109-
write!(f, ", ")?;
110-
}
111-
}
112-
write!(f, "}}")?;
113-
Ok(())
114-
}
115-
}
116-
117-
impl<N: Display> Display for json_schema::EntityAttributeTypeInternal<N> {
118-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119-
match self {
120-
json_schema::EntityAttributeTypeInternal::Type(ty) => ty.fmt(f),
121-
json_schema::EntityAttributeTypeInternal::EAMap { value_type } => {
122-
write!(f, "{{ ?: {value_type} }}")
123-
}
124-
}
125-
}
126-
}
127-
12897
/// Create a non-empty with borrowed contents from a slice
12998
fn non_empty_slice<T>(v: &[T]) -> Option<NonEmpty<&T>> {
13099
let vs: Vec<&T> = v.iter().collect();

cedar-policy-validator/src/cedar_schema/grammar.lalrpop

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,15 @@ pub Type: Node<SType> = {
183183
=> Node::with_source_loc(SType::Record(ds.unwrap_or_default()), Loc::new(l..r, Arc::clone(src))),
184184
}
185185

186-
// AttrDecls := [Name ['?'] | '?'] ':' Type [',' | ',' AttrDecls]
186+
// AttrDecls := Name ['?'] ':' Type [',' | ',' AttrDecls]
187187
AttrDecls: Vec<Node<AttrDecl>> = {
188188
<l:@L> <name: Name> <required:"?"?> ":" <ty:Type> ","? <r:@R>
189-
=> vec![Node::with_source_loc(AttrDecl::Concrete { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))],
189+
=> vec![Node::with_source_loc(AttrDecl { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))],
190190
<l:@L> <name: Name> <required:"?"?> ":" <ty:Type> "," <r:@R> <mut ds: AttrDecls>
191-
=> {ds.insert(0, Node::with_source_loc(AttrDecl::Concrete { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))); ds},
192-
<l:@L> "?" ":" <value_ty:Type> ","? <r:@R>
193-
=> vec![Node::with_source_loc(AttrDecl::EAMap { value_ty }, Loc::new(l..r, Arc::clone(src)))],
194-
<l:@L> "?" ":" <value_ty:Type> "," <r:@R> <mut ds: AttrDecls>
195-
=> {ds.insert(0, Node::with_source_loc(AttrDecl::EAMap { value_ty }, Loc::new(l..r, Arc::clone(src)))); ds},
191+
=> {ds.insert(0, Node::with_source_loc(AttrDecl { name, required: required.is_none(), ty}, Loc::new(l..r, Arc::clone(src)))); ds},
196192
}
197193

194+
198195
Comma<E>: Vec<E> = {
199196
<e:E?> => e.into_iter().collect(),
200197
<mut es:(<E> ",")+> <e:E> => {

0 commit comments

Comments
 (0)