Skip to main content

malt_index/
error.rs

1//! Errors of the index. Parsing MATLAB syntax never fails: tree-sitter recovers and the
2//! parts that are not understood are skipped. A file that is not a readable live script
3//! (a `.mlx` that is not a zip archive, or has no document) is a [`IndexError::Format`].
4
5use std::path::PathBuf;
6
7use thiserror::Error;
8
9/// An error from reading the path or a file.
10#[derive(Debug, Error, Clone, PartialEq, Eq)]
11pub enum IndexError {
12    /// A file or folder could not be read.
13    #[error("cannot read {path}: {message}")]
14    Io {
15        /// The path that failed.
16        path: PathBuf,
17        /// The underlying error, as text (keeps the error cloneable for the cache).
18        message: String,
19    },
20    /// A file's contents are not in the format its extension promises (a `.mlx` that is
21    /// not a zip archive, or one without `matlab/document.xml`).
22    #[error("{path}: {message}")]
23    Format {
24        /// The file.
25        path: PathBuf,
26        /// What is wrong with it.
27        message: String,
28    },
29    /// The identifier has an empty part (`pkg..fn` or an empty string).
30    #[error("invalid identifier {0:?}")]
31    InvalidIdentifier(String),
32    /// The superclasses of a class lead back to the class (`A < B`, `B < A`).
33    #[error("cyclic superclasses: {0}")]
34    CyclicBase(String),
35    /// The parser could not be set up (the grammar version does not match).
36    #[error("tree-sitter could not load the MATLAB grammar: {0}")]
37    Grammar(String),
38    /// The `.malt.toml` is not valid TOML, or its `[index]` table is not valid.
39    #[error("invalid .malt.toml: {0}")]
40    Config(String),
41}
42
43impl IndexError {
44    pub(crate) fn io(path: &std::path::Path, err: &std::io::Error) -> Self {
45        IndexError::Io {
46            path: path.to_path_buf(),
47            message: err.to_string(),
48        }
49    }
50}