Skip to main content

mq_lang/module/
error.rs

1use thiserror::Error;
2
3use crate::Token;
4use crate::error::syntax::SyntaxError;
5use std::borrow::Cow;
6
7/// Errors that can occur while loading or resolving mq modules.
8#[derive(Debug, PartialEq, Error)]
9pub enum ModuleError {
10    #[error("Module `{0}` is already loaded")]
11    AlreadyLoaded(Cow<'static, str>),
12    #[error("Module `{0}` not found")]
13    NotFound(Cow<'static, str>),
14    #[error("IO error: {0}")]
15    IOError(Cow<'static, str>),
16    #[error(transparent)]
17    SyntaxError(#[from] SyntaxError),
18    #[error("Invalid module, expected IDENT or BINDING")]
19    InvalidModule,
20    /// HTTP imports are only permitted at the top level; modules may not fetch remote dependencies.
21    #[cfg(feature = "http-import")]
22    #[error(
23        "HTTP import of `{0}` is not allowed inside an imported module; HTTP imports are only permitted at the top level"
24    )]
25    HttpImportNotAllowed(Cow<'static, str>),
26}
27
28impl ModuleError {
29    /// Returns the token associated with a syntax error, if any.
30    #[cold]
31    pub fn token(&self) -> Option<&Token> {
32        match self {
33            ModuleError::AlreadyLoaded(_) => None,
34            ModuleError::NotFound(_) => None,
35            ModuleError::IOError(_) => None,
36            ModuleError::SyntaxError(err) => err.token(),
37            ModuleError::InvalidModule => None,
38            #[cfg(feature = "http-import")]
39            ModuleError::HttpImportNotAllowed(_) => None,
40        }
41    }
42}