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
use crate::gen::common::{InterfaceGenError, Result};
use crate::gen::interface::config::InterfaceConfig;
use crate::gen::interface::contract::ContractInterface;
use crate::gen::interface::import::Import;
use crate::parse::source_file::SourceFile;
use cubist_config::Target;
use serde::{Serialize, Serializer};
use solang_parser::pt;
use solang_parser::pt::Docable;
use std::fmt;
use std::path::PathBuf;
#[derive(Debug, Serialize)]
pub struct FileInterfaces {
source_info: SourceInfo,
sender_target: Target,
receiver_target: Target,
pragmas: Vec<Pragma>,
imports: Vec<Import>,
license: Option<String>,
pub interfaces: Vec<ContractInterface>,
}
#[derive(Debug, Serialize)]
pub struct SourceInfo {
#[serde(skip_serializing)]
full_path: PathBuf,
rel_path: PathBuf,
}
#[derive(Debug)]
pub struct Pragma(pt::SourceUnitPart);
impl Serialize for Pragma {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let str = self.to_string();
serializer.serialize_str(&str)
}
}
impl fmt::Display for Pragma {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.display())
}
}
impl Serialize for Import {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let str = self.to_string();
serializer.serialize_str(&str)
}
}
impl FileInterfaces {
pub fn new(source: &SourceFile, config: &InterfaceConfig, target: Target) -> Result<Self> {
let license = get_license(&source.comments)?;
let mut pragmas = Vec::new();
let mut imports = Vec::new();
let mut interfaces = Vec::new();
for part in &source.pt.0 {
if let pt::SourceUnitPart::ImportDirective(_) = part {
imports.push(part.clone());
} else if let pt::SourceUnitPart::ContractDefinition(cd) = part {
let contract_name = &cd.name.name;
if !config.gen_contract(contract_name) {
continue;
}
let interface = ContractInterface::new(config, cd)?;
interfaces.push(interface);
} else if let pt::SourceUnitPart::PragmaDirective(..) = part {
pragmas.push(Pragma(part.clone()));
}
}
if !source.file_name.is_file() {
return Err(InterfaceGenError::NotAFile(
source.file_name.display().to_string(),
));
}
Ok(FileInterfaces {
source_info: SourceInfo {
full_path: source.file_name.clone(),
rel_path: source.rel_path.clone(),
},
sender_target: target,
receiver_target: source.target,
pragmas,
imports: imports.iter().map(Import::new).collect(),
license,
interfaces,
})
}
pub fn is_empty(&self) -> bool {
self.interfaces.is_empty()
}
pub fn get_sender_target(&self) -> Target {
self.sender_target
}
pub fn get_receiver_target(&self) -> Target {
self.receiver_target
}
pub fn get_source_path(&self) -> &PathBuf {
&self.source_info.full_path
}
pub fn get_source_file(&self) -> &PathBuf {
&self.source_info.rel_path
}
pub fn get_file_stem(&self) -> Option<String> {
self.source_info
.rel_path
.file_stem()
.and_then(|f| f.to_str().map(|s| s.to_string()))
}
}
fn get_license(comments: &[pt::Comment]) -> Result<Option<String>> {
if comments.is_empty() {
return Ok(None);
} else {
let contents = comments[0].get_contents();
if let Some((_, after)) = contents.split_once("SPDX-License-Identifier:") {
if after.is_empty() {
return Err(InterfaceGenError::MissingLicense);
}
let mut license = None;
for word in after.split(' ').collect::<Vec<&str>>() {
if !word.is_empty() {
license = Some(word.to_string());
break;
}
}
if license.is_none() {
return Err(InterfaceGenError::MissingLicense);
}
return Ok(license);
}
}
Ok(None)
}