Module: ScaleRb::Decode

Extended by:
TypeEnforcer
Includes:
Types
Included in:
Codec
Defined in:
lib/scale_rb/decode.rb

Constant Summary

Constants included from Types

Types::DecodeResult, Types::HashMap, Types::Hex, Types::PortableType, Types::Primitive, Types::Registry, Types::Ti, Types::TypedArray, Types::U8, Types::U8Array, Types::UnsignedInteger, Types::VariantKind

Instance Method Summary collapse

Methods included from TypeEnforcer

__, extended, method_added

Instance Method Details

#decode(id, bytes, registry) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/scale_rb/decode.rb', line 12

def decode(id, bytes, registry)
  ScaleRb.logger.debug("Decoding #{id}, bytes: #{bytes.length} bytes")
  type = registry[id]
  raise Codec::TypeNotFound, "id: #{id}" if type.nil?

  case type
  when PrimitiveType then decode_primitive(type, bytes)
  when CompactType then decode_compact(bytes)
  when ArrayType then decode_array(type, bytes, registry)
  when SequenceType then decode_sequence(type, bytes, registry)
  when TupleType then decode_tuple(type, bytes, registry)
  when StructType then decode_struct(type, bytes, registry)
  when UnitType then [[], bytes]
  when VariantType then decode_variant(type, bytes, registry)
  else raise Codec::TypeNotImplemented, "id: #{id}, type: #{type}"
  end
end

#decode_array(type, bytes, registry) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/scale_rb/decode.rb', line 51

def decode_array(type, bytes, registry)
  ScaleRb.logger.debug("Decoding array: #{type}, bytes: #{bytes.length} bytes")

  len = type.len
  inner_type_id = type.type

  _decode_types([inner_type_id] * len, bytes, registry)
end

#decode_compact(bytes) ⇒ Object



44
45
46
47
48
# File 'lib/scale_rb/decode.rb', line 44

def decode_compact(bytes)
  ScaleRb.logger.debug("Decoding compact: bytes: #{bytes.length} bytes")

  CodecUtils.decode_compact(bytes)
end

#decode_primitive(type, bytes) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scale_rb/decode.rb', line 31

def decode_primitive(type, bytes)
  primitive = type.primitive.to_s
  ScaleRb.logger.debug("Decoding primitive: #{primitive}, bytes: #{bytes.length} bytes")

  return CodecUtils.decode_uint(primitive, bytes) if primitive.start_with?('U')
  return CodecUtils.decode_int(primitive, bytes) if primitive.start_with?('I')
  return CodecUtils.decode_str(bytes) if primitive == 'Str'
  return CodecUtils.decode_boolean(bytes) if primitive == 'Bool'

  raise Codec::TypeNotImplemented, "decoding primitive: #{primitive}"
end

#decode_sequence(sequence_type, bytes, registry) ⇒ Object



61
62
63
64
65
66
# File 'lib/scale_rb/decode.rb', line 61

def decode_sequence(sequence_type, bytes, registry)
  ScaleRb.logger.debug("Decoding sequence: #{sequence_type}, bytes: #{bytes.length} bytes")

  len, remaining_bytes = decode_compact(bytes)
  _decode_types([sequence_type.type] * len, remaining_bytes, registry)
end

#decode_struct(struct_type, bytes, registry) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/scale_rb/decode.rb', line 84

def decode_struct(struct_type, bytes, registry)
  ScaleRb.logger.debug("Decoding struct: #{struct_type}, bytes: #{bytes.length} bytes")

  fields = struct_type.fields

  names = fields.map { |f| f.name.to_sym }
  type_ids = fields.map(&:type)

  values, remaining_bytes = _decode_types(type_ids, bytes, registry)
  [
    [names, values].transpose.to_h,
    remaining_bytes
  ]
end

#decode_tuple(tuple_type, bytes, registry) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scale_rb/decode.rb', line 69

def decode_tuple(tuple_type, bytes, registry)
  ScaleRb.logger.debug("Decoding tuple: #{tuple_type}, bytes: #{bytes.length} bytes")

  type_ids = tuple_type.tuple

  # NOTE: If the tuple has only one element, decode that element directly.
  # This is to make the structure of the decoded result clear.
  if type_ids.length == 1
    decode(type_ids.first, bytes, registry)
  else
    _decode_types(type_ids, bytes, registry)
  end
end

#decode_variant(variant_type, bytes, registry) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/scale_rb/decode.rb', line 100

def decode_variant(variant_type, bytes, registry)
  ScaleRb.logger.debug("Decoding variant: #{variant_type}, bytes: #{bytes.length} bytes")

  # find the variant by the index
  index = bytes[0].to_i
  variant = variant_type.variants.find { |v| v.index == index }
  raise Codec::VariantIndexOutOfRange, "type: #{variant_type}, index: #{index}, bytes: #{bytes}" if variant.nil?

  # decode the variant
  case variant
  when SimpleVariant
    if variant.name == :None
      [nil, bytes[1..]]
    else
      [
        variant.name,
        bytes[1..]
      ]
    end
  when TupleVariant
    value, remainning_bytes = decode_tuple(variant.tuple, bytes[1..], registry)
    if variant.name == :Some
      [value, remainning_bytes]
    else
      [
        { variant.name => value },
        remainning_bytes
      ]
    end
  when StructVariant
    value, remainning_bytes = decode_struct(variant.struct, bytes[1..], registry)
    [
      { variant.name => value },
      remainning_bytes
    ]
  else raise 'Unreachable'
  end
end