|
| 1 | +//! Error types for parsing. |
| 2 | +use itertools::Itertools as _; |
| 3 | +use proc_macro2::Span; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +#[derive(Debug)] |
| 7 | +/// Wrapper for a parse error which includes a span. |
| 8 | +pub struct ParseErrorWithSpan { |
| 9 | + /// Parse error |
| 10 | + error: ParseError, |
| 11 | + /// Span |
| 12 | + span: Span, |
| 13 | +} |
| 14 | + |
| 15 | +impl std::error::Error for ParseErrorWithSpan {} |
| 16 | + |
| 17 | +impl std::fmt::Display for ParseErrorWithSpan { |
| 18 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 19 | + write!( |
| 20 | + f, |
| 21 | + "{}, on line {} and column {}", |
| 22 | + self.error, |
| 23 | + self.span.start().line, |
| 24 | + self.span.start().column |
| 25 | + ) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Errors that can occur while parsing Rust source input. |
| 30 | +#[derive(Debug, Error)] |
| 31 | +#[allow(missing_docs)] |
| 32 | +pub enum ParseError { |
| 33 | + #[error("{0}")] |
| 34 | + SynError(#[from] syn::Error), |
| 35 | + #[error("Failed to parse a Rust type: {0}")] |
| 36 | + RustTypeParseError(#[from] RustTypeParseError), |
| 37 | + #[error("Unsupported language encountered: {0}")] |
| 38 | + UnsupportedLanguage(String), |
| 39 | + #[error("Unsupported type encountered: {0}")] |
| 40 | + UnsupportedType(String), |
| 41 | + #[error("Tuple structs with more than one field are currently unsupported")] |
| 42 | + ComplexTupleStruct, |
| 43 | + #[error("Multiple unnamed associated types are not currently supported")] |
| 44 | + MultipleUnnamedAssociatedTypes, |
| 45 | + #[error("The serde tag attribute is not supported for non-algebraic enums: {enum_ident}")] |
| 46 | + SerdeTagNotAllowed { enum_ident: String }, |
| 47 | + #[error("The serde content attribute is not supported for non-algebraic enums: {enum_ident}")] |
| 48 | + SerdeContentNotAllowed { enum_ident: String }, |
| 49 | + #[error("Serde tag attribute needs to be specified for algebraic enum {enum_ident}. e.g. #[serde(tag = \"type\", content = \"content\")]")] |
| 50 | + SerdeTagRequired { enum_ident: String }, |
| 51 | + #[error("Serde content attribute needs to be specified for algebraic enum {enum_ident}. e.g. #[serde(tag = \"type\", content = \"content\")]")] |
| 52 | + SerdeContentRequired { enum_ident: String }, |
| 53 | + #[error("The expression assigned to this constant variable is not a numeric literal")] |
| 54 | + RustConstExprInvalid, |
| 55 | + #[error("You cannot use typeshare on a constant that is not a numeric literal")] |
| 56 | + RustConstTypeInvalid, |
| 57 | + #[error("The serde flatten attribute is not currently supported")] |
| 58 | + SerdeFlattenNotAllowed, |
| 59 | + #[error("IO error: {0}")] |
| 60 | + IOError(String), |
| 61 | +} |
| 62 | + |
| 63 | +/// Parse error types that can capture a span and convert |
| 64 | +/// into the top level [ParseErrorWithSpan] type. |
| 65 | +pub trait WithSpan { |
| 66 | + /// Convert [Self] into a [`ParserErrorWithSpan`]. |
| 67 | + fn with_span(self, span: Span) -> ParseErrorWithSpan; |
| 68 | +} |
| 69 | + |
| 70 | +impl WithSpan for RustTypeParseError { |
| 71 | + fn with_span(self, span: Span) -> ParseErrorWithSpan { |
| 72 | + ParseError::RustTypeParseError(self).with_span(span) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl WithSpan for ParseError { |
| 77 | + fn with_span(self, span: Span) -> ParseErrorWithSpan { |
| 78 | + ParseErrorWithSpan { error: self, span } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Debug, Error)] |
| 83 | +/// Errors during file generation. |
| 84 | +pub enum GenerationError { |
| 85 | + /// The post generation step failed. |
| 86 | + #[error("Post generation failed: {0}")] |
| 87 | + PostGeneration(String), |
| 88 | +} |
| 89 | + |
| 90 | +#[derive(Debug, Error)] |
| 91 | +#[allow(missing_docs)] |
| 92 | +pub enum RustTypeParseError { |
| 93 | + #[error("Unsupported type: \"{}\"", .0.iter().join(","))] |
| 94 | + UnsupportedType(Vec<String>), |
| 95 | + #[error("Unexpected token when parsing type: `{0}`. This is an internal error, please ping a typeshare developer to resolve this problem.")] |
| 96 | + UnexpectedToken(String), |
| 97 | + #[error("Tuples are not allowed in typeshare types")] |
| 98 | + UnexpectedParameterizedTuple, |
| 99 | + #[error("Could not parse numeric literal")] |
| 100 | + NumericLiteral(syn::parse::Error), |
| 101 | +} |
0 commit comments