rspack_error/
diagnosable.rs

1use std::borrow::Cow;
2
3use crate::diagnostic::Diagnostic;
4
5pub trait Diagnosable {
6  fn add_diagnostic(&mut self, _diagnostic: Diagnostic);
7
8  fn add_diagnostics(&mut self, _diagnostics: Vec<Diagnostic>);
9
10  fn diagnostics(&self) -> Cow<'_, [Diagnostic]>;
11
12  fn first_error(&self) -> Option<Cow<'_, Diagnostic>> {
13    match self.diagnostics() {
14      Cow::Borrowed(diagnostics) => diagnostics.iter().find(|d| d.is_error()).map(Cow::Borrowed),
15      Cow::Owned(diagnostics) => diagnostics
16        .into_iter()
17        .find(|d| d.is_error())
18        .map(Cow::Owned),
19    }
20  }
21}
22
23#[macro_export]
24macro_rules! impl_empty_diagnosable_trait {
25  ($ty:ty) => {
26    impl $crate::Diagnosable for $ty {
27      fn add_diagnostic(&mut self, _diagnostic: $crate::Diagnostic) {
28        unimplemented!(
29          "`<{ty} as Diagnosable>::add_diagnostic` is not implemented",
30          ty = stringify!($ty)
31        )
32      }
33      fn add_diagnostics(&mut self, _diagnostics: Vec<$crate::Diagnostic>) {
34        unimplemented!(
35          "`<{ty} as Diagnosable>::add_diagnostics` is not implemented",
36          ty = stringify!($ty)
37        )
38      }
39      fn diagnostics(&self) -> std::borrow::Cow<'_, [$crate::Diagnostic]> {
40        std::borrow::Cow::Owned(vec![])
41      }
42    }
43  };
44}