rspack_error/
lib.rs

1#![feature(let_chains)]
2#![feature(anonymous_lifetime_in_impl_trait)]
3
4mod catch_unwind;
5mod convert;
6mod diagnostic;
7mod error;
8mod ext;
9pub(crate) mod graphical;
10pub(crate) mod miette_helpers;
11pub use catch_unwind::*;
12pub use diagnostic::*;
13pub use error::*;
14pub use ext::*;
15pub mod emitter;
16pub use convert::*;
17
18mod macros;
19
20pub use miette;
21pub use thiserror;
22
23pub type Error = miette::Error;
24
25pub type Result<T, E = miette::Error> = std::result::Result<T, E>;
26
27/// A helper struct for change logic from
28/// return something to something with diagnostics array
29#[derive(Debug)]
30pub struct TWithDiagnosticArray<T: std::fmt::Debug> {
31  pub inner: T,
32  pub diagnostic: Vec<Diagnostic>,
33}
34
35impl<T: std::fmt::Debug> TWithDiagnosticArray<T> {
36  pub fn new(inner: T, diagnostic: Vec<Diagnostic>) -> Self {
37    Self { inner, diagnostic }
38  }
39
40  pub fn diagnostics(&self) -> &Vec<Diagnostic> {
41    &self.diagnostic
42  }
43
44  pub fn take_inner(self) -> T {
45    self.inner
46  }
47
48  pub fn split_into_parts(self) -> (T, Vec<Diagnostic>) {
49    (self.inner, self.diagnostic)
50  }
51
52  pub fn get(&self) -> &T {
53    &self.inner
54  }
55}
56
57impl<T: Clone + std::fmt::Debug> Clone for TWithDiagnosticArray<T> {
58  fn clone(&self) -> Self {
59    Self {
60      inner: self.inner.clone(),
61      diagnostic: self.diagnostic.clone(),
62    }
63  }
64}
65
66// Helper trait to make `TWithDiagnosticArray` conversion more easily.
67pub trait IntoTWithDiagnosticArray {
68  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
69  where
70    Self: Sized + std::fmt::Debug;
71
72  fn with_empty_diagnostic(self) -> TWithDiagnosticArray<Self>
73  where
74    Self: Sized + std::fmt::Debug,
75  {
76    TWithDiagnosticArray {
77      inner: self,
78      diagnostic: vec![],
79    }
80  }
81}
82
83impl<T: Sized + std::fmt::Debug> IntoTWithDiagnosticArray for T {
84  fn with_diagnostic(self, diagnostic: Vec<Diagnostic>) -> TWithDiagnosticArray<Self>
85  where
86    Self: Sized + std::fmt::Debug,
87  {
88    TWithDiagnosticArray {
89      inner: self,
90      diagnostic,
91    }
92  }
93}
94
95#[doc(hidden)]
96pub mod __private {
97  pub use core::result::Result::Err;
98
99  pub use miette::{miette, Severity};
100
101  pub use crate::{diagnostic::Severity as RspackSeverity, error, error::InternalError};
102}