1use core::fmt;
2
3use crate as rune;
4use crate::alloc;
5use crate::alloc::prelude::*;
6use crate::compile::context::{AttributeMacroHandler, MacroHandler, TraitHandler};
7use crate::compile::{meta, Docs};
8use crate::function_meta::AssociatedName;
9use crate::runtime::{ConstValue, FieldMap, FunctionHandler, TypeInfo};
10use crate::{Hash, Item, ItemBuf};
11
12#[doc(hidden)]
16pub struct ModuleMetaData {
17 #[doc(hidden)]
18 pub item: &'static Item,
19 #[doc(hidden)]
20 pub docs: &'static [&'static str],
21}
22
23pub type ModuleMeta = fn() -> alloc::Result<ModuleMetaData>;
34
35pub(crate) struct ModuleType {
38 pub(crate) item: ItemBuf,
41 pub(crate) hash: Hash,
43 pub(crate) common: ModuleItemCommon,
45 pub(crate) type_parameters: Hash,
47 pub(crate) type_info: TypeInfo,
49 pub(crate) spec: Option<TypeSpecification>,
51 pub(crate) constructor: Option<TypeConstructor>,
53}
54
55pub(crate) struct ModuleTrait {
57 pub(crate) item: ItemBuf,
58 pub(crate) hash: Hash,
59 pub(crate) common: ModuleItemCommon,
60 pub(crate) handler: Option<TraitHandler>,
61 pub(crate) functions: Vec<TraitFunction>,
62}
63
64pub(crate) struct ModuleTraitImpl {
66 pub(crate) item: ItemBuf,
67 pub(crate) hash: Hash,
68 pub(crate) type_info: TypeInfo,
69 pub(crate) trait_item: ItemBuf,
70 pub(crate) trait_hash: Hash,
71}
72
73pub(crate) struct ModuleReexport {
75 pub(crate) item: ItemBuf,
76 pub(crate) hash: Hash,
77 pub(crate) to: ItemBuf,
78}
79
80#[derive(Debug)]
82pub(crate) enum Fields {
83 Named(&'static [&'static str]),
85 Unnamed(usize),
87 Empty,
89}
90
91impl Fields {
92 #[inline]
94 pub(crate) fn len(&self) -> usize {
95 match self {
96 Fields::Named(fields) => fields.len(),
97 Fields::Unnamed(size) => *size,
98 Fields::Empty => 0,
99 }
100 }
101
102 #[inline]
104 fn size(&self) -> usize {
105 match self {
106 Fields::Named(fields) => fields.len(),
107 _ => 0,
108 }
109 }
110
111 #[inline]
113 pub(crate) fn to_fields(&self) -> alloc::Result<FieldMap<Box<str>, usize>> {
114 let mut fields = crate::runtime::new_field_hash_map_with_capacity(self.size())?;
115
116 if let Fields::Named(names) = self {
117 for (index, name) in names.iter().copied().enumerate() {
118 fields.try_insert(name.try_into()?, index)?;
119 }
120 }
121
122 Ok(fields)
123 }
124}
125
126pub struct Variant {
128 pub(crate) name: &'static str,
130 pub(crate) fields: Option<Fields>,
132 pub(crate) constructor: Option<TypeConstructor>,
134 pub(crate) deprecated: Option<Box<str>>,
136 pub(crate) docs: Docs,
138}
139
140impl Variant {
141 pub(super) fn new(name: &'static str) -> Self {
142 Self {
143 name,
144 fields: None,
145 constructor: None,
146 deprecated: None,
147 docs: Docs::EMPTY,
148 }
149 }
150}
151
152impl fmt::Debug for Variant {
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 let mut f = f.debug_struct("Variant");
155 f.field("fields", &self.fields);
156 f.field("constructor", &self.constructor.is_some());
157 #[cfg(feature = "doc")]
158 f.field("deprecated", &self.deprecated);
159 f.field("docs", &self.docs);
160 f.finish()
161 }
162}
163
164pub(crate) struct Enum {
166 pub(crate) variants: Vec<Variant>,
168}
169
170pub(crate) enum TypeSpecification {
172 Struct(Fields),
173 Enum(Enum),
174}
175
176pub(crate) struct TypeConstructor {
178 pub(crate) handler: FunctionHandler,
180 pub(crate) args: usize,
182}
183
184#[derive(Debug, TryClone, PartialEq, Eq, Hash)]
186#[non_exhaustive]
187pub(crate) struct AssociatedKey {
188 pub(crate) type_hash: Hash,
190 pub(crate) kind: meta::AssociatedKind,
192 pub(crate) parameters: Hash,
194}
195
196pub(crate) enum ModuleItemKind {
197 Constant(ConstValue),
198 Function(ModuleFunction),
199 Macro(ModuleMacro),
200 AttributeMacro(ModuleAttributeMacro),
201}
202
203pub(crate) struct ModuleItem {
204 pub(crate) item: ItemBuf,
205 pub(crate) hash: Hash,
206 pub(crate) common: ModuleItemCommon,
207 pub(crate) kind: ModuleItemKind,
208}
209
210#[derive(Default, TryClone)]
211pub(crate) struct DocFunction {
212 #[cfg(feature = "doc")]
213 #[try_clone(copy)]
214 pub(crate) is_async: bool,
215 #[cfg(feature = "doc")]
216 #[try_clone(copy)]
217 pub(crate) args: Option<usize>,
218 #[cfg(feature = "doc")]
219 pub(crate) argument_types: Box<[meta::DocType]>,
220 #[cfg(feature = "doc")]
221 pub(crate) return_type: meta::DocType,
222}
223
224#[derive(TryClone)]
225pub(crate) struct ModuleFunction {
226 pub(crate) handler: FunctionHandler,
228 pub(crate) trait_hash: Option<Hash>,
230 pub(crate) doc: DocFunction,
232}
233
234#[derive(TryClone)]
235pub(crate) enum ModuleAssociatedKind {
236 Constant(ConstValue),
237 Function(ModuleFunction),
238}
239
240#[derive(Default, TryClone)]
241pub(crate) struct ModuleItemCommon {
242 pub(crate) docs: Docs,
244 pub(crate) deprecated: Option<Box<str>>,
246}
247
248#[derive(TryClone)]
249pub(crate) struct ModuleAssociated {
250 pub(crate) container: Hash,
251 pub(crate) container_type_info: TypeInfo,
252 pub(crate) name: AssociatedName,
253 pub(crate) common: ModuleItemCommon,
254 pub(crate) kind: ModuleAssociatedKind,
255}
256
257pub(crate) struct ModuleMacro {
259 pub(crate) handler: MacroHandler,
260}
261
262pub(crate) struct ModuleAttributeMacro {
264 pub(crate) handler: AttributeMacroHandler,
265}
266
267pub(crate) struct TraitFunction {
269 pub(crate) name: AssociatedName,
270 pub(crate) common: ModuleItemCommon,
271 pub(crate) doc: DocFunction,
272}