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
use crate::bridge_module_attributes::CfgAttr;
use crate::SwiftBridgeModule;
mod generate_c_header;
mod generate_rust_tokens;
mod generate_swift;
#[cfg(test)]
mod codegen_tests;
pub struct SwiftCodeAndCHeader {
pub swift: String,
pub c_header: String,
}
pub struct CodegenConfig {
pub crate_feature_lookup: Box<dyn Fn(&str) -> bool>,
}
#[cfg(test)]
impl CodegenConfig {
pub(crate) fn no_features_enabled() -> Self {
CodegenConfig {
crate_feature_lookup: Box::new(|_| false),
}
}
}
impl SwiftBridgeModule {
pub fn generate_swift_code_and_c_header(&self, config: CodegenConfig) -> SwiftCodeAndCHeader {
SwiftCodeAndCHeader {
swift: self.generate_swift(&config),
c_header: self.generate_c_header(&config),
}
}
fn module_will_be_compiled(&self, config: &CodegenConfig) -> bool {
for cfg_attr in &self.cfg_attrs {
match cfg_attr {
CfgAttr::Feature(feature_name) => {
if !(config.crate_feature_lookup)(&feature_name.value()) {
return false;
}
}
}
}
true
}
}