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
use std::{ borrow::Cow, collections::HashSet, fmt, }; use clang::{Entity, EntityKind, EntityVisitResult}; use maplit::hashmap; use once_cell::sync::Lazy; use crate::{ CompiledInterpolation, Const, DefaultElement, Element, EntityElement, GeneratedElement, get_debug, StrExt, }; #[derive(Clone)] pub struct Enum<'tu> { entity: Entity<'tu>, } impl<'tu> Enum<'tu> { pub fn new(entity: Entity<'tu>) -> Self { Self { entity } } pub fn consts(&self) -> Vec<Const> { let mut out = vec![]; self.entity.visit_children(|const_decl, _| { if const_decl.get_kind() == EntityKind::EnumConstantDecl { out.push(Const::new(const_decl)); } EntityVisitResult::Continue }); out } } impl<'tu> EntityElement<'tu> for Enum<'tu> { fn entity(&self) -> Entity<'tu> { self.entity } } impl Element for Enum<'_> { fn is_system(&self) -> bool { DefaultElement::is_system(self) } fn is_public(&self) -> bool { DefaultElement::is_public(self) } fn usr(&self) -> Cow<str> { DefaultElement::usr(self) } fn rendered_doc_comment_with_prefix(&self, prefix: &str, opencv_version: &str) -> String { DefaultElement::rendered_doc_comment_with_prefix(self, prefix, opencv_version) } fn cpp_namespace(&self) -> Cow<str> { DefaultElement::cpp_namespace(self) } fn cpp_localname(&self) -> Cow<str> { DefaultElement::cpp_localname(self) } fn rust_module(&self) -> Cow<str> { DefaultElement::rust_module(self) } fn rust_leafname(&self) -> Cow<str> { self.cpp_localname() } fn rust_localname(&self) -> Cow<str> { DefaultElement::rust_localname(self) } } impl GeneratedElement for Enum<'_> { fn element_safe_id(&self) -> String { format!("{}-{}", self.rust_module(), self.rust_localname()) } fn gen_rust(&self, opencv_version: &str) -> String { static ENUM_TPL: Lazy<CompiledInterpolation> = Lazy::new( || include_str!("../tpl/enum/enum.tpl.ru.rs").compile_interpolation() ); static CONST_TPL: Lazy<CompiledInterpolation> = Lazy::new( || include_str!("../tpl/enum/const.tpl.ru.rs").compile_interpolation() ); static CONST_IGNORED_TPL: Lazy<CompiledInterpolation> = Lazy::new( || include_str!("../tpl/enum/const_ignored.tpl.ru.rs").compile_interpolation() ); let consts = self.consts(); let mut generated_values = HashSet::with_capacity(consts.len()); let consts = consts.into_iter() .map(|c| { let name = c.rust_leafname(); let value = c.value().expect("Can't get value of enum variant").to_string(); let is_ignored = generated_values.contains(&value); let tpl = if is_ignored { &CONST_IGNORED_TPL } else { &CONST_TPL }; let doc_comment = if is_ignored { c.rendered_doc_comment_with_prefix("//", opencv_version) } else { c.rendered_doc_comment(opencv_version) }; generated_values.insert(value.clone()); tpl.interpolate(&hashmap! { "doc_comment" => doc_comment, "name" => name.into_owned(), "value" => value, }) }).collect::<Vec<_>>(); ENUM_TPL.interpolate(&hashmap! { "doc_comment" => self.rendered_doc_comment(opencv_version), "debug" => get_debug(self), "rust_local" => self.rust_localname().into_owned(), "consts" => consts.join(""), }) } } impl fmt::Display for Enum<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.entity.get_display_name().unwrap_or_else(|| "unnamed".to_string())) } } impl fmt::Debug for Enum<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let mut debug_struct = f.debug_struct("Enum"); self.update_debug_struct(&mut debug_struct) .field("consts", &self.consts()) .finish() } }