supa_mdx_lint/
app_error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt::Display;

use thiserror::Error;

#[derive(Error, Debug)]
pub(crate) enum AppError {
    #[error("File system error encountered when {0}: {1}")]
    FileSystemError(String, #[source] std::io::Error),
}

#[derive(Error, Debug)]
pub(crate) enum ParseError {
    #[error("Lint time configuration comments must have a rule: found only \"{0}\"")]
    ConfigurationCommentMissingRule(String),
    #[error("Position is required, but underlying node has no position: {0}")]
    MissingPosition(String),
    #[error("Unmatched configuration pair - {0}: [Row {1}]")]
    UnmatchedConfigurationPair(
        String,
        /// Start row (1-indexed)
        usize,
    ),
}

#[derive(Error, Debug)]
pub enum PublicError {
    #[error("Variant not found: {0}")]
    VariantNotFound(String),
}

#[derive(Error, Debug, Default)]
pub(crate) struct MultiError(Vec<Box<dyn std::error::Error>>);

impl MultiError {
    pub(crate) fn add_err(&mut self, error: Box<dyn std::error::Error>) {
        self.0.push(error);
    }
}

impl Display for MultiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (i, err) in self.0.iter().enumerate() {
            writeln!(f, "\nError {} of {}: {}", i + 1, self.0.len(), err)?;
        }
        Ok(())
    }
}

#[derive(Debug, Default)]
#[must_use = "The result may contain an error, which it is recommended to check"]
pub(crate) struct ResultBoth<T, E: std::error::Error> {
    res: T,
    err: Option<E>,
}

impl<T, E: std::error::Error> ResultBoth<T, E> {
    pub(crate) fn new(res: T, err: Option<E>) -> Self {
        Self { res, err }
    }

    #[allow(unused)]
    pub(crate) fn has_err(&self) -> bool {
        self.err.is_some()
    }

    pub(crate) fn split(self) -> (T, Option<E>) {
        (self.res, self.err)
    }

    pub(crate) fn unwrap(self) -> T {
        self.res
    }
}