1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use proc_macro2::TokenStream;
use std::fmt;
use syn::export::Span;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
    InvalidFormat,
    Multiple,
    NonStruct,
    UnexpectedMode,
    UnexpectedParam,
    Bullet,
    AlreadyDefined,
    ParseError,
    MissingParam(&'static str),
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ErrorKind::InvalidFormat => write!(fmt, "Invalid annotation format."),
            ErrorKind::Multiple => write!(fmt, "Multiple annotations per field."),
            ErrorKind::NonStruct => write!(
                fmt,
                "ImGuiExt macro is only supported for structs with named fields."
            ),
            ErrorKind::UnexpectedMode => write!(fmt, "Unexpected annotation."),
            ErrorKind::UnexpectedParam => write!(fmt, "Unexpected parameter."),
            ErrorKind::Bullet => write!(
                fmt,
                "Multiple nested annotations inside of a bullet list element."
            ),
            ErrorKind::AlreadyDefined => write!(fmt, "Field is defined already."),
            ErrorKind::ParseError => write!(fmt, "String parsing error."),
            ErrorKind::MissingParam(p) => write!(fmt, "Parameter `{}` missing.", p),
        }
    }
}

#[derive(Debug)]
pub struct Error {
    kind: ErrorKind,
    span: Span,
}

impl From<Error> for syn::Error {
    fn from(err: Error) -> Self {
        syn::Error::new(err.span, err.kind)
    }
}

impl Error {
    pub fn new(kind: ErrorKind, span: Span) -> Self {
        Self { kind, span }
    }

    pub fn to_compile_error(&self) -> TokenStream {
        syn::Error::new(self.span.clone(), self.kind).to_compile_error()
    }

    pub fn missing_param(span: Span, name: &'static str) -> Self {
        Self {
            kind: ErrorKind::MissingParam(name),
            span,
        }
    }

    pub fn invalid_format(span: Span) -> Self {
        Self {
            kind: ErrorKind::InvalidFormat,
            span,
        }
    }

    /// Multiple annotations per field.
    pub fn multiple(span: Span) -> Self {
        Self {
            kind: ErrorKind::Multiple,
            span,
        }
    }

    /// No support for anything other that structs with names fields
    pub fn non_struct(span: Span) -> Self {
        Self {
            kind: ErrorKind::NonStruct,
            span,
        }
    }

    /// Unexpected annotation mode.
    pub fn unexpected_mode(span: Span) -> Self {
        Self {
            kind: ErrorKind::UnexpectedMode,
            span,
        }
    }

    pub fn unexpected_param(span: Span) -> Self {
        Self {
            kind: ErrorKind::UnexpectedParam,
            span,
        }
    }

    pub fn bullet(span: Span) -> Self {
        Self {
            kind: ErrorKind::Bullet,
            span,
        }
    }

    /// Annotation param defined already
    pub fn already_defined(span: Span) -> Self {
        Self {
            kind: ErrorKind::AlreadyDefined,
            span,
        }
    }

    pub fn parsing_error(span: Span) -> Self {
        Self {
            kind: ErrorKind::ParseError,
            span,
        }
    }
}