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
use std::borrow::Cow;
use std::collections::HashMap;

use once_cell::sync::Lazy;

use crate::debug::NameDebug;
use crate::type_ref::{FishStyle, NameStyle, TypeRefKind};
use crate::{CompiledInterpolation, CppNameStyle, DefaultElement, EntityElement, IteratorExt, StrExt, Typedef};

use super::element::{DefaultRustNativeElement, RustElement};
use super::type_ref::{Lifetime, TypeRefExt};
use super::RustNativeGeneratedElement;

impl RustElement for Typedef<'_, '_> {
	fn rust_module(&self) -> Cow<str> {
		DefaultRustNativeElement::rust_module(self.entity())
	}

	fn rust_name(&self, style: NameStyle) -> Cow<str> {
		DefaultRustNativeElement::rust_name(self, self.entity(), style).into()
	}

	fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
		match self.underlying_type_ref().source().kind().as_ref() {
			TypeRefKind::Class(..)
			| TypeRefKind::Function(..)
			| TypeRefKind::StdVector(..)
			| TypeRefKind::SmartPtr(..)
			| TypeRefKind::StdTuple(..) => DefaultElement::cpp_name(self, self.entity(), CppNameStyle::Declaration),
			_ => DefaultRustNativeElement::rust_leafname(self),
		}
	}

	fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
		DefaultRustNativeElement::rendered_doc_comment(self.entity(), comment_marker, opencv_version)
	}
}

impl RustNativeGeneratedElement for Typedef<'_, '_> {
	fn element_safe_id(&self) -> String {
		format!("{}-{}", self.rust_module(), self.rust_name(NameStyle::decl()))
	}

	fn gen_rust(&self, opencv_version: &str) -> String {
		static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/typedef/tpl.rs").compile_interpolation());
		let underlying_type = self.underlying_type_ref();
		let lifetimes = Lifetime::explicit()
			.into_iter()
			.take(underlying_type.rust_lifetime_count())
			.map(|l| l.to_string())
			.join(", ");
		let generic_args = if lifetimes.is_empty() {
			"".to_string()
		} else {
			format!("<{lifetimes}>")
		};

		TPL.interpolate(&HashMap::from([
			("doc_comment", Cow::Owned(self.rendered_doc_comment("///", opencv_version))),
			("debug", self.get_debug().into()),
			("rust_local", self.rust_name(NameStyle::decl())),
			("generic_args", generic_args.into()),
			(
				"definition",
				underlying_type.rust_name_ext(NameStyle::ref_(), Lifetime::explicit()),
			),
		]))
	}
}