opencv_binding_generator/
lib.rs1#![allow(clippy::nonminimal_bool)] use std::borrow::Cow;
17use std::fs::File;
18use std::io::{BufRead, Read, Seek, SeekFrom};
19use std::ops::ControlFlow;
20
21pub use abstract_ref_wrapper::AbstractRefWrapper;
22use clang::Entity;
23pub use class::Class;
24pub use constant::Const;
25pub use element::{is_opencv_path, opencv_module_from_path, DefaultElement, Element, EntityElement};
26#[allow(unused)]
27use entity::dbg_clang_entity;
28pub use entity::EntityExt;
29pub use enumeration::Enum;
30use field::Field;
31pub use func::{Func, FuncTypeHint, Pred, UsageTracker};
32pub use generator::{GeneratedType, Generator, GeneratorVisitor, OpenCvWalker};
33pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
34pub use iterator_ext::IteratorExt;
35pub use map_borrowed::CowMapBorrowedExt;
36use memoize::{MemoizeMap, MemoizeMapExt};
37use smart_ptr::SmartPtr;
38pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
39pub use supported_module::SupportedModule;
40use tuple::Tuple;
41#[allow(unused)]
42use type_ref::dbg_clang_type;
43use type_ref::TypeRef;
44pub use type_ref::{Constness, CppNameStyle, NameStyle};
45pub use typedef::Typedef;
46use vector::Vector;
47pub use walker::{EntityWalkerExt, EntityWalkerVisitor};
48
49use crate::debug::NameDebug;
50
51mod abstract_ref_wrapper;
52mod class;
53pub mod comment;
54mod constant;
55mod debug;
56mod element;
57mod entity;
58mod enumeration;
59mod field;
60mod func;
61mod function;
62mod generator;
63mod generator_env;
64mod iterator_ext;
65mod map_borrowed;
66mod memoize;
67mod name_pool;
68mod renderer;
69pub mod settings;
70mod smart_ptr;
71mod string_ext;
72mod supported_module;
73#[cfg(test)]
74mod test;
75mod tuple;
76mod type_ref;
77mod typedef;
78mod vector;
79mod walker;
80pub mod writer;
81
82fn get_definition_text(entity: Entity) -> String {
83 if let Some(range) = entity.get_range() {
84 let loc = range.get_start().get_spelling_location();
85 let start = loc.offset;
86 let end = range.get_end().get_spelling_location().offset;
87 let len = usize::try_from(end - start).expect("Definition span is too large");
88 let mut def_bytes = vec![0; len];
89 let mut source = File::open(loc.file.expect("Can't get file").get_path()).expect("Can't open source file");
90 source.seek(SeekFrom::Start(u64::from(start))).expect("Cannot seek");
91 source.read_exact(&mut def_bytes).expect("Can't read definition");
92 String::from_utf8(def_bytes).expect("Can't parse definition")
93 } else {
94 unreachable!("Can't get entity range: {:#?}", entity)
95 }
96}
97
98fn reserved_rename(val: Cow<str>) -> Cow<str> {
99 if let Some(&v) = settings::RESERVED_RENAME.get(val.as_ref()) {
100 v.into()
101 } else {
102 val
103 }
104}
105
106#[inline(always)]
107pub fn line_reader(mut b: impl BufRead, mut cb: impl FnMut(&str) -> ControlFlow<()>) {
108 let mut line = String::with_capacity(256);
109 while let Ok(bytes_read) = b.read_line(&mut line) {
110 if bytes_read == 0 {
111 break;
112 }
113 if cb(&line).is_break() {
114 break;
115 }
116 line.clear();
117 }
118}