@@ -88,13 +88,40 @@ impl<'a, T: Equiv> Equiv for &'a T {
8888    } 
8989} 
9090
91+ impl  Equiv  for  cedar_policy_core:: est:: Annotations  { 
92+     fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
93+         Equiv :: equiv ( & lhs. 0 ,  & rhs. 0 ) 
94+     } 
95+ } 
96+ 
97+ impl  Equiv  for  Option < cedar_policy_core:: ast:: Annotation >  { 
98+     fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
99+         match  ( lhs,  rhs)  { 
100+             ( Some ( a) ,  Some ( b) )  => { 
101+                 if  a == b { 
102+                     return  Ok ( ( ) ) ; 
103+                 } 
104+             } 
105+             ( Some ( a) ,  None )  | ( None ,  Some ( a) )  => { 
106+                 if  a. val . is_empty ( )  { 
107+                     return  Ok ( ( ) ) ; 
108+                 } 
109+             } 
110+             ( None ,  None )  => return  Ok ( ( ) ) , 
111+         } ; 
112+         Err ( format ! ( "{lhs:?} and {rhs:?} are not equivalent" ) ) 
113+     } 
114+ } 
115+ 
91116impl < N :  Clone  + PartialEq  + Debug  + Display  + TypeName  + Ord >  Equiv 
92117    for  json_schema:: NamespaceDefinition < N > 
93118{ 
94119    fn  equiv ( 
95120        lhs :  & json_schema:: NamespaceDefinition < N > , 
96121        rhs :  & json_schema:: NamespaceDefinition < N > , 
97122    )  -> Result < ( ) ,  String >  { 
123+         Equiv :: equiv ( & lhs. annotations ,  & rhs. annotations ) 
124+             . map_err ( |e| format ! ( "mismatch in namespace annotations: {e}" ) ) ?; 
98125        Equiv :: equiv ( & lhs. entity_types ,  & rhs. entity_types ) 
99126            . map_err ( |e| format ! ( "mismatch in entity type declarations: {e}" ) ) ?; 
100127        Equiv :: equiv ( & lhs. common_types ,  & rhs. common_types ) 
@@ -191,12 +218,16 @@ impl<K: Eq + Ord + Display, V: Equiv> Equiv for BTreeMap<K, V> {
191218
192219impl < N :  Clone  + PartialEq  + Debug  + Display  + TypeName  + Ord >  Equiv  for  json_schema:: CommonType < N >  { 
193220    fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
221+         Equiv :: equiv ( & lhs. annotations ,  & rhs. annotations ) 
222+             . map_err ( |e| format ! ( "mismatch in common type annotations: {e}" ) ) ?; 
194223        Equiv :: equiv ( & lhs. ty ,  & rhs. ty ) 
195224    } 
196225} 
197226
198227impl < N :  Clone  + PartialEq  + Debug  + Display  + TypeName  + Ord >  Equiv  for  json_schema:: EntityType < N >  { 
199228    fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
229+         Equiv :: equiv ( & lhs. annotations ,  & rhs. annotations ) 
230+             . map_err ( |e| format ! ( "mismatch in entity annotations: {e}" ) ) ?; 
200231        Equiv :: equiv ( 
201232            & lhs. member_of_types . iter ( ) . collect :: < BTreeSet < _ > > ( ) , 
202233            & rhs. member_of_types . iter ( ) . collect :: < BTreeSet < _ > > ( ) , 
@@ -226,6 +257,8 @@ impl Equiv for cedar_policy_validator::ValidatorEntityType {
226257
227258impl < N :  Clone  + PartialEq  + TypeName  + Debug  + Display >  Equiv  for  json_schema:: TypeOfAttribute < N >  { 
228259    fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
260+         Equiv :: equiv ( & lhs. annotations ,  & rhs. annotations ) 
261+             . map_err ( |e| format ! ( "mismatch in type of attribute annotations: {e}" ) ) ?; 
229262        if  lhs. required  != rhs. required  { 
230263            return  Err ( "attributes differ in required flag" . into ( ) ) ; 
231264        } 
@@ -434,6 +467,8 @@ impl TypeName for InternalName {
434467
435468impl < N :  PartialEq  + Debug  + Display  + Clone  + TypeName  + Ord >  Equiv  for  json_schema:: ActionType < N >  { 
436469    fn  equiv ( lhs :  & Self ,  rhs :  & Self )  -> Result < ( ) ,  String >  { 
470+         Equiv :: equiv ( & lhs. annotations ,  & rhs. annotations ) 
471+             . map_err ( |e| format ! ( "mismatch in action annotations: {e}" ) ) ?; 
437472        if  & lhs. attributes  != & rhs. attributes 
438473            && !( lhs. attributes . as_ref ( ) . is_none_or ( HashMap :: is_empty) 
439474                && rhs. attributes . as_ref ( ) . is_none_or ( HashMap :: is_empty) ) 
@@ -517,3 +552,70 @@ impl Equiv for cedar_policy_validator::ValidatorSchema {
517552        Ok ( ( ) ) 
518553    } 
519554} 
555+ 
556+ #[ cfg( test) ]  
557+ mod  tests { 
558+     use  cedar_drt:: est:: Annotations ; 
559+ 
560+     use  crate :: schemas:: Equiv ; 
561+ 
562+     #[ test]  
563+     fn  annotations ( )  { 
564+         // positive cases 
565+         let  pairs:  [ ( Annotations ,  Annotations ) ;  5 ]  = [ 
566+             // value being null is equivalent to an empty string 
567+             ( 
568+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
569+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "" } ) ) . unwrap ( ) , 
570+             ) , 
571+             ( 
572+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "" } ) ) . unwrap ( ) , 
573+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
574+             ) , 
575+             // both values being null is also equivalent 
576+             ( 
577+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
578+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
579+             ) , 
580+             // otherwise compare non-null values 
581+             ( 
582+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🥨" } ) ) . unwrap ( ) , 
583+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🥨" } ) ) . unwrap ( ) , 
584+             ) , 
585+             ( 
586+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🥨" ,  "b" :  "🥯🍩" } ) ) . unwrap ( ) , 
587+                 serde_json:: from_value ( serde_json:: json!( { "b" :  "🥯🍩" ,  "a" :  "🥨" } ) ) . unwrap ( ) , 
588+             ) , 
589+         ] ; 
590+         pairs
591+             . iter ( ) 
592+             . for_each ( |( a,  b) | assert ! ( Equiv :: equiv( a,  b) . is_ok( ) ) ) ; 
593+ 
594+         // negative cases 
595+         let  pairs:  [ ( Annotations ,  Annotations ) ;  5 ]  = [ 
596+             ( 
597+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
598+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🍪" } ) ) . unwrap ( ) , 
599+             ) , 
600+             ( 
601+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "" } ) ) . unwrap ( ) , 
602+                 serde_json:: from_value ( serde_json:: json!( { "b" :  null} ) ) . unwrap ( ) , 
603+             ) , 
604+             ( 
605+                 serde_json:: from_value ( serde_json:: json!( { "a" :  null} ) ) . unwrap ( ) , 
606+                 serde_json:: from_value ( serde_json:: json!( { "b" :  null} ) ) . unwrap ( ) , 
607+             ) , 
608+             ( 
609+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🥨" } ) ) . unwrap ( ) , 
610+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🍪" } ) ) . unwrap ( ) , 
611+             ) , 
612+             ( 
613+                 serde_json:: from_value ( serde_json:: json!( { "a" :  "🥨" ,  "b" :  "🥯🍪" } ) ) . unwrap ( ) , 
614+                 serde_json:: from_value ( serde_json:: json!( { "b" :  "🥯🍩" ,  "a" :  "🥨" } ) ) . unwrap ( ) , 
615+             ) , 
616+         ] ; 
617+         pairs
618+             . iter ( ) 
619+             . for_each ( |( a,  b) | assert ! ( Equiv :: equiv( a,  b) . is_err( ) ) ) ; 
620+     } 
621+ } 
0 commit comments