1use proc_macro2::TokenStream;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum CodamaError {
6 #[error("{0}")]
7 Filesystem(#[from] std::io::Error),
8
9 #[error("Could not parse Cargo.toml files: {0}")]
10 Manifest(#[from] cargo_toml::Error),
11
12 #[error("{0}")]
13 Compilation(#[from] syn::Error),
14
15 #[error("Could not parse JSON files: {0}")]
16 Json(#[from] serde_json::Error),
17
18 #[error("Could not convert node from `{from}` into `{into}`")]
19 InvalidNodeConversion { from: String, into: String },
20
21 #[error("Unexpected node: expected `{expected}`, found `{actual}`")]
22 UnexpectedNode { expected: String, actual: String },
23
24 #[error("Node not found")]
25 NodeNotFound,
26
27 #[error("Invalid encoding: {0}")]
28 InvalidBytesEncoding(String),
29
30 #[error("Invalid number format: {0}")]
31 InvalidNumberFormat(String),
32
33 #[error("Invalid endian: {0}")]
34 InvalidEndian(String),
35
36 #[error("Invalid attribute, Expected {expected}, got {actual}")]
37 InvalidAttribute { expected: String, actual: String },
38
39 #[error("Invalid Codama directive, Expected {expected}, got {actual}")]
40 InvalidCodamaDirective { expected: String, actual: String },
41}
42
43pub type CodamaResult<T> = Result<T, CodamaError>;
44
45impl CodamaError {
46 pub fn to_compile_error(&self) -> TokenStream {
47 match self {
48 CodamaError::Compilation(error) => error.to_compile_error(),
49 _ => TokenStream::new(),
50 }
51 }
52
53 pub fn into_compile_error(self) -> TokenStream {
54 self.to_compile_error()
55 }
56}