57 lines
1.8 KiB
Rust
57 lines
1.8 KiB
Rust
|
use super::TypeNamespaceId;
|
||
|
|
||
|
use super::GlobalNamespace;
|
||
|
|
||
|
use super::r#trait::Trait;
|
||
|
use super::r#type::Type;
|
||
|
|
||
|
pub enum TypeDef<'a> {
|
||
|
Type(Type<'a>),
|
||
|
Trait(Trait<'a>),
|
||
|
List(Vec<TypeDef<'a>>),
|
||
|
Record(Vec<(String, TypeDef<'a>)>),
|
||
|
}
|
||
|
|
||
|
impl<'a> TypeDef<'a> {
|
||
|
pub(super) fn from_internal(ns: &'a GlobalNamespace, def: &InternalTypeDef) -> TypeDef<'a> {
|
||
|
match def {
|
||
|
InternalTypeDef::Single(id) => match id {
|
||
|
// safe to unwrap because this is only used with internal representations
|
||
|
TypeNamespaceId::Types(id) => TypeDef::Type(ns.get_type(*id).unwrap()),
|
||
|
TypeNamespaceId::Traits(id) => TypeDef::Trait(ns.get_trait(*id).unwrap()),
|
||
|
},
|
||
|
InternalTypeDef::List(list) => TypeDef::List(
|
||
|
list.into_iter()
|
||
|
.map(|def| Self::from_internal(ns, def))
|
||
|
.collect(),
|
||
|
),
|
||
|
InternalTypeDef::Record(rec) => TypeDef::Record(
|
||
|
rec.into_iter()
|
||
|
.map(|(name, def)| (name.clone(), Self::from_internal(ns, def)))
|
||
|
.collect(),
|
||
|
),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub(super) enum InternalTypeDef {
|
||
|
Single(TypeNamespaceId),
|
||
|
List(Vec<InternalTypeDef>),
|
||
|
Record(Vec<(String, InternalTypeDef)>),
|
||
|
}
|
||
|
|
||
|
impl From<TypeDef<'_>> for InternalTypeDef {
|
||
|
fn from(value: TypeDef) -> Self {
|
||
|
match value {
|
||
|
TypeDef::Type(val) => Self::Single(TypeNamespaceId::Types(val.id)),
|
||
|
TypeDef::Trait(val) => Self::Single(TypeNamespaceId::Traits(val.id)),
|
||
|
TypeDef::List(list) => Self::List(list.into_iter().map(|def| def.into()).collect()),
|
||
|
TypeDef::Record(rec) => Self::Record(
|
||
|
rec.into_iter()
|
||
|
.map(|(name, typ)| (name, typ.into()))
|
||
|
.collect(),
|
||
|
),
|
||
|
}
|
||
|
}
|
||
|
}
|