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