Skip to content

Commit d819390

Browse files
committed
Cleanup
1 parent b3d2ab4 commit d819390

File tree

24 files changed

+88
-111
lines changed

24 files changed

+88
-111
lines changed

binding-generator/src/class.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::{fmt, iter};
88
use clang::{Accessibility, Entity, EntityKind};
99
pub use desc::ClassDesc;
1010

11-
use crate::comment::strip_doxygen_comment_markers;
1211
use crate::debug::{DefinitionLocation, LocationName};
1312
use crate::element::ExcludeKind;
1413
use crate::entity::{ControlFlowExt, ToEntity};
@@ -642,7 +641,7 @@ impl Element for Class<'_, '_> {
642641

643642
fn doc_comment(&self) -> Cow<'_, str> {
644643
match self {
645-
&Self::Clang { entity, .. } => strip_doxygen_comment_markers(&entity.get_comment().unwrap_or_default()).into(),
644+
Self::Clang { entity, .. } => entity.doc_comment(),
646645
Self::Desc(_) => "".into(),
647646
}
648647
}

binding-generator/src/constant.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ use std::rc::Rc;
44

55
use clang::token::{Token, TokenKind};
66
use clang::{Entity, EntityKind, EvaluationResult};
7-
use desc::ConstDesc;
7+
pub use desc::ConstDesc;
88

9-
use crate::comment::strip_doxygen_comment_markers;
109
use crate::debug::{DefinitionLocation, LocationName};
1110
use crate::element::ExcludeKind;
1211
use crate::type_ref::CppNameStyle;
@@ -56,6 +55,10 @@ impl<'tu> Const<'tu> {
5655
Self::Clang { entity }
5756
}
5857

