github.com/nspcc-dev/neo-go@v0.105.2-0.20240517133400-6be757af3eba/pkg/vm/stackitem/type.go (about)

     1  package stackitem
     2  
     3  import "errors"
     4  
     5  // ErrInvalidType is returned upon attempts to deserialize some unknown item type.
     6  var ErrInvalidType = errors.New("invalid type")
     7  
     8  // Type represents a type of the stack item.
     9  type Type byte
    10  
    11  // This block defines all known stack item types.
    12  const (
    13  	AnyT       Type = 0x00
    14  	PointerT   Type = 0x10
    15  	BooleanT   Type = 0x20
    16  	IntegerT   Type = 0x21
    17  	ByteArrayT Type = 0x28
    18  	BufferT    Type = 0x30
    19  	ArrayT     Type = 0x40
    20  	StructT    Type = 0x41
    21  	MapT       Type = 0x48
    22  	InteropT   Type = 0x60
    23  	InvalidT   Type = 0xFF
    24  )
    25  
    26  // String implements the fmt.Stringer interface.
    27  func (t Type) String() string {
    28  	switch t {
    29  	case AnyT:
    30  		return "Any"
    31  	case PointerT:
    32  		return "Pointer"
    33  	case BooleanT:
    34  		return "Boolean"
    35  	case IntegerT:
    36  		return "Integer"
    37  	case ByteArrayT:
    38  		return "ByteString"
    39  	case BufferT:
    40  		return "Buffer"
    41  	case ArrayT:
    42  		return "Array"
    43  	case StructT:
    44  		return "Struct"
    45  	case MapT:
    46  		return "Map"
    47  	case InteropT:
    48  		return "InteropInterface"
    49  	default:
    50  		return "INVALID"
    51  	}
    52  }
    53  
    54  // IsValid checks if s is a well defined stack item type.
    55  func (t Type) IsValid() bool {
    56  	switch t {
    57  	case AnyT, PointerT, BooleanT, IntegerT, ByteArrayT, BufferT, ArrayT, StructT, MapT, InteropT:
    58  		return true
    59  	default:
    60  		return false
    61  	}
    62  }
    63  
    64  // FromString returns stackitem type from the string.
    65  func FromString(s string) (Type, error) {
    66  	switch s {
    67  	case "Any":
    68  		return AnyT, nil
    69  	case "Pointer":
    70  		return PointerT, nil
    71  	case "Boolean":
    72  		return BooleanT, nil
    73  	case "Integer":
    74  		return IntegerT, nil
    75  	case "ByteString":
    76  		return ByteArrayT, nil
    77  	case "Buffer":
    78  		return BufferT, nil
    79  	case "Array":
    80  		return ArrayT, nil
    81  	case "Struct":
    82  		return StructT, nil
    83  	case "Map":
    84  		return MapT, nil
    85  	case "InteropInterface":
    86  		return InteropT, nil
    87  	default:
    88  		return 0xFF, ErrInvalidType
    89  	}
    90  }