use core::fmt;
use ::rust_alloc::sync::Arc;
use crate as rune;
use crate::alloc;
use crate::alloc::prelude::*;
use crate::compile::context::{AttributeMacroHandler, MacroHandler, TraitHandler};
use crate::compile::{meta, Docs};
use crate::function_meta::AssociatedName;
use crate::runtime::{ConstValue, FieldMap, FunctionHandler, TypeInfo};
use crate::{Hash, ItemBuf};
#[doc(hidden)]
pub struct ModuleMetaData {
#[doc(hidden)]
pub item: ItemBuf,
#[doc(hidden)]
pub docs: &'static [&'static str],
}
pub type ModuleMeta = fn() -> alloc::Result<ModuleMetaData>;
pub(crate) struct ModuleType {
pub(crate) item: ItemBuf,
pub(crate) hash: Hash,
pub(crate) common: ModuleItemCommon,
pub(crate) type_parameters: Hash,
pub(crate) type_info: TypeInfo,
pub(crate) spec: Option<TypeSpecification>,
pub(crate) constructor: Option<Arc<FunctionHandler>>,
}
pub(crate) struct ModuleTrait {
pub(crate) item: ItemBuf,
pub(crate) hash: Hash,
pub(crate) common: ModuleItemCommon,
pub(crate) handler: Option<Arc<TraitHandler>>,
pub(crate) functions: Vec<TraitFunction>,
}
pub(crate) struct ModuleTraitImpl {
pub(crate) item: ItemBuf,
pub(crate) hash: Hash,
pub(crate) type_info: TypeInfo,
pub(crate) trait_item: ItemBuf,
pub(crate) trait_hash: Hash,
}
pub(crate) struct ModuleReexport {
pub(crate) item: ItemBuf,
pub(crate) hash: Hash,
pub(crate) to: ItemBuf,
}
#[derive(Debug)]
pub(crate) enum Fields {
Named(&'static [&'static str]),
Unnamed(usize),
Empty,
}
impl Fields {
#[inline]
fn size(&self) -> usize {
match self {
Fields::Named(fields) => fields.len(),
_ => 0,
}
}
#[inline]
pub(crate) fn to_fields(&self) -> alloc::Result<FieldMap<Box<str>, usize>> {
let mut fields = crate::runtime::new_field_hash_map_with_capacity(self.size())?;
if let Fields::Named(names) = self {
for (index, name) in names.iter().copied().enumerate() {
fields.try_insert(name.try_into()?, index)?;
}
}
Ok(fields)
}
}
pub struct Variant {
pub(crate) name: &'static str,
pub(crate) fields: Option<Fields>,
pub(crate) constructor: Option<Arc<FunctionHandler>>,
pub(crate) deprecated: Option<Box<str>>,
pub(crate) docs: Docs,
}
impl Variant {
pub(super) fn new(name: &'static str) -> Self {
Self {
name,
fields: None,
constructor: None,
deprecated: None,
docs: Docs::EMPTY,
}
}
}
impl fmt::Debug for Variant {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("Variant");
f.field("fields", &self.fields);
f.field("constructor", &self.constructor.is_some());
#[cfg(feature = "doc")]
f.field("deprecated", &self.deprecated);
f.field("docs", &self.docs);
f.finish()
}
}
pub(crate) struct Enum {
pub(crate) variants: Vec<Variant>,
}
pub(crate) enum TypeSpecification {
Struct(Fields),
Enum(Enum),
}
#[derive(Debug, TryClone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub(crate) struct AssociatedKey {
pub(crate) type_hash: Hash,
pub(crate) kind: meta::AssociatedKind,
pub(crate) parameters: Hash,
}
pub(crate) enum ModuleItemKind {
Constant(ConstValue),
Function(ModuleFunction),
Macro(ModuleMacro),
AttributeMacro(ModuleAttributeMacro),
}
pub(crate) struct ModuleItem {
pub(crate) item: ItemBuf,
pub(crate) hash: Hash,
pub(crate) common: ModuleItemCommon,
pub(crate) kind: ModuleItemKind,
}
#[derive(Default, TryClone)]
pub(crate) struct DocFunction {
#[cfg(feature = "doc")]
#[try_clone(copy)]
pub(crate) is_async: bool,
#[cfg(feature = "doc")]
#[try_clone(copy)]
pub(crate) args: Option<usize>,
#[cfg(feature = "doc")]
pub(crate) argument_types: Box<[meta::DocType]>,
#[cfg(feature = "doc")]
pub(crate) return_type: meta::DocType,
}
#[derive(TryClone)]
pub(crate) struct ModuleFunction {
pub(crate) handler: Arc<FunctionHandler>,
pub(crate) trait_hash: Option<Hash>,
pub(crate) doc: DocFunction,
}
#[derive(TryClone)]
pub(crate) enum ModuleAssociatedKind {
Constant(ConstValue),
Function(ModuleFunction),
}
#[derive(Default, TryClone)]
pub(crate) struct ModuleItemCommon {
pub(crate) docs: Docs,
pub(crate) deprecated: Option<Box<str>>,
}
#[derive(TryClone)]
pub(crate) struct ModuleAssociated {
pub(crate) container: Hash,
pub(crate) container_type_info: TypeInfo,
pub(crate) name: AssociatedName,
pub(crate) common: ModuleItemCommon,
pub(crate) kind: ModuleAssociatedKind,
}
pub(crate) struct ModuleMacro {
pub(crate) handler: Arc<MacroHandler>,
}
pub(crate) struct ModuleAttributeMacro {
pub(crate) handler: Arc<AttributeMacroHandler>,
}
pub(crate) struct TraitFunction {
pub(crate) name: AssociatedName,
pub(crate) common: ModuleItemCommon,
pub(crate) doc: DocFunction,
}