use crate::collections::HashSet;
use crate::{Hash, Item, ValueType};
use std::fmt;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct MetaClosureCapture {
pub ident: String,
}
#[derive(Debug, Clone)]
pub enum Meta {
MetaTuple {
value_type: ValueType,
tuple: MetaTuple,
},
MetaVariantTuple {
value_type: ValueType,
enum_item: Item,
tuple: MetaTuple,
},
MetaStruct {
value_type: ValueType,
object: MetaStruct,
},
MetaVariantStruct {
value_type: ValueType,
enum_item: Item,
object: MetaStruct,
},
MetaEnum {
value_type: ValueType,
item: Item,
},
MetaFunction {
value_type: ValueType,
item: Item,
},
MetaClosure {
value_type: ValueType,
item: Item,
captures: Arc<Vec<MetaClosureCapture>>,
},
MetaAsyncBlock {
value_type: ValueType,
item: Item,
captures: Arc<Vec<MetaClosureCapture>>,
},
}
impl Meta {
pub fn item(&self) -> &Item {
match self {
Meta::MetaTuple { tuple, .. } => &tuple.item,
Meta::MetaVariantTuple { tuple, .. } => &tuple.item,
Meta::MetaStruct { object, .. } => &object.item,
Meta::MetaVariantStruct { object, .. } => &object.item,
Meta::MetaEnum { item, .. } => item,
Meta::MetaFunction { item, .. } => item,
Meta::MetaClosure { item, .. } => item,
Meta::MetaAsyncBlock { item, .. } => item,
}
}
pub fn value_type(&self) -> Option<ValueType> {
match self {
Self::MetaTuple { value_type, .. } => Some(*value_type),
Self::MetaVariantTuple { .. } => None,
Self::MetaStruct { value_type, .. } => Some(*value_type),
Self::MetaVariantStruct { .. } => None,
Self::MetaEnum { value_type, .. } => Some(*value_type),
Self::MetaFunction { value_type, .. } => Some(*value_type),
Self::MetaClosure { value_type, .. } => Some(*value_type),
Self::MetaAsyncBlock { value_type, .. } => Some(*value_type),
}
}
}
impl fmt::Display for Meta {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::MetaTuple { tuple, .. } => {
write!(fmt, "struct {}", tuple.item)?;
}
Self::MetaVariantTuple { tuple, .. } => {
write!(fmt, "variant {}", tuple.item)?;
}
Self::MetaStruct { object, .. } => {
write!(fmt, "struct {}", object.item)?;
}
Self::MetaVariantStruct { object, .. } => {
write!(fmt, "variant {}", object.item)?;
}
Self::MetaEnum { item, .. } => {
write!(fmt, "enum {}", item)?;
}
Self::MetaFunction { item, .. } => {
write!(fmt, "fn {}", item)?;
}
Self::MetaClosure { item, .. } => {
write!(fmt, "closure {}", item)?;
}
Self::MetaAsyncBlock { item, .. } => {
write!(fmt, "async block {}", item)?;
}
}
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct MetaExternal {
pub item: Item,
}
#[derive(Debug, Clone)]
pub struct MetaStruct {
pub item: Item,
pub fields: Option<HashSet<String>>,
}
#[derive(Debug, Clone)]
pub struct MetaTuple {
pub item: Item,
pub args: usize,
pub hash: Hash,
}