fervid_css/css/
error.rs

1use fervid_core::error::{Severity, SeverityLevel};
2use swc_core::common::{Spanned, Span};
3use swc_css_parser::error::{ErrorKind as ParseErrorKind, Error as ParseError};
4
5#[derive(Debug)]
6pub struct CssError {
7    pub span: Span,
8    pub kind: CssErrorKind
9}
10
11#[derive(Debug)]
12pub enum CssErrorKind {
13    ParseRecoverable(ParseErrorKind),
14    ParseUnrecoverable(ParseErrorKind),
15    ParseDeepRecoverable(ParseErrorKind),
16    ParseDeepUnrecoverable(ParseErrorKind),
17    // MinifyError(Error<MinifyErrorKind>),
18    // PrinterError(Error<PrinterErrorKind>),
19}
20
21impl CssError {
22    pub fn from_parse_error(from: ParseError, is_recoverable: bool, is_deep: bool) -> CssError {
23        let (span, kind) = *from.into_inner();
24
25        let kind = match (is_deep, is_recoverable) {
26            (true, true) => CssErrorKind::ParseDeepRecoverable(kind),
27            (true, false) => CssErrorKind::ParseDeepUnrecoverable(kind),
28            (false, true) => CssErrorKind::ParseRecoverable(kind),
29            (false, false) => CssErrorKind::ParseUnrecoverable(kind)
30        };
31
32        CssError {
33            span,
34            kind
35        }
36    }
37}
38
39impl Severity for CssError {
40    fn get_severity(&self) -> SeverityLevel {
41        match &self.kind {
42            CssErrorKind::ParseRecoverable(_) => SeverityLevel::RecoverableError,
43            CssErrorKind::ParseUnrecoverable(_) => SeverityLevel::UnrecoverableError,
44            CssErrorKind::ParseDeepRecoverable(_) => SeverityLevel::RecoverableError,
45            CssErrorKind::ParseDeepUnrecoverable(_) => SeverityLevel::UnrecoverableError,
46        }
47    }
48}
49
50impl Spanned for CssError {
51    fn span(&self) -> swc_core::common::Span {
52        self.span
53    }
54}