Skip to main content

rspack_error/
diagnostic_array.rs

1use crate::diagnostic::Diagnostic;
2
3/// A helper struct for change logic from
4/// return something to something with diagnostics array
5#[derive(Debug)]
6pub struct TWithDiagnosticArray<T: std::fmt::Debug> {
7  pub inner: T,
8  pub diagnostic: Vec<Diagnostic>,
9}
10
11impl<T: std::fmt::Debug> TWithDiagnosticArray<T> {
12  pub fn split_into_parts(self) -> (T, Vec<Diagnostic>) {
13    (self.inner, self.diagnostic)
14  }
15}
16
17impl<T: Clone + std::fmt::Debug> Clone for TWithDiagnosticArray<T> {
18  fn clone(&self) -> Self {
19    Self {
20      inner: self.inner.clone(),
21      diagnostic: self.diagnostic.clone(),
22    }
23  }
24}
25
26// Helper trait to make `TWithDiagnosticArray` conversion more easily.
27pub trait IntoTWithDiagnosticArray {
28  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
29  where
30    Self: Sized + std::fmt::Debug;
31
32  fn with_empty_diagnostic(self) -> TWithDiagnosticArray<Self>
33  where
34    Self: Sized + std::fmt::Debug,
35  {
36    TWithDiagnosticArray {
37      inner: self,
38      diagnostic: vec![],
39    }
40  }
41}
42
43impl<T: Sized + std::fmt::Debug> IntoTWithDiagnosticArray for T {
44  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
45  where
46    Self: Sized + std::fmt::Debug,
47  {
48    TWithDiagnosticArray {
49      inner: self,
50      diagnostic,
51    }
52  }
53}