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
29
# 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 BitSequenceType then decode_bit_sequence(type, bytes)
  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



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

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_bit_sequence(sequence_type, bytes) ⇒ Object



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

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

  bit_len, remaining_bytes = decode_compact(bytes)
  byte_len = (bit_len / 8.0).ceil
  the_bytes, remaining_bytes = [remaining_bytes[0...byte_len], remaining_bytes[byte_len..]]
  [
    {
      bytes: the_bytes,
      bit_len: bit_len
    },
    remaining_bytes
  ]
end

#decode_compact(bytes) ⇒ Object



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

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

  CodecUtils.decode_compact(bytes)
end

#decode_primitive(type, bytes) ⇒ Object



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

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



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

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/scale_rb/decode.rb', line 101

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



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

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/scale_rb/decode.rb', line 117

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