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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use proc_macro2::Span;
use syn::{Ident, LitStr, Result as ParseResult};
#[derive(Hash, Debug)]
pub enum Allowlist {
Unspecified,
All,
Specific(Vec<String>),
}
impl Allowlist {
pub(crate) fn push(&mut self, item: LitStr) -> ParseResult<()> {
match self {
Allowlist::Unspecified => {
*self = Allowlist::Specific(vec![item.value()]);
}
Allowlist::All => {
return Err(syn::Error::new(
item.span(),
"use either generate!/generate_pod! or generate_all!, not both.",
))
}
Allowlist::Specific(list) => list.push(item.value()),
};
Ok(())
}
pub(crate) fn set_all(&mut self, ident: &Ident) -> ParseResult<()> {
if matches!(self, Allowlist::Specific(..)) {
return Err(syn::Error::new(
ident.span(),
"use either generate!/generate_pod! or generate_all!, not both.",
));
}
*self = Allowlist::All;
Ok(())
}
}
impl Default for Allowlist {
fn default() -> Self {
Allowlist::Unspecified
}
}
#[derive(Default, Hash, Debug)]
pub(crate) struct TypeConfigInput {
pub(crate) pod_requests: Vec<String>,
pub(crate) allowlist: Allowlist,
pub(crate) blocklist: Vec<String>,
pub(crate) exclude_utilities: bool,
}
#[derive(Hash, Debug)]
pub struct TypeConfig(TypeConfigInput);
const UTILITIES: &[&str] = &["make_string"];
const NO_UTILITIES: &[&str] = &[];
impl TypeConfigInput {
pub(crate) fn into_type_config(self) -> ParseResult<TypeConfig> {
if matches!(self.allowlist, Allowlist::Unspecified) {
Err(syn::Error::new(
Span::call_site(),
"expected generate! or generate_all!",
))
} else {
Ok(TypeConfig(self))
}
}
}
impl TypeConfig {
pub fn get_pod_requests(&self) -> &[String] {
&self.0.pod_requests
}
pub fn exclude_utilities(&self) -> bool {
self.0.exclude_utilities
}
pub fn must_generate_list(&self) -> Box<dyn Iterator<Item = String> + '_> {
if let Allowlist::Specific(items) = &self.0.allowlist {
Box::new(items.iter().chain(self.0.pod_requests.iter()).cloned())
} else {
Box::new(self.0.pod_requests.iter().cloned())
}
}
pub fn bindgen_allowlist(&self) -> Option<Box<dyn Iterator<Item = String> + '_>> {
match &self.0.allowlist {
Allowlist::All => None,
Allowlist::Specific(items) => Some(Box::new(
items
.iter()
.chain(self.0.pod_requests.iter())
.cloned()
.chain(self.active_utilities().iter().map(|s| s.to_string())),
)),
Allowlist::Unspecified => unreachable!(),
}
}
fn active_utilities(&self) -> &'static [&'static str] {
if self.0.exclude_utilities {
NO_UTILITIES
} else {
UTILITIES
}
}
pub fn is_on_allowlist(&self, cpp_name: &str) -> bool {
match self.bindgen_allowlist() {
None => true,
Some(mut items) => {
items.any(|item| item == cpp_name)
|| self.active_utilities().iter().any(|item| *item == cpp_name)
}
}
}
pub fn is_on_blocklist(&self, cpp_name: &str) -> bool {
self.0.blocklist.contains(&cpp_name.to_string())
}
pub fn get_blocklist(&self) -> impl Iterator<Item = &String> {
self.0.blocklist.iter()
}
pub fn new_for_test() -> Self {
let mut input = TypeConfigInput::default();
input.allowlist = Allowlist::All;
input.into_type_config().unwrap()
}
}