import { StackItemLike } from "../sc" ; import { Fixed8 , hexstring2str } from "../u" ; /**
 * RPC Response from test invokes
 */ export interface RPCVMResponse { script : string ; state : "HALT, BREAK" | "FAULT, BREAK" | "HALT" | "FAULT" ; gas_consumed : string ; stack : StackItemLike [ ] ; } export type StackItemParser = ( item : StackItemLike ) => any ; export type VMResultParser = ( result : RPCVMResponse ) => any [ ] ; param args returns export function buildParser ( ... args ) { return ( result ) => { if ( result . stack . length !== args . length ) { throw new Error ( `Wrong number of items to parse! Expected ${ args . length } but got ${ result . stack . length }!` ) ; } return result . stack . map ( ( item , i ) => args [ i ] ( item ) ) ; } ; } /**
 * This just returns the value of the StackItem.
 */ export function NoOpParser ( item ) { return item . value ; } /**
 * Parses the result to an integer.
 */ export function IntegerParser ( item ) { return parseInt ( ( item . value as string ) || "0" , 10 ) ; } /**
 *  Parses the result to a ASCII string.
 */ export function StringParser ( item ) { return hexstring2str ( item . value as string ) ; } /**
 * Parses the result to a Fixed8.
 */ export function Fixed8Parser ( item ) { return Fixed8 . fromReverseHex ( item . value as string ) ; } param res return export function SimpleParser ( res ) { return res . stack . map ( ( item ) => { switch ( item . type ) { case "ByteArray" : return StringParser ( item ) ; case "Integer" : return IntegerParser ( item ) ; default : throw Error ( `Unknown type: ${ item . type }` ) ; } } ) ; }