mysql_common_derive/
error.rs

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
use proc_macro2::Span;
use proc_macro_error2::{Diagnostic, Level};

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("expected a struct with a single unnamed field")]
    NotANewTypeStruct(Span),
    #[error("structs with named fields are not supported")]
    NamedFieldsNotSupported(Span),
    #[error("unit structs are not supported")]
    UnitStructsNotSupported(Span),
    #[error("structs with unnamed fields are not supported")]
    StructsWithUnnamedFieldsNotSupported(Span),
    #[error("unions are not supported")]
    UnionsNotSupported(Span),
    #[error("enums are not supported")]
    EnumsNotSupported(Span),
    #[error("non-unit variants are not supported")]
    NonUnitVariant(Span),
    #[error("unsupported discriminant")]
    UnsupportedDiscriminant(Span),
    #[error("add #[mysql(explicit_invalid)] attribute to allow")]
    ExplicitInvalid(Span),
    #[error("no suitable crate found, use #[mysql(crate = \"..\")] to specify the crate name")]
    NoCrateNameFound,
    #[error("multiple crates found, use #[mysql(crate = \"..\")] to specify the particular name")]
    MultipleCratesFound,
    #[error(transparent)]
    Syn(#[from] syn::Error),
    #[error(transparent)]
    Darling(#[from] darling::error::Error),
    #[error("conflicting attributes")]
    ConflictingsAttributes(Span, Span),
    #[error("representation won't fit into MySql integer")]
    UnsupportedRepresentation(Span),
    #[error("this attribute requires `{}` attribute", 0)]
    AttributeRequired(Span, &'static str),
}

impl From<Error> for Diagnostic {
    fn from(x: Error) -> Diagnostic {
        match x {
            Error::UnionsNotSupported(span)
            | Error::EnumsNotSupported(span)
            | Error::NonUnitVariant(span)
            | Error::UnsupportedDiscriminant(span)
            | Error::ExplicitInvalid(span)
            | Error::NotANewTypeStruct(span)
            | Error::NamedFieldsNotSupported(span)
            | Error::UnitStructsNotSupported(span)
            | Error::UnsupportedRepresentation(span)
            | Error::StructsWithUnnamedFieldsNotSupported(span) => {
                Diagnostic::spanned(span, Level::Error, format!("FromValue: {x}"))
            }
            Error::Syn(ref e) => {
                Diagnostic::spanned(e.span(), Level::Error, format!("FromValue: {x}"))
            }
            Error::Darling(ref e) => {
                Diagnostic::spanned(e.span(), Level::Error, format!("FromValue: {x}"))
            }
            Error::NoCrateNameFound => Diagnostic::new(Level::Error, format!("FromValue: {x}")),
            Error::MultipleCratesFound => Diagnostic::new(Level::Error, format!("FromValue: {x}")),
            Error::ConflictingsAttributes(s1, s2) => {
                Diagnostic::spanned(s1, Level::Error, format!("FromValue: {x}"))
                    .span_error(s2, "conflicting attribute".into())
            }
            Error::AttributeRequired(s, _) => {
                Diagnostic::spanned(s, Level::Error, format!("FromValue: {x}"))
            }
        }
    }
}