1use core::fmt;
2
3use ::rust_alloc::sync::Arc;
4
5use crate as rune;
6use crate::alloc;
7use crate::alloc::prelude::*;
8use crate::compile::context::{AttributeMacroHandler, MacroHandler, TraitHandler};
9use crate::compile::{meta, Docs};
10use crate::function_meta::AssociatedName;
11use crate::runtime::{ConstValue, FieldMap, FunctionHandler, TypeInfo};
12use crate::{Hash, ItemBuf};
13
14#[doc(hidden)]
15pub struct ModuleMetaData {
16 #[doc(hidden)]
17 pub item: ItemBuf,
18 #[doc(hidden)]
19 pub docs: &'static [&'static str],
20}
21
22pub type ModuleMeta = fn() -> alloc::Result<ModuleMetaData>;
33
34pub(crate) struct ModuleType {
37 pub(crate) item: ItemBuf,
40 pub(crate) hash: Hash,
42 pub(crate) common: ModuleItemCommon,
44 pub(crate) type_parameters: Hash,
46 pub(crate) type_info: TypeInfo,
48 pub(crate) spec: Option<TypeSpecification>,
50 pub(crate) constructor: Option<Arc<FunctionHandler>>,
52}
53
54pub(crate) struct ModuleTrait {
56 pub(crate) item: ItemBuf,
57 pub(crate) hash: Hash,
58 pub(crate) common: ModuleItemCommon,
59 pub(crate) handler: Option<Arc<TraitHandler>>,
60 pub(crate) functions: Vec<TraitFunction>,
61}
62
63pub(crate) struct ModuleTraitImpl {
65 pub(crate) item: ItemBuf,
66 pub(crate) hash: Hash,
67 pub(crate) type_info: TypeInfo,
68 pub(crate) trait_item: ItemBuf,
69 pub(crate) trait_hash: Hash,
70}
71
72pub(crate) struct ModuleReexport {
74 pub(crate) item: ItemBuf,
75 pub(crate) hash: Hash,
76 pub(crate) to: ItemBuf,
77}
78
79#[derive(Debug)]
81pub(crate) enum Fields {
82 Named(&'static [&'static str]),
84 Unnamed(usize),
86 Empty,
88}
89
90impl Fields {
91 #[inline]
93 fn size(&self) -> usize {
94 match self {
95 Fields::Named(fields) => fields.len(),
96 _ => 0,
97 }
98 }
99
100 #[inline]
102 pub(crate) fn to_fields(&self) -> alloc::Result<FieldMap<Box<str>, usize>> {
103 let mut fields = crate::runtime::new_field_hash_map_with_capacity(self.size())?;
104
105 if let Fields::Named(names) = self {
106 for (index, name) in names.iter().copied().enumerate() {
107 fields.try_insert(name.try_into()?, index)?;
108 }
109 }
110
111 Ok(fields)
112 }
113}
114
115pub struct Variant {
117 pub(crate) name: &'static str,
119 pub(crate) fields: Option<Fields>,
121 pub(crate) constructor: Option<Arc<FunctionHandler>>,
123 pub(crate) deprecated: Option<Box<str>>,
125 pub(crate) docs: Docs,
127}
128
129impl Variant {
130 pub(super) fn new(name: &'static str) -> Self {
131 Self {
132 name,
133 fields: None,
134 constructor: None,
135 deprecated: None,
136 docs: Docs::EMPTY,
137 }
138 }
139}
140
141impl fmt::Debug for Variant {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 let mut f = f.debug_struct("Variant");
144 f.field("fields", &self.fields);
145 f.field("constructor", &self.constructor.is_some());
146 #[cfg(feature = "doc")]
147 f.field("deprecated", &self.deprecated);
148 f.field("docs", &self.docs);
149 f.finish()
150 }
151}
152
153pub(crate) struct Enum {
155 pub(crate) variants: Vec<Variant>,
157}
158
159pub(crate) enum TypeSpecification {
161 Struct(Fields),
162 Enum(Enum),
163}
164
165#[derive(Debug, TryClone, PartialEq, Eq, Hash)]
167#[non_exhaustive]
168pub(crate) struct AssociatedKey {
169 pub(crate) type_hash: Hash,
171 pub(crate) kind: meta::AssociatedKind,
173 pub(crate) parameters: Hash,
175}
176
177pub(crate) enum ModuleItemKind {
178 Constant(ConstValue),
179 Function(ModuleFunction),
180 Macro(ModuleMacro),
181 AttributeMacro(ModuleAttributeMacro),
182}
183
184pub(crate) struct ModuleItem {
185 pub(crate) item: ItemBuf,
186 pub(crate) hash: Hash,
187 pub(crate) common: ModuleItemCommon,
188 pub(crate) kind: ModuleItemKind,
189}
190
191#[derive(Default, TryClone)]
192pub(crate) struct DocFunction {
193 #[cfg(feature = "doc")]
194 #[try_clone(copy)]
195 pub(crate) is_async: bool,
196 #[cfg(feature = "doc")]
197 #[try_clone(copy)]
198 pub(crate) args: Option<usize>,
199 #[cfg(feature = "doc")]
200 pub(crate) argument_types: Box<[meta::DocType]>,
201 #[cfg(feature = "doc")]
202 pub(crate) return_type: meta::DocType,
203}
204
205#[derive(TryClone)]
206pub(crate) struct ModuleFunction {
207 pub(crate) handler: Arc<FunctionHandler>,
209 pub(crate) trait_hash: Option<Hash>,
211 pub(crate) doc: DocFunction,
213}
214
215#[derive(TryClone)]
216pub(crate) enum ModuleAssociatedKind {
217 Constant(ConstValue),
218 Function(ModuleFunction),
219}
220
221#[derive(Default, TryClone)]
222pub(crate) struct ModuleItemCommon {
223 pub(crate) docs: Docs,
225 pub(crate) deprecated: Option<Box<str>>,
227}
228
229#[derive(TryClone)]
230pub(crate) struct ModuleAssociated {
231 pub(crate) container: Hash,
232 pub(crate) container_type_info: TypeInfo,
233 pub(crate) name: AssociatedName,
234 pub(crate) common: ModuleItemCommon,
235 pub(crate) kind: ModuleAssociatedKind,
236}
237
238pub(crate) struct ModuleMacro {
240 pub(crate) handler: Arc<MacroHandler>,
241}
242
243pub(crate) struct ModuleAttributeMacro {
245 pub(crate) handler: Arc<AttributeMacroHandler>,
246}
247
248pub(crate) struct TraitFunction {
250 pub(crate) name: AssociatedName,
251 pub(crate) common: ModuleItemCommon,
252 pub(crate) doc: DocFunction,
253}