syntastica_core/
error.rs

1use std::convert::Infallible;
2
3use crate::ts_runtime::QueryError;
4use palette::rgb::FromHexError;
5use thiserror::Error;
6
7/// The main result type.
8///
9/// Uses [`Error`](enum@Error) for the error variant.
10pub type Result<T> = std::result::Result<T, Error>;
11
12// TODO: rethink the error type:
13// - do we really want to expose other errors publicly?
14// - should it be split into multiple types?
15// - are all variants required?
16// - should `Custom` maybe hold a `Box<dyn Error>` (or similar) instead
17// - use `Box<str>` or `Arc<str>` instead of `String`? (mutation isn't required)
18
19/// The main error type.
20#[derive(Debug, Error)]
21pub enum Error {
22    /// A [`Theme`](crate::theme::Theme) contained a link to a non-existent key.
23    ///
24    /// Contains the name of the non-existent key.
25    ///
26    /// Can occur when calling [`Theme::resolve_links`](crate::theme::Theme::resolve_links).
27    #[error("link to unknown key '{0}'")]
28    InvalidLink(String),
29
30    /// The requested language is not supported by the current language collection.
31    ///
32    /// Contains the language name which was requested.
33    #[error("unsupported language with name '{0}'")]
34    UnsupportedLanguage(String),
35
36    /// A [`LanguageSet`](crate::language_set::LanguageSet) did not provide queries for a
37    /// provided parser.
38    ///
39    /// Contains the name of the language which is missing queries.
40    #[error("missing queries for language '{0}'")]
41    MissingQueries(String),
42
43    /// A [`Theme`](crate::theme::Theme) contains a color with an invalid hex literal.
44    ///
45    /// Contains a [`palette::rgb::FromHexError`].
46    #[error(transparent)]
47    InvalidHex(#[from] FromHexError),
48
49    /// The provided queries were malformed or not applicable to the parser.
50    ///
51    /// Contains a [`QueryError`].
52    #[error(transparent)]
53    MalformedQueries(#[from] QueryError),
54
55    /// Highlighting failed, usually because of tree-sitter version mismatches.
56    ///
57    /// Contains a [`syntastica_highlight::Error`].
58    #[error(transparent)]
59    Highlight(#[from] syntastica_highlight::Error),
60
61    /// A custom error which may be returned by external crates if no other variant fits.
62    ///
63    /// Contains a string describing the error.
64    #[error("{0}")]
65    Custom(String),
66}
67
68impl From<Infallible> for Error {
69    fn from(_: Infallible) -> Self {
70        unreachable!("`Infallible` cannot be constructed")
71    }
72}