codeprism_lang_python/
error.rs

1//! Error types for Python parser
2
3use std::path::PathBuf;
4use thiserror::Error;
5
6/// Error type for Python parser
7#[derive(Error, Debug)]
8pub enum Error {
9    /// Parse error
10    #[error("Failed to parse {file}: {message}")]
11    ParseError { file: PathBuf, message: String },
12
13    /// Tree-sitter error
14    #[error("Tree-sitter error in {file}: {message}")]
15    TreeSitterError { file: PathBuf, message: String },
16
17    /// AST mapping error
18    #[error("AST mapping error in {file}: {message}")]
19    AstMappingError { file: PathBuf, message: String },
20
21    /// IO error
22    #[error("IO error: {0}")]
23    Io(#[from] std::io::Error),
24
25    /// JSON serialization error
26    #[error("JSON error: {0}")]
27    Json(#[from] serde_json::Error),
28
29    /// Generic error
30    #[error("Python parser error: {0}")]
31    Generic(String),
32}
33
34impl Error {
35    /// Create a parse error
36    pub fn parse(file: &std::path::Path, message: &str) -> Self {
37        Self::ParseError {
38            file: file.to_path_buf(),
39            message: message.to_string(),
40        }
41    }
42
43    /// Create a tree-sitter error
44    pub fn tree_sitter(file: &std::path::Path, message: &str) -> Self {
45        Self::TreeSitterError {
46            file: file.to_path_buf(),
47            message: message.to_string(),
48        }
49    }
50
51    /// Create an AST mapping error
52    pub fn ast_mapping(file: &std::path::Path, message: &str) -> Self {
53        Self::AstMappingError {
54            file: file.to_path_buf(),
55            message: message.to_string(),
56        }
57    }
58
59    /// Create a generic error
60    pub fn generic(message: &str) -> Self {
61        Self::Generic(message.to_string())
62    }
63}
64
65/// Result type for Python parser
66pub type Result<T> = std::result::Result<T, Error>;