Skip to content

Commit a5865a4

Browse files
committed
Merge branch 'main' into 1285-core-schema-performance
2 parents 80b7ebc + 632860a commit a5865a4

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

cedar-policy-core/src/parser/cst_to_ast.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,26 @@ impl ast::UnreservedId {
490490
)
491491
.into())
492492
} 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);
493508
Err(ToASTError::new(
494-
ToASTErrorKind::UnknownMethod(self.clone()),
509+
ToASTErrorKind::UnknownMethod {
510+
id: self.clone(),
511+
hint,
512+
},
495513
loc.clone(),
496514
)
497515
.into())
@@ -4019,6 +4037,13 @@ mod tests {
40194037
.exactly_one_underline("[].bar()")
40204038
.build(),
40214039
),
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+
),
40224047
(
40234048
"bar([])",
40244049
ExpectedErrorMessageBuilder::error("`bar` is not a valid function")

cedar-policy-core/src/parser/err.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,14 @@ pub enum ToASTErrorKind {
297297
#[error("attempted to call `{0}.{1}(...)`, but `{0}` does not have any methods")]
298298
NoMethods(ast::Name, ast::UnreservedId),
299299
/// 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+
},
302308
/// Returned when a policy attempts to call a function that does not exist
303309
#[error("`{id}` is not a valid function")]
304310
UnknownFunction {

cedar-policy/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Cedar Language Version: TBD
2020

2121
- The error associated with parsing a non-existent extension function additionally
2222
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).
2325
- Extract action graph inversion from CoreSchema to ValidatorSchema instantiation to
2426
improve schema validation speeds. (#1290, as part of resolving #1285)
2527

0 commit comments

Comments
 (0)