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 new(inner: T, diagnostic: Vec<Diagnostic>) -> Self {
13    Self { inner, diagnostic }
14  }
15
16  pub fn diagnostics(&self) -> &Vec<Diagnostic> {
17    &self.diagnostic
18  }
19
20  pub fn take_inner(self) -> T {
21    self.inner
22  }
23
24  pub fn split_into_parts(self) -> (T, Vec<Diagnostic>) {
25    (self.inner, self.diagnostic)
26  }
27
28  pub fn get(&self) -> &T {
29    &self.inner
30  }
31}
32
33impl<T: Clone + std::fmt::Debug> Clone for TWithDiagnosticArray<T> {
34  fn clone(&self) -> Self {
35    Self {
36      inner: self.inner.clone(),
37      diagnostic: self.diagnostic.clone(),
38    }
39  }
40}
41
42// Helper trait to make `TWithDiagnosticArray` conversion more easily.
43pub trait IntoTWithDiagnosticArray {
44  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
45  where
46    Self: Sized + std::fmt::Debug;
47
48  fn with_empty_diagnostic(self) -> TWithDiagnosticArray<Self>
49  where
50    Self: Sized + std::fmt::Debug,
51  {
52    TWithDiagnosticArray {
53      inner: self,
54      diagnostic: vec![],
55    }
56  }
57}
58
59impl<T: Sized + std::fmt::Debug> IntoTWithDiagnosticArray for T {
60  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
61  where
62    Self: Sized + std::fmt::Debug,
63  {
64    TWithDiagnosticArray {
65      inner: self,
66      diagnostic,
67    }
68  }
69}