-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathconfig.rs
386 lines (340 loc) · 10.8 KB
/
config.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use std::collections::{BTreeMap, HashMap, HashSet};
use std::error::Error;
use std::fmt;
use std::fs;
use std::path::Path;
use std::str::FromStr;
use heck::ToTrainCase;
use semver::Version;
use serde::{de, Deserialize, Deserializer};
use crate::stmt::{Counterpart, Derives};
use crate::{ItemIdentifier, Location};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Config {
pub libraries: BTreeMap<String, LibraryConfig>,
pub system: LibraryConfig,
}
fn uses_system_config(library_name: &str) -> bool {
matches!(
library_name,
"System" | "bitflags" | "block2" | "libc" | "objc2"
)
}
impl Config {
pub fn library(&self, library_name: &str) -> &LibraryConfig {
if uses_system_config(library_name) {
&self.system
} else {
self.libraries.get(library_name).unwrap_or_else(|| {
error!("tried to get library config from {library_name:?}");
&self.system
})
}
}
pub fn library_from_crate(&self, krate: &str) -> &LibraryConfig {
if uses_system_config(krate) {
&self.system
} else {
self.libraries
.values()
.find(|lib| lib.krate == krate)
.unwrap_or_else(|| {
error!("tried to get library config from krate {krate:?}");
&self.system
})
}
}
pub fn replace_protocol_name(&self, id: ItemIdentifier) -> ItemIdentifier {
let library_config = self.library(id.library_name());
id.map_name(|name| {
library_config
.protocol_data
.get(&name)
.and_then(|data| data.renamed.clone())
.unwrap_or(name)
})
}
}
fn get_version<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Version>, D::Error> {
struct VersionVisitor;
impl de::Visitor<'_> for VersionVisitor {
type Value = Option<Version>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a version string")
}
fn visit_none<E>(self) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(None)
}
fn visit_borrowed_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(Some(
lenient_semver_parser::parse::<Version>(v).map_err(de::Error::custom)?,
))
}
}
deserializer.deserialize_str(VersionVisitor)
}
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ExternalData {
pub module: Location,
#[serde(rename = "thread-safety")]
#[serde(default)]
pub thread_safety: Option<String>,
#[serde(rename = "required-items")]
#[serde(default)]
pub required_items: Vec<ItemIdentifier>,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct LibraryConfig {
pub framework: String,
#[serde(rename = "crate")]
pub krate: String,
/// Dependencies are optional by default, this can be used to make a
/// dependency required.
///
/// This is used when depending on `objc2-foundation`, as we don't really
/// want a feature for something as fundamental as `NSString`.
/// Additionally, it is used for things like `MetalKit` always wanting
/// `Metal` enabled.
#[serde(rename = "required-dependencies")]
pub required_dependencies: HashSet<String>,
#[serde(rename = "custom-lib-rs")]
#[serde(default)]
pub custom_lib_rs: bool,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub macos: Option<Version>,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub maccatalyst: Option<Version>,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub ios: Option<Version>,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub tvos: Option<Version>,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub watchos: Option<Version>,
#[serde(default)]
#[serde(deserialize_with = "get_version")]
pub visionos: Option<Version>,
#[serde(default)]
pub gnustep: bool,
#[serde(default = "link_default")]
pub link: bool,
/// Data about an external class or protocol whose header isn't imported.
///
/// I.e. a bare `@protocol X;` or `@class X;`.
#[serde(default)]
pub external: BTreeMap<String, ExternalData>,
#[serde(rename = "class")]
#[serde(default)]
pub class_data: HashMap<String, ClassData>,
#[serde(rename = "protocol")]
#[serde(default)]
pub protocol_data: HashMap<String, ProtocolData>,
#[serde(rename = "struct")]
#[serde(default)]
pub struct_data: HashMap<String, StructData>,
#[serde(rename = "enum")]
#[serde(default)]
pub enum_data: HashMap<String, EnumData>,
#[serde(rename = "fn")]
#[serde(default)]
pub fns: HashMap<String, FnData>,
#[serde(rename = "static")]
#[serde(default)]
pub statics: HashMap<String, StaticData>,
#[serde(rename = "typedef")]
#[serde(default)]
pub typedef_data: HashMap<String, TypedefData>,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Example {
pub name: String,
#[serde(default)]
pub description: String,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ClassData {
#[serde(default)]
pub skipped: bool,
#[serde(rename = "definition-skipped")]
#[serde(default)]
pub definition_skipped: bool,
#[serde(default)]
pub methods: HashMap<String, MethodData>,
#[serde(default)]
pub categories: HashMap<String, CategoryData>,
#[serde(default)]
pub derives: Derives,
#[serde(default)]
pub counterpart: Counterpart,
#[serde(default)]
#[serde(rename = "main-thread-only")]
pub main_thread_only: bool,
#[serde(rename = "skipped-protocols")]
#[serde(default)]
pub skipped_protocols: HashSet<String>,
}
impl ClassData {
pub fn get_method_data(this: Option<&Self>, name: &str) -> MethodData {
this.map(|data| data.methods.get(name).copied().unwrap_or_default())
.unwrap_or_default()
}
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct CategoryData {
#[serde(default)]
pub skipped: bool,
#[serde(default)]
pub renamed: Option<String>,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ProtocolData {
#[serde(default)]
pub renamed: Option<String>,
#[serde(default)]
pub skipped: bool,
#[serde(default)]
#[serde(rename = "requires-mainthreadonly")]
pub requires_mainthreadonly: Option<bool>,
#[serde(default)]
pub methods: HashMap<String, MethodData>,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct StructData {
#[serde(default)]
pub skipped: bool,
}
#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct EnumData {
#[serde(default)]
pub skipped: bool,
#[serde(rename = "use-value")]
#[serde(default)]
pub use_value: bool,
#[serde(default)]
pub constants: HashMap<String, StructData>,
}
#[derive(Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct MethodData {
#[serde(rename = "unsafe")]
#[serde(default = "unsafe_default")]
pub unsafe_: bool,
#[serde(default = "skipped_default")]
pub skipped: bool,
}
impl MethodData {
pub(crate) fn merge_with_superclass(self, superclass: Self) -> Self {
Self {
// Only use `unsafe` from itself, never take if from the superclass
unsafe_: self.unsafe_,
skipped: self.skipped | superclass.skipped,
}
}
}
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct FnData {
#[serde(default)]
pub skipped: bool,
#[serde(rename = "unsafe")]
#[serde(default = "unsafe_default")]
pub unsafe_: bool,
}
impl Default for FnData {
fn default() -> Self {
Self {
skipped: skipped_default(),
unsafe_: unsafe_default(),
}
}
}
// TODO
pub type StaticData = StructData;
pub type TypedefData = StructData;
fn unsafe_default() -> bool {
true
}
fn skipped_default() -> bool {
false
}
fn link_default() -> bool {
true
}
impl Default for MethodData {
fn default() -> Self {
Self {
unsafe_: unsafe_default(),
skipped: skipped_default(),
}
}
}
impl LibraryConfig {
pub fn from_file(file: &Path) -> Result<Self, Box<dyn Error>> {
let s = fs::read_to_string(file)?;
let config: Self = basic_toml::from_str(&s)?;
assert_eq!(
config.framework.to_lowercase(),
config.krate.replace("objc2-", "").replace('-', ""),
"crate name had an unexpected format",
);
assert_eq!(
Some(&*config.framework.to_train_case().to_lowercase()),
config.krate.strip_prefix("objc2-"),
"crate name had an unexpected format",
);
Ok(config)
}
}
impl<'de> de::Deserialize<'de> for Counterpart {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct CounterpartVisitor;
impl de::Visitor<'_> for CounterpartVisitor {
type Value = Counterpart;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("item identifier")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
if let Some(value) = value.strip_prefix("ImmutableSuperclass(") {
let value = value
.strip_suffix(')')
.ok_or_else(|| de::Error::custom("end parenthesis"))?;
let item = ItemIdentifier::from_str(value).map_err(de::Error::custom)?;
return Ok(Counterpart::ImmutableSuperclass(item));
}
if let Some(value) = value.strip_prefix("MutableSubclass(") {
let value = value
.strip_suffix(')')
.ok_or_else(|| de::Error::custom("end parenthesis"))?;
let item = ItemIdentifier::from_str(value).map_err(de::Error::custom)?;
return Ok(Counterpart::MutableSubclass(item));
}
Err(de::Error::custom(format!("unknown variant {value:?}")))
}
}
deserializer.deserialize_str(CounterpartVisitor)
}
}