arbor_core/error.rs
1//! Error types for the parsing module.
2//!
3//! We keep errors simple and actionable. Each variant tells you
4//! exactly what went wrong and (usually) how to fix it.
5
6use std::path::PathBuf;
7use thiserror::Error;
8
9/// Convenience type for functions that can fail during parsing.
10pub type Result<T> = std::result::Result<T, ParseError>;
11
12/// Things that can go wrong when parsing source files.
13#[derive(Error, Debug)]
14pub enum ParseError {
15 /// Couldn't read the file from disk.
16 #[error("failed to read file '{path}': {source}")]
17 IoError {
18 path: PathBuf,
19 #[source]
20 source: std::io::Error,
21 },
22
23 /// File extension doesn't map to any supported language.
24 #[error("unsupported language for file '{0}'")]
25 UnsupportedLanguage(PathBuf),
26
27 /// Tree-sitter failed to parse the source. Usually means
28 /// the file has syntax errors or the parser hit an edge case.
29 #[error("parser error: {0}")]
30 ParserError(String),
31
32 /// Tree-sitter query compilation failed. Usually means
33 /// the query pattern is invalid for the language grammar.
34 #[error("query error: {0}")]
35 QueryError(String),
36
37 /// The file exists but is empty. Not really an error,
38 /// but we surface it so callers can handle it gracefully.
39 #[error("file is empty: '{0}'")]
40 EmptyFile(PathBuf),
41}
42
43impl ParseError {
44 /// Creates an IO error with the path for context.
45 pub fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
46 Self::IoError {
47 path: path.into(),
48 source,
49 }
50 }
51}