fervid_parser/
error.rs

1use swc_core::common::{Span, Spanned};
2
3#[derive(Debug)]
4pub struct ParseError {
5    pub kind: ParseErrorKind,
6    pub span: Span,
7}
8
9#[derive(Debug)]
10pub enum ParseErrorKind {
11    /// Malformed directive (e.g. `:`, `@`)
12    DirectiveSyntax,
13    /// More than one `<script>`
14    DuplicateScriptOptions,
15    /// More than one `<script setup>`
16    DuplicateScriptSetup,
17    /// More than one `<template>`
18    DuplicateTemplate,
19    /// More than one attribute with the same name on a root element
20    DuplicateAttribute,
21    /// Unclosed dynamic argument, e.g. `:[dynamic`
22    DynamicArgument,
23    /// Error while parsing EcmaScript/TypeScript
24    EcmaSyntaxError(Box<swc_ecma_parser::error::SyntaxError>),
25    /// Unrecoverable error while parsing HTML
26    InvalidHtml(Box<swc_html_parser::error::ErrorKind>),
27    /// Both `<template>` and `<script>` are missing
28    MissingTemplateOrScript,
29    /// `<script>`/`<style>` content was not Text
30    UnexpectedNonRawTextContent,
31    /// Language not supported
32    UnsupportedLang,
33}
34
35impl From<swc_ecma_parser::error::Error> for ParseError {
36    fn from(value: swc_ecma_parser::error::Error) -> ParseError {
37        let span = value.span();
38
39        ParseError {
40            kind: ParseErrorKind::EcmaSyntaxError(Box::new(value.into_kind())),
41            span,
42        }
43    }
44}
45
46impl std::fmt::Display for ParseErrorKind {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "{:?}", self)
49    }
50}
51
52impl std::fmt::Display for ParseError {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        write!(f, "{:?}", self)
55    }
56}
57
58impl Spanned for ParseError {
59    fn span(&self) -> Span {
60        self.span
61    }
62}