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};
39use tuple::Tuple;
40#[allow(unused)]
41use type_ref::dbg_clang_type;
42use type_ref::TypeRef;
43pub use type_ref::{Constness, CppNameStyle, NameStyle};
44pub use typedef::Typedef;
45use vector::Vector;
46pub use walker::{EntityWalkerExt, EntityWalkerVisitor};
47
48use crate::debug::NameDebug;
49
50mod abstract_ref_wrapper;
51mod class;
52pub mod comment;
53mod constant;
54mod debug;
55mod element;
56mod entity;
57mod enumeration;
58mod field;
59mod func;
60mod function;
61mod generator;
62mod generator_env;
63mod iterator_ext;
64mod map_borrowed;
65mod memoize;
66mod name_pool;
67mod renderer;
68pub mod settings;
69mod smart_ptr;
70mod string_ext;
71#[cfg(test)]
72mod test;
73mod tuple;
74mod type_ref;
75mod typedef;
76mod vector;
77mod walker;
78pub mod writer;
79
80fn get_definition_text(entity: Entity) -> String {
81 if let Some(range) = entity.get_range() {
82 let loc = range.get_start().get_spelling_location();
83 let mut source = File::open(loc.file.expect("Can't get file").get_path()).expect("Can't open source file");
84 let start = loc.offset;
85 let end = range.get_end().get_spelling_location().offset;
86 let len = usize::try_from(end - start).expect("Definition span is too large");
87 let mut def_bytes = vec![0; len];
88 source.seek(SeekFrom::Start(u64::from(start))).expect("Cannot seek");
89 source.read_exact(&mut def_bytes).expect("Can't read definition");
90 String::from_utf8(def_bytes).expect("Can't parse definition")
91 } else {
92 unreachable!("Can't get entity range: {:#?}", entity)
93 }
94}
95
96fn reserved_rename(val: Cow<str>) -> Cow<str> {
97 if let Some(&v) = settings::RESERVED_RENAME.get(val.as_ref()) {
98 v.into()
99 } else {
100 val
101 }
102}
103
104#[inline(always)]
105pub fn line_reader(mut b: impl BufRead, mut cb: impl FnMut(&str) -> ControlFlow<()>) {
106 let mut line = String::with_capacity(256);
107 while let Ok(bytes_read) = b.read_line(&mut line) {
108 if bytes_read == 0 {
109 break;
110 }
111 if cb(&line).is_break() {
112 break;
113 }
114 line.clear();
115 }
116}