Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving error messages #1129

Merged
merged 7 commits into from
Aug 9, 2024
Merged
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
59 changes: 59 additions & 0 deletions cedar-policy-core/src/error_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ macro_rules! impl_diagnostic_from_source_loc_field {
};
}

/// Macro which implements the `.labels()` and `.source_code()` methods of
/// `miette::Diagnostic` by using the parameters `$i` and `$j` which must be the name
/// of fields of type `Loc`.
/// Both spans are underlined, only the first span is reported as the source code location
#[macro_export]
macro_rules! impl_diagnostic_from_source_loc_fields {
( $i:ident , $j:ident ) => {
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.$i.src as &dyn miette::SourceCode)
}

fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
Some(Box::new(
[
miette::LabeledSpan::underline(self.$i.span),
miette::LabeledSpan::underline(self.$j.span),
]
.into_iter(),
) as _)
}
};
}

/// Macro which implements the `.labels()` and `.source_code()` methods of
/// `miette::Diagnostic` by using the parameter `$i` which must be the name
/// of a field of type `Option<Loc>`
Expand All @@ -52,6 +75,42 @@ macro_rules! impl_diagnostic_from_source_loc_opt_field {
};
}

/// Macro which implements the `.labels()` and `.source_code()` methods of
/// `miette::Diagnostic` by using the parameters `$i` and `$j` which must be the name
/// of fields of type `Option<Loc>`
#[macro_export]
macro_rules! impl_diagnostic_from_source_loc_opt_fields {
( $i:ident , $j:ident ) => {
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
self.$i
.as_ref()
.map(|loc| &loc.src as &dyn miette::SourceCode)
.or_else(|| {
self.$j
.as_ref()
.map(|loc| &loc.src as &dyn miette::SourceCode)
})
}

fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
let x = self
.$i
.as_ref()
.map(|loc| miette::LabeledSpan::underline(loc.span));
let y = self
.$j
.as_ref()
.map(|loc| miette::LabeledSpan::underline(loc.span));

match (x, y) {
(None, None) => None,
(Some(span), None) | (None, Some(span)) => Some(Box::new(std::iter::once(span))),
(Some(span_a), Some(span_b)) => Some(Box::new([span_a, span_b].into_iter()) as _),
}
}
};
}

/// Macro which implements the `.labels()` and `.source_code()` methods of
/// `miette::Diagnostic` by using the parameter `$i` which must be an `Expr`
/// (or `Box<Expr>`) type field.
Expand Down
2 changes: 1 addition & 1 deletion cedar-policy-validator/src/cedar_schema/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub struct AttrDecl {
}

/// The target of a [`PRAppDecl`]
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PR {
/// Applies to the `principal` variable
Principal,
Expand Down
Loading
Loading