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
use std::borrow::Cow;
use std::fmt::Debug;

use clang::{Entity, EntityKind};

use crate::type_ref::FishStyle;
use crate::{
	opencv_module_from_path, reserved_rename, settings, CppNameStyle, Element, GeneratedType, IteratorExt, NameStyle, StringExt,
};

use super::comment::RenderComment;

pub struct DefaultRustNativeElement;

impl DefaultRustNativeElement {
	pub fn rust_module(entity: Entity) -> Cow<'static, str> {
		let loc = entity
			.get_location()
			.expect("Can't get location")
			.get_spelling_location()
			.file
			.expect("Can't file")
			.get_path();
		opencv_module_from_path(&loc).map_or_else(|| "core".into(), |x| x.to_string().into())
	}

	pub fn rust_module_reference(this: &(impl RustElement + ?Sized)) -> Cow<str> {
		let module = this.rust_module();
		if settings::STATIC_MODULES.contains(module.as_ref()) {
			module
		} else {
			format!("crate::{}", module).into()
		}
	}

	pub fn rust_leafname(this: &(impl Element + ?Sized)) -> Cow<str> {
		reserved_rename(this.cpp_name(CppNameStyle::Declaration))
	}

	pub fn rust_name(this: &(impl RustElement + ?Sized), entity: Entity, name_style: NameStyle) -> String {
		let mut parts = Vec::with_capacity(4);
		parts.push(this.rust_leafname(name_style.turbo_fish_style()));
		let mut entity = entity;
		let module = Self::rust_module(entity);
		while let Some(parent) = entity.get_semantic_parent() {
			match parent.get_kind() {
				EntityKind::ClassDecl | EntityKind::StructDecl | EntityKind::ClassTemplate => {
					let parent_name = parent.get_name().expect("Can't get parent name");
					if parts.last().map_or(true, |last| last != &parent_name) {
						parts.push(parent_name.into());
					}
				}
				EntityKind::EnumDecl => {
					if parent.is_scoped() {
						parts.push(parent.get_name().expect("Can't get parent name").into());
					}
				}
				EntityKind::TranslationUnit | EntityKind::UnexposedDecl | EntityKind::FunctionTemplate => {
					break;
				}
				EntityKind::Namespace => {
					let parent_namespace = parent.get_name().expect("Can't get parent name");
					let no_skip_prefix = settings::NO_SKIP_NAMESPACE_IN_LOCALNAME
						.get(module.as_ref())
						.and_then(|module_specific| module_specific.get(parent_namespace.as_str()))
						.or_else(|| {
							settings::NO_SKIP_NAMESPACE_IN_LOCALNAME
								.get("*")
								.and_then(|generic| generic.get(parent_namespace.as_str()))
						});
					if let Some(&prefix) = no_skip_prefix {
						parts.push(prefix.into());
					} else {
						break;
					}
				}
				EntityKind::Constructor | EntityKind::FunctionDecl | EntityKind::Method | EntityKind::NotImplemented => {}
				_ => {
					unreachable!("Can't get kind of parent: {parent:#?} for element: {entity:#?}")
				}
			}
			entity = parent;
		}
		let decl_name = parts.into_iter().rev().join("_");
		match name_style {
			NameStyle::Declaration => decl_name,
			NameStyle::Reference(_) => {
				let mut out = this.rust_module_reference().into_owned();
				out.extend_sep("::", &decl_name);
				out
			}
		}
	}

	pub fn rendered_doc_comment(entity: Entity, comment_marker: &str, opencv_version: &str) -> String {
		RenderComment::new(&entity.doc_comment(), opencv_version)
			.render_with_comment_marker(comment_marker)
			.into_owned()
	}
}

pub trait RustNativeGeneratedElement {
	/// Element order in the output file, lower means earlier
	fn element_order(&self) -> u8 {
		50
	}

	fn element_safe_id(&self) -> String;

	fn gen_rust(&self, _opencv_version: &str) -> String {
		"".to_string()
	}

	fn gen_rust_externs(&self) -> String {
		"".to_string()
	}

	fn gen_cpp(&self) -> String {
		"".to_string()
	}
}

pub trait RustElement: Element {
	fn rust_module(&self) -> Cow<str>;

	fn rust_module_reference(&self) -> Cow<str> {
		DefaultRustNativeElement::rust_module_reference(self)
	}

	fn rust_name(&self, style: NameStyle) -> Cow<str>;

	/// The very last concrete part of the name in Rust
	///
	/// This might not match `rust_name(NameStyle::Declaration)` because some classes in Rust are prefixed with their namespace. E.g.
	/// `Detail_Blender`, in this case the `rust_leafname()` == `Blender` and `rust_name(NameStyle::Declaration)` == `Detail_Blender`.
	fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
		DefaultRustNativeElement::rust_leafname(self)
	}

	fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String;
}

pub trait DebugRust {
	type DebugType: Debug;

	fn dbg_rust(self) -> Self::DebugType;
}

impl<'ne, 'tu: 'ne, 'ge: 'ne> AsRef<dyn RustNativeGeneratedElement + 'ne> for GeneratedType<'tu, 'ge> {
	fn as_ref(&self) -> &(dyn RustNativeGeneratedElement + 'ne) {
		match self {
			GeneratedType::Vector(vec) => vec,
			GeneratedType::SmartPtr(ptr) => ptr,
			GeneratedType::Tuple(tuple) => tuple,
			GeneratedType::AbstractRefWrapper(aref) => aref,
		}
	}
}