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
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::fs;
use std::fs::{File, OpenOptions};
use std::io::{ErrorKind, Write};
use std::path::{Path, PathBuf};
use dunce::canonicalize;
use maplit::hashmap;
use once_cell::sync::Lazy;
use class::ClassExt;
use element::{RustElement, RustNativeGeneratedElement};
use crate::field::Field;
use crate::name_pool::NamePool;
use crate::type_ref::{Constness, CppNameStyle, FishStyle, NameStyle};
use crate::{
is_ephemeral_header, opencv_module_from_path, settings, Class, CompiledInterpolation, Const, Element, Enum, Func,
GeneratedType, GeneratorVisitor, IteratorExt, StrExt, Typedef,
};
mod abstract_ref_wrapper;
mod class;
mod comment;
mod constant;
pub mod element;
mod enumeration;
mod field;
mod func;
mod func_desc;
mod function;
pub mod renderer;
mod smart_ptr;
mod tuple;
pub mod type_ref;
mod typedef;
mod vector;
type Entries = Vec<(String, String)>;
type UniqueEntries = BTreeMap<String, String>;
#[derive(Clone, Debug)]
pub struct RustNativeBindingWriter<'s> {
debug: bool,
src_cpp_dir: PathBuf,
module: &'s str,
opencv_version: &'s str,
debug_path: PathBuf,
rust_path: PathBuf,
types_dir: PathBuf,
exports_path: PathBuf,
cpp_path: PathBuf,
comment: String,
prelude_traits: Vec<String>,
consts: Entries,
enums: Entries,
rust_funcs: Entries,
rust_typedefs: Option<UniqueEntries>,
rust_classes: Entries,
export_funcs: Entries,
export_classes: Entries,
cpp_funcs: Entries,
cpp_classes: Entries,
}
impl<'s> RustNativeBindingWriter<'s> {
pub fn new(src_cpp_dir: &Path, out_dir: impl Into<PathBuf>, module: &'s str, opencv_version: &'s str, debug: bool) -> Self {
let out_dir = out_dir.into();
let debug_path = out_dir.join(format!("{module}.log"));
#[allow(clippy::collapsible_if)]
if false {
if debug {
File::create(&debug_path).expect("Can't create debug log");
}
}
Self {
debug,
src_cpp_dir: canonicalize(src_cpp_dir).expect("Can't canonicalize src_cpp_dir"),
module,
opencv_version,
debug_path,
rust_path: out_dir.join(format!("{module}.rs")),
exports_path: out_dir.join(format!("{module}.externs.rs")),
cpp_path: out_dir.join(format!("{module}.cpp")),
types_dir: out_dir,
comment: String::new(),
prelude_traits: vec![],
consts: vec![],
enums: vec![],
rust_funcs: vec![],
rust_typedefs: Some(BTreeMap::new()),
rust_classes: vec![],
export_funcs: vec![],
export_classes: vec![],
cpp_funcs: vec![],
cpp_classes: vec![],
}
}
fn emit_debug_log(&mut self, obj: &impl Debug) {
#[allow(clippy::collapsible_if)]
if false {
if self.debug {
let mut f = OpenOptions::new()
.append(true)
.open(&self.debug_path)
.expect("Can't open debug file");
writeln!(f, "{obj:#?}").expect("Can't write debug info");
}
}
}
}
impl GeneratorVisitor for RustNativeBindingWriter<'_> {
fn wants_file(&mut self, path: &Path) -> bool {
is_ephemeral_header(path) || opencv_module_from_path(path).map_or(false, |m| m == self.module)
}
fn visit_module_comment(&mut self, comment: String) {
self.comment = comment;
}
fn visit_const(&mut self, cnst: Const) {
self.emit_debug_log(&cnst);
self.consts.push((
cnst.rust_name(NameStyle::decl()).into_owned(),
cnst.gen_rust(self.opencv_version),
));
}
fn visit_enum(&mut self, enm: Enum) {
self.emit_debug_log(&enm);
self.enums.push((
enm.rust_name(NameStyle::decl()).into_owned(),
enm.gen_rust(self.opencv_version),
));
}
fn visit_func(&mut self, func: Func) {
self.emit_debug_log(&func);
let name: String = func.identifier().into_owned();
self.rust_funcs.push((name.clone(), func.gen_rust(self.opencv_version)));
self.export_funcs.push((name.clone(), func.gen_rust_exports()));
self.cpp_funcs.push((name, func.gen_cpp()));
}
fn visit_typedef(&mut self, typedef: Typedef) {
self.emit_debug_log(&typedef);
let opencv_version = self.opencv_version;
if let Some(typedefs) = self.rust_typedefs.as_mut() {
let cpp_refname = typedef.cpp_name(CppNameStyle::Reference);
if !typedefs.contains_key(cpp_refname.as_ref()) {
typedefs.insert(cpp_refname.into_owned(), typedef.gen_rust(opencv_version));
}
}
}
fn visit_class(&mut self, class: Class) {
self.emit_debug_log(&class);
if let Some(enm) = class.as_enum() {
self.enums.push((
enm.rust_name(NameStyle::decl()).into_owned(),
enm.gen_rust(self.opencv_version),
));
} else {
if class.is_trait() {
self.prelude_traits.push(format!(
"super::{}",
class.rust_trait_name(NameStyle::decl(), Constness::Const).into_owned()
));
self.prelude_traits.push(format!(
"super::{}",
class.rust_trait_name(NameStyle::decl(), Constness::Mut).into_owned()
));
}
let name = class.cpp_name(CppNameStyle::Reference).into_owned();
self.rust_classes.push((name.clone(), class.gen_rust(self.opencv_version)));
self.export_classes.push((name.clone(), class.gen_rust_exports()));
self.cpp_classes.push((name, class.gen_cpp()));
}
}
fn visit_generated_type(&mut self, typ: GeneratedType) {
let typ = typ.as_ref();
let prio = typ.element_order();
let safe_id = typ.element_safe_id();
let suffix = ".type.rs";
let mut file_name = format!("{prio:03}-{safe_id}");
ensure_filename_length(&mut file_name, suffix.len());
file_name.push_str(suffix);
let path = self.types_dir.join(file_name);
let file = OpenOptions::new().create_new(true).write(true).open(&path);
match file {
Ok(mut file) => {
let gen = typ.gen_rust(self.opencv_version);
if !gen.is_empty() {
file.write_all(gen.as_bytes()).expect("Can't write to rust file");
} else {
drop(file);
fs::remove_file(&path).expect("Can't remove empty file");
}
}
Err(e) if e.kind() == ErrorKind::AlreadyExists => { }
Err(e) if e.kind() == ErrorKind::PermissionDenied => { }
Err(e) => {
panic!("Error while creating file for rust generated type: {e}")
}
}
let suffix = ".type.cpp";
let mut filename = format!("{prio:03}-{safe_id}");
ensure_filename_length(&mut filename, suffix.len());
filename.push_str(suffix);
let path = self.types_dir.join(filename);
let file = OpenOptions::new().create_new(true).write(true).open(&path);
match file {
Ok(mut file) => {
let gen = typ.gen_cpp();
if !gen.is_empty() {
file.write_all(gen.as_bytes()).expect("Can't write to cpp file");
} else {
drop(file);
fs::remove_file(&path).expect("Can't remove empty file");
}
}
Err(e) if e.kind() == ErrorKind::AlreadyExists => { }
Err(e) if e.kind() == ErrorKind::PermissionDenied => { }
Err(e) => {
panic!("Error while creating file for cpp generated type: {e}")
}
}
}
fn visit_ephemeral_header(&mut self, contents: &str) {
if self.debug {
let mut file = File::create(self.types_dir.join(format!("ocvrs_ephemeral_{}.hpp", self.module)))
.expect("Can't create debug ephemeral file");
file.write_all(contents.as_bytes()).expect("Can't write debug ephemeral file");
}
}
}
fn join<T: AsRef<str>>(v: &mut Vec<(String, T)>) -> String {
v.sort_unstable_by(|(name_left, _), (name_right, _)| name_left.cmp(name_right));
v.drain(..).map(|(_, gen)| gen).join("")
}
impl Drop for RustNativeBindingWriter<'_> {
fn drop(&mut self) {
static RUST: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/module/rust.tpl.rs").compile_interpolation());
static RUST_PRELUDE: Lazy<CompiledInterpolation> =
Lazy::new(|| include_str!("tpl/module/prelude.tpl.rs").compile_interpolation());
static RUST_EXTERNS_TPL: Lazy<CompiledInterpolation> =
Lazy::new(|| include_str!("tpl/module/externs.tpl.rs").compile_interpolation());
static CPP: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/module/cpp.tpl.cpp").compile_interpolation());
let mut rust = join(&mut self.consts);
rust += &join(&mut self.enums);
let mut typedefs = self
.rust_typedefs
.take()
.map(|typedefs| typedefs.into_iter().collect())
.unwrap_or_default();
rust += &join(&mut typedefs);
rust += &join(&mut self.rust_funcs);
rust += &join(&mut self.rust_classes);
let prelude = RUST_PRELUDE.interpolate(&hashmap! {
"traits" => self.prelude_traits.join(", "),
});
File::create(&self.rust_path)
.expect("Can't create rust file")
.write_all(
RUST
.interpolate(&hashmap! {
"static_modules" => settings::STATIC_MODULES.iter().join(", "),
"comment" => comment::render_doc_comment(&self.comment, "//!", self.opencv_version),
"prelude" => prelude,
"code" => rust,
})
.as_bytes(),
)
.expect("Can't write rust file");
let mut cpp = join(&mut self.cpp_funcs);
cpp += &join(&mut self.cpp_classes);
let includes = if self.src_cpp_dir.join(format!("{}.hpp", self.module)).exists() {
format!("#include \"{module}.hpp\"", module = self.module)
} else {
format!(
"#include \"ocvrs_common.hpp\"\n#include <opencv2/{module}.hpp>",
module = self.module
)
};
File::create(&self.cpp_path)
.expect("Can't create cpp file")
.write_all(
CPP.interpolate(&hashmap! {
"module" => self.module,
"includes" => includes.as_str(),
"code" => cpp.as_str(),
})
.as_bytes(),
)
.expect("Can't write cpp file");
let mut exports = join(&mut self.export_funcs);
exports += &join(&mut self.export_classes);
File::create(&self.exports_path)
.expect("Can't create rust exports file")
.write_all(
RUST_EXTERNS_TPL
.interpolate(&hashmap! {
"code" => exports,
})
.as_bytes(),
)
.expect("Can't write rust exports file");
}
}
fn ensure_filename_length(file_name: &mut String, reserve: usize) {
const MAX_FILENAME_LEN: usize = 255;
let max_length = MAX_FILENAME_LEN - reserve;
if file_name.len() > max_length {
*file_name = file_name[..max_length].to_string();
}
}
fn rust_disambiguate_names<'tu, 'ge, I: IntoIterator<Item = Field<'tu, 'ge>>>(
args: I,
) -> impl Iterator<Item = (String, Field<'tu, 'ge>)>
where
'tu: 'ge,
{
let args = args.into_iter();
NamePool::with_capacity(args.size_hint().1.unwrap_or_default()).into_disambiguator(args, |f| f.rust_leafname(FishStyle::No))
}