supa_mdx_lint/
app_error.rs
1use std::fmt::Display;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub(crate) enum AppError {
7 #[error("File system error encountered when {0}: {1}")]
8 FileSystemError(String, #[source] std::io::Error),
9}
10
11#[derive(Error, Debug)]
12pub(crate) enum ParseError {
13 #[error("Lint time configuration comments must have a rule: found only \"{0}\"")]
14 ConfigurationCommentMissingRule(String),
15 #[error("Position is required, but underlying node has no position: {0}")]
16 MissingPosition(String),
17 #[error("Unmatched configuration pair - {0}: [Row {1}]")]
18 UnmatchedConfigurationPair(
19 String,
20 usize,
22 ),
23}
24
25#[non_exhaustive]
26#[derive(Error, Debug)]
27pub enum PublicError {
28 #[error("Variant not found: {0}")]
29 VariantNotFound(String),
30}
31
32#[derive(Error, Debug, Default)]
33pub(crate) struct MultiError(Vec<Box<dyn std::error::Error>>);
34
35impl MultiError {
36 pub(crate) fn add_err(&mut self, error: Box<dyn std::error::Error>) {
37 self.0.push(error);
38 }
39}
40
41impl Display for MultiError {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 for (i, err) in self.0.iter().enumerate() {
44 writeln!(f, "\nError {} of {}: {}", i + 1, self.0.len(), err)?;
45 }
46 Ok(())
47 }
48}
49
50#[derive(Debug, Default)]
51#[must_use = "The result may contain an error, which it is recommended to check"]
52pub(crate) struct ResultBoth<T, E: std::error::Error> {
53 res: T,
54 err: Option<E>,
55}
56
57impl<T, E: std::error::Error> ResultBoth<T, E> {
58 pub(crate) fn new(res: T, err: Option<E>) -> Self {
59 Self { res, err }
60 }
61
62 #[allow(unused)]
63 pub(crate) fn has_err(&self) -> bool {
64 self.err.is_some()
65 }
66
67 pub(crate) fn split(self) -> (T, Option<E>) {
68 (self.res, self.err)
69 }
70
71 pub(crate) fn unwrap(self) -> T {
72 self.res
73 }
74}