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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#[cfg(feature = "legacy")]
use macro_compose::{Collector, Lint};
#[cfg(feature = "legacy")]
use quote::ToTokens;
use std::convert::TryFrom;
#[cfg(feature = "legacy")]
use syn::Lit;
use syn::{parse_quote, Error, GenericArgument, Path, PathArguments};
#[derive(Clone, Copy, Debug)]
pub struct Type {
pub ty: Types,
pub optional: bool,
}
#[cfg(feature = "legacy")]
impl<'a> Lint<Option<&'a Lit>> for Type {
fn lint(&self, input: &Option<&'a Lit>, c: &mut Collector) {
let ty = match self.ty {
Types::Any => "anything",
Types::Flag => "nothing",
Types::Str => "string",
Types::ByteStr => "byte string",
Types::Byte => "byte",
Types::Char => "char",
Types::I32 => "i32",
Types::F32 => "f32",
Types::Bool => "bool",
};
match (input, self.ty) {
(Some(_), Types::Any)
| (None, Types::Flag)
| (Some(Lit::Str(_)), Types::Str)
| (Some(Lit::ByteStr(_)), Types::ByteStr)
| (Some(Lit::Byte(_)), Types::Byte)
| (Some(Lit::Char(_)), Types::Char)
| (Some(Lit::Int(_)), Types::I32)
| (Some(Lit::Float(_)), Types::F32)
| (Some(Lit::Bool(_)), Types::Bool) => {}
(None, _) if self.optional => {}
(Some(lit), _) => c.error(Error::new_spanned(
input,
format!("expected {}, got {}", ty, lit.to_token_stream()),
)),
(None, _) => c.error(Error::new_spanned(
input,
format!("expected {}, got nothing", ty,),
)),
}
}
}
#[derive(Clone, Copy, Debug)]
pub enum Types {
Any,
Flag,
Str,
ByteStr,
Byte,
Char,
I32,
F32,
Bool,
}
impl TryFrom<&syn::Type> for Type {
type Error = Error;
fn try_from(ty: &syn::Type) -> Result<Self, Self::Error> {
let byte_vec_path: Path = parse_quote!(Vec<u8>);
let error = || Err(Error::new_spanned(&ty, "unexpected type"));
match ty {
syn::Type::Path(p) => {
if p.path.is_ident("String") {
Ok(Type {
ty: Types::Str,
optional: false,
})
} else if p.path == byte_vec_path {
Ok(Type {
ty: Types::ByteStr,
optional: false,
})
} else if p.path.is_ident("u8") {
Ok(Type {
ty: Types::Byte,
optional: false,
})
} else if p.path.is_ident("char") {
Ok(Type {
ty: Types::Char,
optional: false,
})
} else if p.path.is_ident("i32") {
Ok(Type {
ty: Types::I32,
optional: false,
})
} else if p.path.is_ident("f32") {
Ok(Type {
ty: Types::F32,
optional: false,
})
} else if p.path.is_ident("bool") {
Ok(Type {
ty: Types::Bool,
optional: false,
})
} else {
if p.path.segments.len() == 1 {
let segment = p.path.segments.first().unwrap();
if segment.ident == "Option" {
if let PathArguments::AngleBracketed(args) = &segment.arguments {
if let Some(GenericArgument::Type(ty)) = args.args.first() {
return Type::try_from(ty).and_then(|ty| {
if ty.optional {
error()
} else {
Ok(Type {
ty: ty.ty,
optional: true,
})
}
});
}
}
}
}
error()
}
}
syn::Type::Tuple(t) if t.elems.is_empty() => Ok(Type {
ty: Types::Flag,
optional: false,
}),
_ => error(),
}
}
}