58+
pub fn new_desc(desc: ConstDesc) -> Self {
59+
Self::Desc(Rc::new(desc))
60+
}
61+
5962
pub fn value(&self) -> Option<Cow<'_, Value>> {
6063
match self {
6164
Self::Clang { entity } => match entity.get_kind() {
@@ -110,8 +113,8 @@ impl Element for Const<'_> {
110113

111114
fn doc_comment(&self) -> Cow<'_, str> {
112115
match self {
113-
Self::Clang { entity } => Owned(strip_doxygen_comment_markers(&entity.get_comment().unwrap_or_default())),
114-
Self::Desc(_) => Borrowed(""),
116+
Self::Clang { entity } => entity.doc_comment(),
117+
Self::Desc(desc) => Borrowed(desc.doc_comment.as_ref()),
115118
}
116119
}
117120

@@ -156,6 +159,13 @@ pub struct Value {
156159
}
157160

158161
impl Value {
162+
pub fn integer(val: i32) -> Self {
163+
Self {
164+
kind: ValueKind::Integer,
165+
value: val.to_string(),
166+
}
167+
}
168+
159169
pub fn try_from_tokens(tokens: &[Token]) -> Option<Self> {
160170
let mut out = Value {
161171
kind: ValueKind::Integer,

binding-generator/src/constant/desc.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,23 @@ use crate::SupportedModule;
77
pub struct ConstDesc {
88
pub cpp_fullname: Rc<str>,
99
pub rust_module: SupportedModule,
10+
pub doc_comment: Rc<str>,
1011
pub value: Rc<Value>,
1112
}
13+
14+
impl ConstDesc {
15+
pub fn new(cpp_fullname: impl Into<Rc<str>>, rust_module: SupportedModule, value: Value) -> Self {
16+
Self {
17+
cpp_fullname: cpp_fullname.into(),
18+
rust_module,
19+
doc_comment: "".into(),
20+
value: Rc::new(value),
21+
}
22+
}
23+
24+
#[inline]
25+
pub fn doc_comment(mut self, doc_comment: impl Into<Rc<str>>) -> Self {
26+
self.doc_comment = doc_comment.into();
27+
self
28+
}
29+
}

binding-generator/src/enumeration.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::rc::Rc;
55

66
use clang::{Entity, EntityKind, EntityVisitResult};
77

8-
use crate::comment::strip_doxygen_comment_markers;
98
use crate::debug::LocationName;
109
use crate::element::ExcludeKind;
1110
use crate::type_ref::CppNameStyle;
@@ -81,7 +80,7 @@ impl Element for Enum<'_> {
8180
}
8281

8382
fn doc_comment(&self) -> Cow<'_, str> {
84-
strip_doxygen_comment_markers(&self.entity.get_comment().unwrap_or_default()).into()
83+
self.entity.doc_comment()
8584
}
8685

8786
fn cpp_namespace(&self) -> Cow<'_, str> {

binding-generator/src/field.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::rc::Rc;
55
use clang::token::TokenKind;
66
use clang::{Entity, EntityKind, EntityVisitResult};
77

8-
use crate::comment::strip_doxygen_comment_markers;
98
use crate::debug::{DefinitionLocation, LocationName, NameDebug};
109
use crate::element::ExcludeKind;
1110
use crate::settings::{ARGUMENT_NAMES_MULTIPLE_SLICE, ARGUMENT_NAMES_NOT_SLICE, ARGUMENT_NAMES_USERDATA};
@@ -248,7 +247,7 @@ impl Element for Field<'_, '_> {
248247

249248
fn doc_comment(&self) -> Cow<'_, str> {
250249
match self {
251-
Field::Clang { entity, .. } => strip_doxygen_comment_markers(&entity.get_comment().unwrap_or_default()).into(),
250+
Field::Clang { entity, .. } => entity.doc_comment(),
252251
Field::Desc(_) => "".into(),
253252
}
254253
}

binding-generator/src/func.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub use kind::{FuncKind, OperatorKind, ReturnKind};
1212
use regex::bytes::Regex;
1313
use slice_arg_finder::SliceArgFinder;
1414

15-
use crate::comment::strip_doxygen_comment_markers;
1615
use crate::debug::{DefinitionLocation, LocationName};
1716
use crate::element::ExcludeKind;
1817
use crate::entity::ToEntity;
@@ -691,7 +690,7 @@ impl Element for Func<'_, '_> {
691690

692691
fn doc_comment(&self) -> Cow<'_, str> {
693692
match self {
694-
&Self::Clang { entity, .. } => strip_doxygen_comment_markers(&entity.get_comment().unwrap_or_default()).into(),
693+
Self::Clang { entity, .. } => entity.doc_comment(),
695694
Self::Desc(desc) => desc.doc_comment.as_ref().into(),
696695
}
697696
}

binding-generator/src/typedef.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::rc::Rc;
66
use clang::{Entity, EntityKind};
77
pub use desc::TypedefDesc;
88

9-
use crate::comment::strip_doxygen_comment_markers;
109
use crate::debug::{DefinitionLocation, LocationName};
1110
use crate::element::ExcludeKind;
1211
use crate::type_ref::{CppNameStyle, NameStyle, TypeRefDesc, TypeRefKind, TypeRefTypeHint};
@@ -122,7 +121,7 @@ impl Element for Typedef<'_, '_> {
122121

123122
fn doc_comment(&self) -> Cow<'_, str> {
124123
match self {
125-
Self::Clang { entity, .. } => strip_doxygen_comment_markers(&entity.get_comment().unwrap_or_default()).into(),
124+
Self::Clang { entity, .. } => entity.doc_comment(),
126125
Self::Desc(_) => "".into(),
127126
}
128127
}

binding-generator/src/writer/rust_native/class.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,6 @@ impl RustElement for Class<'_, '_> {
4545
}
4646
}
4747
}
48-
49-
fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
50-
match self {
51-
&Self::Clang { entity, .. } => DefaultRustNativeElement::rendered_doc_comment(entity, comment_marker, opencv_version),
52-
Self::Desc(_) => "".to_string(),
53-
}
54-
}
5548
}
5649

