Skip to main content

opencv_binding_generator/
lib.rs

1// todo support converting pointer + size to slice of Mat and other similar objects
2// todo add support for arrays in dnn::DictValue
3// todo allow ergonomically combining of enum variants with |
4// todo almost everything from the manual module must be connected to the binding generator, not the main crate
5// todo check that FN_FaceDetector works at all (receiving InputArray, passing as callback)
6// todo public static properties like opencv2/core/base.hpp:384 Hamming::normType
7// todo test returning reference to array like cv_MatStep_buf
8// todo, allow extension of simple classes for e.g. Elliptic_KeyPoint
9// todo OCRTesseract::create should have nullable params
10// fixme vector<Mat*> get's interpreted as Vector<Mat> which should be wrong (e.g. Layer::forward and Layer::apply_halide_scheduler)
11// fixme MatConstIterator::m return Mat**, is it handled correctly?
12// fixme VectorOfMat::get allows mutation
13// fixme MatSize looks like just a pointer inside a Mat so it should take a lifetime
14
15// copy-pasted form python generator (may be obsolete):
16// fixme returning MatAllocator (trait) by reference is bad, check knearestneighbour
17
18#![expect(clippy::nonminimal_bool)] // pattern `!type_ref.as_vector().is_some()` used for more clarity
19
20use std::borrow::Cow;
21use std::fs::File;
22use std::io::{BufRead, Read, Seek, SeekFrom};
23use std::ops::ControlFlow;
24
25pub use abstract_ref_wrapper::AbstractRefWrapper;
26use clang::Entity;
27pub use class::Class;
28pub use constant::Const;
29pub use element::{DefaultElement, Element, EntityElement, is_opencv_path, opencv_module_from_path};
30pub use entity::EntityExt;
31#[expect(unused)]
32use entity::dbg_clang_entity;
33pub use enumeration::Enum;
34use field::Field;
35pub use func::{Func, FuncTypeHint, Pred, UsageTracker};
36pub use generator::{GeneratedType, Generator, GeneratorVisitor, OpenCvWalker};
37pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
38pub use iterator_ext::IteratorExt;
39pub use map_borrowed::CowMapBorrowedExt;
40use memoize::{MemoizeMap, MemoizeMapExt};
41use smart_ptr::SmartPtr;
42pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
43pub use supported_module::SupportedModule;
44use tuple::Tuple;
45use type_ref::TypeRef;
46#[expect(unused)]
47use type_ref::dbg_clang_type;
48pub use type_ref::{Constness, CppNameStyle, NameStyle};
49pub use typedef::Typedef;
50use vector::Vector;
51pub use walker::{EntityWalkerExt, EntityWalkerVisitor};
52
53use crate::debug::NameDebug;
54
55mod abstract_ref_wrapper;
56mod class;
57pub mod comment;
58mod constant;
59pub mod debug;
60mod element;
61mod entity;
62mod enumeration;
63mod field;
64mod func;
65mod function;
66mod generator;
67mod generator_env;
68mod iterator_ext;
69mod map_borrowed;
70mod memoize;
71mod name_pool;
72mod renderer;
73pub mod settings;
74mod smart_ptr;
75mod string_ext;
76mod supported_module;
77#[cfg(test)]
78mod test;
79mod tuple;
80mod type_ref;
81mod typedef;
82mod vector;
83pub mod version;
84mod walker;
85pub mod writer;
86
87fn get_definition_text(entity: Entity) -> String {
88	if let Some(range) = entity.get_range() {
89		let loc = range.get_start().get_spelling_location();
90		let start = loc.offset;
91		let end = range.get_end().get_spelling_location().offset;
92		let len = usize::try_from(end - start).expect("Definition span is too large");
93		let mut def_bytes = vec![0; len];
94		let mut source = File::open(loc.file.expect("Can't get file").get_path()).expect("Can't open source file");
95		source.seek(SeekFrom::Start(u64::from(start))).expect("Cannot seek");
96		source.read_exact(&mut def_bytes).expect("Can't read definition");
97		String::from_utf8(def_bytes).expect("Can't parse definition")
98	} else {
99		unreachable!("Can't get entity range: {:#?}", entity)
100	}
101}
102
103fn reserved_rename(val: Cow<str>) -> Cow<str> {
104	settings::RESERVED_RENAME.get(val.as_ref()).map_or(val, |&v| v.into())
105}
106
107#[inline(always)]
108pub fn line_reader(mut b: impl BufRead, mut cb: impl FnMut(&str) -> ControlFlow<()>) {
109	let mut line = String::with_capacity(256);
110	while let Ok(bytes_read) = b.read_line(&mut line) {
111		if bytes_read == 0 || cb(&line).is_break() {
112			break;
113		}
114		line.clear();
115	}
116}