File tree Expand file tree Collapse file tree 3 files changed +36
-3
lines changed
cedar-policy-core/src/parser Expand file tree Collapse file tree 3 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -490,8 +490,26 @@ impl ast::UnreservedId {
490
490
)
491
491
. into ( ) )
492
492
} else {
493
+ fn suggest_method (
494
+ name : & ast:: UnreservedId ,
495
+ methods : & HashSet < ast:: UnreservedId > ,
496
+ ) -> Option < String > {
497
+ const SUGGEST_METHOD_MAX_DISTANCE : usize = 3 ;
498
+ let method_names =
499
+ methods. iter ( ) . map ( ToString :: to_string) . collect :: < Vec < _ > > ( ) ;
500
+ let suggested_method = fuzzy_search_limited (
501
+ & name. to_string ( ) ,
502
+ method_names. as_slice ( ) ,
503
+ Some ( SUGGEST_METHOD_MAX_DISTANCE ) ,
504
+ ) ;
505
+ suggested_method. map ( |m| format ! ( "did you mean `{m}`?" ) )
506
+ }
507
+ let hint = suggest_method ( & self , & EXTENSION_STYLES . methods ) ;
493
508
Err ( ToASTError :: new (
494
- ToASTErrorKind :: UnknownMethod ( self . clone ( ) ) ,
509
+ ToASTErrorKind :: UnknownMethod {
510
+ id : self . clone ( ) ,
511
+ hint,
512
+ } ,
495
513
loc. clone ( ) ,
496
514
)
497
515
. into ( ) )
@@ -4019,6 +4037,13 @@ mod tests {
4019
4037
. exactly_one_underline ( "[].bar()" )
4020
4038
. build ( ) ,
4021
4039
) ,
4040
+ (
4041
+ "principal.addr.isipv4()" ,
4042
+ ExpectedErrorMessageBuilder :: error ( "`isipv4` is not a valid method" )
4043
+ . exactly_one_underline ( "principal.addr.isipv4()" )
4044
+ . help ( "did you mean `isIpv4`?" )
4045
+ . build ( ) ,
4046
+ ) ,
4022
4047
(
4023
4048
"bar([])" ,
4024
4049
ExpectedErrorMessageBuilder :: error ( "`bar` is not a valid function" )
Original file line number Diff line number Diff line change @@ -297,8 +297,14 @@ pub enum ToASTErrorKind {
297
297
#[ error( "attempted to call `{0}.{1}(...)`, but `{0}` does not have any methods" ) ]
298
298
NoMethods ( ast:: Name , ast:: UnreservedId ) ,
299
299
/// Returned when a policy attempts to call a method that does not exist
300
- #[ error( "`{0}` is not a valid method" ) ]
301
- UnknownMethod ( ast:: UnreservedId ) ,
300
+ #[ error( "`{id}` is not a valid method" ) ]
301
+ UnknownMethod {
302
+ /// The user-provided method id
303
+ id : ast:: UnreservedId ,
304
+ /// The hint to resolve the error
305
+ #[ help]
306
+ hint : Option < String > ,
307
+ } ,
302
308
/// Returned when a policy attempts to call a function that does not exist
303
309
#[ error( "`{id}` is not a valid function" ) ]
304
310
UnknownFunction {
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ Cedar Language Version: TBD
20
20
21
21
- The error associated with parsing a non-existent extension function additionally
22
22
includes a suggestion based on available extension functions (#1280 , resolving #332 ).
23
+ - The error associated with parsing a non-existent extension method additionally
24
+ includes a suggestion based on available extension methods (#1289 , resolving #246 ).
23
25
- Extract action graph inversion from CoreSchema to ValidatorSchema instantiation to
24
26
improve schema validation speeds. (#1290 , as part of resolving #1285 )
25
27
You can’t perform that action at this time.
0 commit comments