5750
impl RustNativeGeneratedElement for Class<'_, '_> {

binding-generator/src/writer/rust_native/class/gen.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ pub fn gen_simple_class(c: &Class, opencv_version: &str) -> String {
3737
SIMPLE_FIELD_TPL.interpolate_into(
3838
&mut fields,
3939
&HashMap::from([
40-
("doc_comment", Cow::Owned(f.rendered_doc_comment("///", opencv_version))),
41-
("visibility", visibility.into()),
42-
("name", f.rust_leafname(FishStyle::No)),
43-
("type", typ),
40+
("doc_comment", f.rust_doc_comment("///", opencv_version).as_str()),
41+
("visibility", visibility),
42+
("name", &f.rust_leafname(FishStyle::No)),
43+
("type", &typ),
4444
]),
4545
)
4646
});
@@ -52,7 +52,7 @@ pub fn gen_simple_class(c: &Class, opencv_version: &str) -> String {
5252
impls.add_explicit_clone(c, &rust_local);
5353

5454
SIMPLE_TPL.interpolate(&HashMap::from([
55-
("doc_comment", c.rendered_doc_comment("///", opencv_version).as_str()),
55+
("doc_comment", c.rust_doc_comment("///", opencv_version).as_str()),
5656
("debug", &c.get_debug()),
5757
("rust_local", &rust_local),
5858
("rust_full", &c.rust_name(NameStyle::ref_())),
@@ -168,7 +168,7 @@ pub fn gen_boxed_class(c: &Class, opencv_version: &str) -> String {
168168

169169
let (type_ref_const, type_ref_mut) = make_const_mut(type_ref);
170170
BOXED_TPL.interpolate(&HashMap::from([
171-
("doc_comment", c.rendered_doc_comment("///", opencv_version).as_str()),
171+
("doc_comment", c.rust_doc_comment("///", opencv_version).as_str()),
172172
("debug", &c.get_debug()),
173173
("rust_local", &rust_local),
174174
("rust_decl_lt", &rust_decl_lt),

binding-generator/src/writer/rust_native/constant.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::borrow::Cow::{Borrowed, Owned};
23
use std::collections::HashMap;
34
use std::sync::LazyLock;
45

@@ -40,13 +41,6 @@ impl RustElement for Const<'_> {
4041
},
4142
}
4243
}
43-
44-
fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
45-
match self {
46-
&Self::Clang { entity } => DefaultRustNativeElement::rendered_doc_comment(entity, comment_marker, opencv_version),
47-
Self::Desc(_) => "".to_string(),
48-
}
49-
}
5044
}
5145

5246
impl RustNativeGeneratedElement for Const<'_> {
@@ -76,11 +70,11 @@ impl RustNativeGeneratedElement for Const<'_> {
7670
.unwrap_or(&value.kind)
7771
.rust_type();
7872
RUST_TPL.interpolate(&HashMap::from([
79-
("doc_comment", Cow::Owned(self.rendered_doc_comment("///", opencv_version))),
73+
("doc_comment", Owned(self.rust_doc_comment("///", opencv_version))),
8074
("debug", self.get_debug().into()),
8175
("name", name),
8276
("type", typ.into()),
83-
("value", value.rust_render().into()),
77+
("value", value.rust_render()),
8478
]))
8579
} else {
8680
"".to_string()
@@ -106,28 +100,28 @@ impl ValueKindExt for ValueKind {
106100
}
107101

108102
pub trait ValueExt {
109-
fn rust_render(self) -> String;
103+
fn rust_render(&self) -> Cow<'_, str>;
110104
}
111105

112106
impl ValueExt for Value {
113-
fn rust_render(self) -> String {
107+
fn rust_render(&self) -> Cow<'_, str> {
114108
match self.kind {
115-
ValueKind::Float | ValueKind::Double if !self.value.contains('.') => {
116-
format!("{}.", self.value)
117-
}
109+
ValueKind::Float | ValueKind::Double if !self.value.contains('.') => Owned(format!("{}.", self.value)),
118110
ValueKind::Integer => {
119111
if let Some(no_prefix) = self.value.strip_prefix("0x") {
120112
// todo: use let chain when MSRV is 1.88
121113
if i32::from_str_radix(no_prefix, 16).is_err() {
122-
format!("{}u32 as i32", self.value)
114+
Owned(format!("{}u32 as i32", self.value))
123115
} else {
124-
self.value
116+
Borrowed(&self.value)
125117
}
126118
} else {
127-
self.value
119+
Borrowed(&self.value)
128120
}
129121
}
130-
ValueKind::UnsignedInteger | ValueKind::Usize | ValueKind::Float | ValueKind::Double | ValueKind::String => self.value,
122+
ValueKind::UnsignedInteger | ValueKind::Usize | ValueKind::Float | ValueKind::Double | ValueKind::String => {
123+
Borrowed(&self.value)
124+
}
131125
}
132126
}
133127
}

0 commit comments

Comments
 (0)