Skip to main content

xidl_parser/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// Errors raised while turning IDL source into a typed AST.
5#[derive(Debug, Clone)]
6pub enum ParseError {
7    /// Free-form parse failure.
8    Message(String),
9    /// Source bytes that are not valid UTF-8.
10    Utf8Error(std::str::Utf8Error),
11    /// A tree-sitter node that did not match the expected grammar shape.
12    UnexpectedNode(String),
13    /// Failure reported by tree-sitter itself.
14    TreeSitterError(String),
15    /// Failure reported by API versioning analysis or projection.
16    Versioning(crate::versioning::VersioningError),
17}
18
19impl fmt::Display for ParseError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::Message(message) => f.write_str(message),
23            Self::Utf8Error(err) => write!(f, "{err}"),
24            Self::UnexpectedNode(node) => write!(f, "Unexpected node: {node}"),
25            Self::TreeSitterError(message) => write!(f, "TreeSitterError: {message}"),
26            Self::Versioning(error) => write!(f, "{error}"),
27        }
28    }
29}
30
31impl Error for ParseError {
32    fn source(&self) -> Option<&(dyn Error + 'static)> {
33        match self {
34            Self::Utf8Error(err) => Some(err),
35            Self::Versioning(error) => Some(error),
36            _ => None,
37        }
38    }
39}
40
41impl From<std::str::Utf8Error> for ParseError {
42    fn from(err: std::str::Utf8Error) -> Self {
43        Self::Utf8Error(err)
44    }
45}
46
47impl From<crate::versioning::VersioningError> for ParseError {
48    fn from(err: crate::versioning::VersioningError) -> Self {
49        Self::Versioning(err)
50    }
51}
52
53/// Result type for parser operations.
54pub type ParserResult<T> = Result<T, ParseError>;