Module: ScaleRb::CodecUtils::InternalDecodeUtils

Extended by:
TypeEnforcer
Includes:
Types
Included in:
ScaleRb::CodecUtils
Defined in:
lib/scale_rb/codec_utils.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_boolean(bytes) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/scale_rb/codec_utils.rb', line 48

def decode_boolean(bytes)
  value = case bytes[0]
          when 0x00 then false
          when 0x01 then true
          else raise Codec::InvalidBytesError, 'type: Boolean'
          end
  [value, bytes[1..]]
end

#decode_compact(bytes) ⇒ Object



59
60
61
# File 'lib/scale_rb/codec_utils.rb', line 59

def decode_compact(bytes)
  _do_decode_compact(bytes)
end

#decode_int(type, bytes) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scale_rb/codec_utils.rb', line 26

def decode_int(type, bytes)
  bit_length = type[1..].to_i
  byte_length = bit_length / 8
  raise Codec::NotEnoughBytesError, "type: #{type}" if bytes.length < byte_length

  value = Utils.u8a_to_int(bytes[0...byte_length].reverse, bit_length)
  # debug 'value', value
  [
    value,
    bytes[byte_length..]
  ]
end

#decode_str(bytes) ⇒ Object



40
41
42
43
44
45
# File 'lib/scale_rb/codec_utils.rb', line 40

def decode_str(bytes)
  length, remaining_bytes = _do_decode_compact(bytes)
  raise Codec::NotEnoughBytesError, 'type: String' if remaining_bytes.length < length

  [Utils.u8a_to_utf8(remaining_bytes[0...length]), remaining_bytes[length..]]
end

#decode_uint(type, bytes) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/scale_rb/codec_utils.rb', line 12

def decode_uint(type, bytes)
  bit_length = type[1..].to_i
  byte_length = bit_length / 8
  raise Codec::NotEnoughBytesError, "type: #{type}" if bytes.length < byte_length

  value = Utils.u8a_to_uint(bytes[0...byte_length].reverse)
  # debug 'value', value
  [
    value,
    bytes[byte_length..]
  ]
end