use super::attr::Shape;
use proc_macro2::Span;
use syn::{Error, Ident};
#[derive(Debug, thiserror::Error)]
pub(crate) enum ParseError {
#[error("Enums are not supported")]
EnumUnsupported(Span),
#[error("Unions are not supported")]
UnionUnsupported(Span),
#[error("Unnamed structs are not supported")]
UnnamedStructUnsupported(Span),
#[error("Union structs are not supported")]
UnitStructUnsupported(Span),
#[error("Expected struct name '{name}' to end with one of: {suffixes}")]
UnknownStructSuffix { name: String, suffixes: String, span: Span },
#[error("Unknown attribute: '{ident}'")]
UnknownAttr { ident: Ident },
#[error("Invalid attribute expression type")]
InvalidAttrExpr { span: Span },
#[error("Expected string expr value, e.g. `#[config({ident} = \"..\")]`")]
ExpectedStrExpr { ident: Ident, span: Span },
#[error("Attribute '{ident}' specified multiple times")]
DuplicateAttr { ident: Ident },
#[error("Attribute '{ident}' cannot be used with '{other}'")]
IncompatibleAttrs { ident: Ident, other: Ident},
#[error("Attribute '{ident}' is automatically implied by '{other}'")]
ImplicitAttr { ident: Ident, other: Ident},
#[error("Unexpected shape {shape:#?} for attribute '{ident}'")]
InvalidAttrShape { ident: Ident, shape: Shape },
#[error("Missing attribute '{missing}', which is required while using '{present}'")]
MissingAttrDep { present: Ident, missing: &'static str },
#[error("Attribute '{ident}' cannot be used with Option types")]
AttrOptionUnsupported { ident: Ident },
#[error("`{ident}` is missing required `#[config(..)]` attribute")]
MissingRequiredAttrHelper { ident: Ident },
#[error("Missing required attribute: '{name}'")]
MissingRequiredAttr { name: &'static str, span: Span },
}
impl ParseError {
pub(crate) fn span(self) -> Span {
match self {
Self::EnumUnsupported(span) |
Self::UnionUnsupported(span) |
Self::UnnamedStructUnsupported(span) |
Self::UnitStructUnsupported(span) |
Self::UnknownStructSuffix { span, .. } |
Self::MissingRequiredAttr { span, .. } |
Self::ExpectedStrExpr { span, .. } |
Self::InvalidAttrExpr { span, .. } => span,
Self::UnknownAttr { ident } |
Self::IncompatibleAttrs { ident, .. } |
Self::ImplicitAttr { ident, .. } |
Self::DuplicateAttr { ident, .. } |
Self::InvalidAttrShape { ident, .. } |
Self::MissingRequiredAttrHelper { ident, .. } |
Self::AttrOptionUnsupported { ident } => ident.span(),
Self::MissingAttrDep { present, .. } => present.span(),
}
}
pub(crate) fn error(self) -> Error {
self.into()
}
}
impl From<ParseError> for Error {
fn from(error: ParseError) -> Self {
let msg = error.to_string();
Self::new(error.span(), msg)
}
}