import { ToMetadata } from "." ; import { IVariant , IVariantDef , ICompositeDef , ITupleDef , IArrayDef , ISequenceDef , IPrimitiveDef , IField , ITypeDef , } from "./specs" ; export enum TypeKind { Primitive , Tuple , Array , Sequence , Composite , Variant , Compact , Phantom } export interface Type extends ToMetadata { typeKind ( ) : TypeKind ; toMetadata ( ) : ITypeDef ; } export class PrimitiveDef implements Type { constructor ( public readonly primitive ) { } typeKind ( ) { return TypeKind . Primitive ; } toMetadata ( ) { return { def : { primitive : this . primitive , } , } ; } } export class TupleDef implements Type { constructor ( public readonly fields ) { } typeKind ( ) { return TypeKind . Tuple ; } toMetadata ( ) { return { def : { tuple : { fields : this . fields , } , } , } ; } } export class ArrayDef implements Type { constructor ( public readonly len , public readonly type ) { } typeKind ( ) { return TypeKind . Array ; } toMetadata ( ) { return { def : { array : { len : this . len , type : this . type , } , } , } ; } } export class SequenceDef implements Type { constructor ( public readonly type ) { } typeKind ( ) { return TypeKind . Sequence ; } toMetadata ( ) { return { def : { sequence : { type : this . type , } , } , } ; } } export class CompositeDef implements Type { constructor ( public readonly fields ) { } typeKind ( ) { return TypeKind . Composite ; } toMetadata ( ) { return { def : { composite : { fields : this . fields . map ( ( f ) => f . toMetadata ( ) ) , } , } , } ; } } export class VariantDef implements Type { constructor ( public readonly variants ) { } typeKind ( ) { return TypeKind . Variant ; } toMetadata ( ) { return { def : { variants : this . variants . map ( ( v ) => v . toMetadata ( ) ) , } , } ; } } export class Variant implements ToMetadata { constructor ( public readonly name , public readonly fields , public readonly discriminant ) { } toMetadata ( ) { return { name : this . name , fields : this . fields . map ( ( f ) => f . toMetadata ( ) ) , discriminant : this . discriminant , } ; } } export class Field implements ToMetadata { constructor ( public readonly name , public readonly type ) { } toMetadata ( ) { return this . name == null ? { type : this . type } : this ; } }