Skip to main content

type_sitter_gen/
error.rs

1use proc_macro2::{Span, TokenStream};
2use std::fmt::{Display, Formatter};
3use std::process::ExitStatus;
4use tree_sitter::QueryError;
5
6#[derive(Debug)]
7pub enum Error {
8    IO(std::io::Error),
9    ParseNodeTypes(serde_json::Error),
10    BuildDylibFailed(cc::Error),
11    MissingDylib,
12    LoadDylibFailed(libloading::Error),
13    LoadDylibSymbolFailed(libloading::Error),
14    CorruptDylib,
15    IncompatibleLanguageVersion {
16        version: usize,
17    },
18    LinkDylibUnsupported,
19    LinkDylibCmdFailed(std::io::Error),
20    LinkDylibFailed {
21        exit_status: ExitStatus,
22    },
23    IllegalTSLanguageSymbolName,
24    ParseQuery(QueryError),
25    IllegalIdentifier {
26        type_desc: String,
27        name: String,
28        source: syn::Error,
29    },
30}
31
32impl Error {
33    pub fn into_syn(self) -> syn::Error {
34        syn::Error::new(Span::call_site(), self)
35    }
36
37    pub fn to_compile_error(self) -> TokenStream {
38        self.into_syn().to_compile_error()
39    }
40}
41
42impl From<std::io::Error> for Error {
43    fn from(e: std::io::Error) -> Self {
44        Error::IO(e)
45    }
46}
47
48impl From<serde_json::Error> for Error {
49    fn from(e: serde_json::Error) -> Self {
50        Error::ParseNodeTypes(e)
51    }
52}
53
54impl From<cc::Error> for Error {
55    fn from(e: cc::Error) -> Self {
56        Error::BuildDylibFailed(e)
57    }
58}
59
60impl From<QueryError> for Error {
61    fn from(e: QueryError) -> Self {
62        Error::ParseQuery(e)
63    }
64}
65
66impl Into<syn::Error> for Error {
67    fn into(self) -> syn::Error {
68        self.into_syn()
69    }
70}
71
72impl Display for Error {
73    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74        match self {
75            Error::IO(e) => write!(f, "IO error: {}", e),
76            Error::ParseNodeTypes(e) => write!(f, "Error parsing node-types: {}", e),
77            Error::BuildDylibFailed(e) => write!(f, "couldn't build tree-sitter language dylib: {}", e),
78            Error::MissingDylib => write!(f, "couldn't find tree-sitter language dylib (internal error or fs weirdness)"),
79            Error::LoadDylibFailed(e) => write!(f, "couldn't load tree-sitter language dylib: {}", e),
80            Error::LoadDylibSymbolFailed(e) => write!(f, "couldn't load tree-sitter language dylib symbol: {}", e),
81            Error::CorruptDylib => write!(f, "corrupt tree-sitter language dylib (or we read it wrong, because accessing trying to dereference the language caused a segfault)"),
82            Error::IncompatibleLanguageVersion { version } => write!(
83                f,
84                "incompatible tree-sitter language version: {} (must be within {}..={})",
85                version,
86                tree_sitter::MIN_COMPATIBLE_LANGUAGE_VERSION,
87                tree_sitter::LANGUAGE_VERSION
88            ),
89            Error::LinkDylibUnsupported => write!(f, "dynamic linking isn't supported on this platform, use macOS, Unix, Windows, or file a PR"),
90            Error::LinkDylibCmdFailed(e) => write!(f, "couldn't link tree-sitter language dylib: {}", e),
91            Error::LinkDylibFailed { exit_status} => write!(f, "couldn't link tree-sitter language dylib: exit code {}", exit_status),
92            Error::IllegalTSLanguageSymbolName => write!(f, "inferred language symbol name is not a valid UTF-8 string"),
93            Error::ParseQuery(e) => write!(f, "Error parsing query: {}", e),
94            Error::IllegalIdentifier { type_desc, name, source } => write!(f, "illegal identifier ({}): `{}`\n{}", type_desc, name, source)
95        }
96    }
97}
98
99impl std::error::Error for Error {
100    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
101        match self {
102            Error::IO(e) => Some(e),
103            Error::ParseNodeTypes(e) => Some(e),
104            Error::BuildDylibFailed(e) => Some(e),
105            Error::MissingDylib => None,
106            Error::LoadDylibFailed(e) => Some(e),
107            Error::LoadDylibSymbolFailed(e) => Some(e),
108            Error::CorruptDylib => None,
109            Error::IncompatibleLanguageVersion { .. } => None,
110            Error::LinkDylibUnsupported => None,
111            Error::LinkDylibCmdFailed(e) => Some(e),
112            Error::LinkDylibFailed { .. } => None,
113            Error::IllegalTSLanguageSymbolName => None,
114            Error::ParseQuery(e) => Some(e),
115            Error::IllegalIdentifier { source, .. } => Some(source),
116        }
117    }
118}