import { ToMetadata } from "./index" ; import { IHashLayout , IArrayLayout , ILayout } from "./specs" ; import { IFieldLayout , IStructLayout , ICellLayout , IHashingStrategy , } from "./specs" ; export enum LayoutKind { Cell , Hash , Array , Struct , Enum } export enum CryptoHasher { Blake2x256 = "Blake2x256" , Sha2x256 = "Sha2x256" , Keccak256 = "Keccak256" } export interface Layout extends ToMetadata { layoutKind ( ) : LayoutKind ; toMetadata ( ) : ILayout ; } export class StructLayout implements Layout { constructor ( public readonly fields ) { } layoutKind ( ) { return LayoutKind . Struct ; } toMetadata ( ) { return { struct : { fields : this . fields . map ( ( f ) => f . toMetadata ( ) ) } } ; } } export class FieldLayout implements ToMetadata { constructor ( public readonly name , public readonly layout ) { } toMetadata ( ) { return { name : this . name , layout : this . layout . toMetadata ( ) , } ; } } export class HashingStrategy implements ToMetadata { constructor ( public readonly hasher , public readonly prefix , public readonly postfix ) { } public toMetadata ( ) { return { hasher : this . hasher . toString ( ) , prefix : this . prefix , postfix : this . postfix , } ; } } export type LayoutKey = string ; export class CellLayout implements Layout { constructor ( public readonly key , public readonly ty ) { } layoutKind ( ) { return LayoutKind . Cell ; } public toMetadata ( ) { return { key : this . key , ty : this . ty , } ; } } export class HashLayout implements Layout { constructor ( public readonly offset , public readonly strategy , public readonly layout ) { } public layoutKind ( ) { return LayoutKind . Hash ; } public toMetadata ( ) { return { offset : this . offset , strategy : this . strategy . toMetadata ( ) , layout : this . layout . toMetadata ( ) , } ; } } export class ArrayLayout implements Layout { constructor ( public readonly offset , public readonly len , public readonly cellsPerElem , public readonly layout ) { } public layoutKind ( ) { return LayoutKind . Array ; } public toMetadata ( ) { return { offset : this . offset . toString ( ) , len : this . len , cellsPerElem : this . cellsPerElem , layout : this . layout . toMetadata ( ) , } ; } }