Skip to content

Commit

Permalink
Add derives for Severity and Diagnostics (#1323)
Browse files Browse the repository at this point in the history
Add derives for `Diagnostic`, `Severity` and `Diagnostics`, also `Deref`
for `Diagnostics`. In current shape these are very hard to work with,
which is caused by lack of basic functionalities (like `Eq`), also
`Diagnostics` can't be merged or extended with ready `Diagnostic` (only
by matching on it's `Severity` and then choosing correct method to
reconstruct in again)
  • Loading branch information
Draggu authored Jun 2, 2024
1 parent eff29c7 commit cf698d4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions plugins/cairo-lang-macro/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl From<AuxData> for Vec<u8> {
}

/// Diagnostic returned by the procedural macro.
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Diagnostic {
/// A human addressed message associated with the [`Diagnostic`].
///
Expand All @@ -176,7 +176,7 @@ pub struct Diagnostic {
/// This should be roughly equivalent to the severity of Cairo diagnostics.
///
/// The appropriate action for each diagnostic kind will be taken by `Scarb`.
#[derive(Debug)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Severity {
/// An error has occurred.
///
Expand All @@ -189,7 +189,7 @@ pub enum Severity {
}

/// A set of diagnostics that arose during the computation.
#[derive(Debug)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Diagnostics(Vec<Diagnostic>);

impl Diagnostic {
Expand Down Expand Up @@ -252,6 +252,18 @@ impl IntoIterator for Diagnostics {
}
}

impl FromIterator<Diagnostic> for Diagnostics {
fn from_iter<T: IntoIterator<Item = Diagnostic>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}

impl Extend<Diagnostic> for Diagnostics {
fn extend<T: IntoIterator<Item = Diagnostic>>(&mut self, iter: T) {
self.0.extend(iter);
}
}

impl ProcMacroResult {
/// Create new [`ProcMacroResult`], empty diagnostics set.
pub fn new(token_stream: TokenStream) -> Self {
Expand Down

0 comments on commit cf698d4

Please sign in to comment.