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
mod analyzer;
mod config;
mod contract;
pub mod file;
mod import;
use crate::gen::common::{InterfaceGenError, Result};
use crate::parse::{source_file::SourceFile, SourceFiles};
use analyzer::Analyzer;
use config::{AnalysisInfo, ExplicitInfo, InterfaceConfig};
use cubist_config::{ContractName, Target};
use file::FileInterfaces;
use std::collections::{BTreeMap as Map, HashSet as Set};
use std::path::{Path, PathBuf};
pub struct Interfaces {
pub interfaces: Vec<FileInterfaces>,
pub contract_locations: Map<PathBuf, Vec<ContractName>>,
pub cross_chain_deps: Map<ContractName, Set<ContractName>>,
}
impl Interfaces {
pub fn get_contracts_in_file(&self, path: &Path) -> Map<ContractName, Set<ContractName>> {
self.contract_locations
.get(path)
.unwrap_or(&vec![])
.iter()
.cloned()
.map(|c| {
let deps = self
.cross_chain_deps
.get(&c)
.map(Clone::clone)
.unwrap_or_default();
(c, deps)
})
.collect()
}
}
pub fn get_interfaces(source_files: &SourceFiles) -> Result<Interfaces> {
let cross_chain_analyzer = analyze(source_files)?;
let interface_config = InterfaceConfig::from(AnalysisInfo {
included_code: cross_chain_analyzer.get_call_info().clone(),
targets: cross_chain_analyzer.get_target_info().clone(),
});
let interfaces = to_file_interfaces(&interface_config, &source_files.sources)?;
let contract_locations = cross_chain_analyzer.get_file_contracts();
let cross_chain_deps = cross_chain_analyzer.get_cross_chain_dependencies();
Ok(Interfaces {
interfaces,
contract_locations,
cross_chain_deps,
})
}
pub fn get_interface_for_contract(
source_files: &SourceFiles,
contract: &ContractName,
targets: &Set<Target>,
) -> Result<Interfaces> {
if source_files.sources.is_empty() {
return Err(InterfaceGenError::MissingContracts);
}
let mut contract_finder = Analyzer::new();
contract_finder.analyze_contract_locations(&source_files.sources)?;
let files = contract_finder.get_contract_files();
if !files.contains_key(contract) {
return Err(InterfaceGenError::MissingContract(contract.clone()));
}
let interface_config = InterfaceConfig::from(ExplicitInfo {
contract: contract.clone(),
file: files[contract].clone(),
targets: targets.clone(),
});
let interfaces = to_file_interfaces(&interface_config, &source_files.sources)?;
let contract_locations = contract_finder.get_file_contracts();
let cross_chain_deps = contract_finder.get_cross_chain_dependencies();
Ok(Interfaces {
interfaces,
contract_locations,
cross_chain_deps,
})
}
fn analyze(source_files: &SourceFiles) -> Result<Analyzer> {
if source_files.sources.is_empty() {
return Err(InterfaceGenError::MissingContracts);
}
let mut cross_chain_analyzer = Analyzer::new();
cross_chain_analyzer.analyze(&source_files.sources)?;
Ok(cross_chain_analyzer)
}
fn to_file_interfaces(
interface_config: &InterfaceConfig,
sources: &[SourceFile],
) -> Result<Vec<FileInterfaces>> {
sources
.iter()
.filter(|source| interface_config.requires_interface(&source.file_name))
.map(|source| {
let file_name = &source.file_name;
interface_config
.get_interface_targets(file_name)?
.iter()
.map(|target| FileInterfaces::new(source, interface_config, *target))
.collect()
})
.collect::<Result<Vec<Vec<FileInterfaces>>>>()
.map(|interfaces| {
interfaces
.into_iter()
.flatten()
.collect::<Vec<FileInterfaces>>()
})
}