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
use super::error::{Error, Result};
use proc_macro2::TokenStream;
use syn::{
Attribute, Data, DataEnum, DataStruct, DataUnion, DeriveInput, Generics, Ident, Visibility,
};
pub trait FromDeriveInput: Sized {
fn from_derive_input(input: &DeriveInput) -> Result<Self> {
match &input.data {
Data::Union(d) => {
Self::from_union(&input.attrs, &input.vis, &input.ident, &input.generics, d)
}
Data::Enum(d) => Self::from_enum(&input.attrs, &input.vis, &input.ident, &input.generics, d),
Data::Struct(d) => {
Self::from_struct(&input.attrs, &input.vis, &input.ident, &input.generics, d)
}
}
}
fn from_union(
attrs: &Vec<Attribute>,
vis: &Visibility,
ident: &Ident,
generics: &Generics,
input: &DataUnion,
) -> Result<Self> {
Err(Error::unsupported_shape("union"))
}
fn from_enum(
attrs: &Vec<Attribute>,
vis: &Visibility,
ident: &Ident,
generics: &Generics,
input: &DataEnum,
) -> Result<Self> {
Err(Error::unsupported_shape("enum"))
}
fn from_struct(
attrs: &Vec<Attribute>,
vis: &Visibility,
ident: &Ident,
generics: &Generics,
input: &DataStruct,
) -> Result<Self> {
Err(Error::unsupported_shape("struct"))
}
fn parse(input: TokenStream) -> Result<Self> {
let input = syn::parse2(input)?;
Self::from_derive_input(&input)
}
}
impl FromDeriveInput for () {
fn from_derive_input(_: &DeriveInput) -> Result<Self> {
Ok(())
}
}
impl FromDeriveInput for DeriveInput {
fn from_derive_input(input: &DeriveInput) -> Result<Self> {
Ok(input.clone())
}
}