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
use syn::{Generics, PathArguments, Type, TypePath};
use darling::{usage::GenericsExt, Error, Result};
use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub struct TargetType {
has_lifetime: bool,
ident: Ident,
}
impl TargetType {
pub fn parse(ident: Ident, generics: Generics) -> Result<Self> {
if generics.declared_lifetimes().len() > 1 {
let mut accum = Error::accumulator();
accum.extend(generics.lifetimes().skip(1).map(|param| {
Error::custom(
"More than one lifetime parameter specified. Try removing this lifetime param.",
)
.with_span(param)
}));
accum.finish()?;
}
if !generics.declared_type_params().is_empty() {
let mut accum = Error::accumulator();
accum.extend(generics.type_params().map(|param| {
Error::custom(
"Target type must not be generic over any type. Try removing thus type param.",
)
.with_span(param)
}));
accum.finish()?;
}
let has_lifetime = !generics.declared_lifetimes().is_empty();
Ok(Self {
has_lifetime,
ident,
})
}
pub fn ident(&self) -> &Ident {
&self.ident
}
pub fn named_lifetime(&self) -> TokenStream {
if self.has_lifetime {
quote! {
<'a>
}
} else {
quote! {}
}
}
pub fn unnamed_lifetime(&self) -> TokenStream {
if self.has_lifetime {
quote! {
<'_>
}
} else {
quote! {}
}
}
}
pub fn extract_generic_type<'a>(ty: &'a Type, expected_container: &[&str]) -> Option<&'a Type> {
//TODO: rewrite
// If it is not `TypePath`, it is not possible to be `Option<T>`, return `None`
if let Type::Path(TypePath { qself: None, path }) = ty {
// We have limited the 5 ways to write `Option`, and we can see that after `Option`,
// there will be no `PathSegment` of the same level
// Therefore, we only need to take out the highest level `PathSegment` and splice it into a string
// for comparison with the analysis result
let segments_str = &path
.segments
.iter()
.map(|segment| segment.ident.to_string())
.collect::<Vec<_>>()
.join(":");
// Concatenate `PathSegment` into a string, compare and take out the `PathSegment` where `Option` is located
let option_segment = expected_container
.iter()
.find(|s| segments_str == *s)
.and_then(|_| path.segments.last());
let inner_type = option_segment
// Take out the generic parameters of the `PathSegment` where `Option` is located
// If it is not generic, it is not possible to be `Option<T>`, return `None`
// But this situation may not occur
.and_then(|path_seg| match &path_seg.arguments {
PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
args, ..
}) => args.first(),
_ => None,
})
// Take out the type information in the generic parameter
// If it is not a type, it is not possible to be `Option<T>`, return `None`
// But this situation may not occur
.and_then(|generic_arg| match generic_arg {
syn::GenericArgument::Type(ty) => Some(ty),
_ => None,
});
// Return `T` in `Option<T>`
return inner_type;
}
None
}