Module: ScaleRb::Encode
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
-
#encode(id, value, registry) ⇒ Object
-
#encode_array(array_type, value, registry) ⇒ Object
-
#encode_compact(value) ⇒ Object
-
#encode_primitive(type, value) ⇒ Object
-
#encode_sequence(sequence_type, value, registry) ⇒ Object
-
#encode_struct(struct_type, value, registry) ⇒ Object
-
#encode_tuple(tuple_type, value, registry) ⇒ Object
-
#encode_variant(variant_type, value, registry) ⇒ Object
__, extended, method_added
Instance Method Details
#encode(id, value, registry) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/scale_rb/encode.rb', line 10
def encode(id, value, registry)
ScaleRb.logger.debug("Encoding #{id}, value: #{value}")
type = registry[id]
raise Codec::TypeNotFound, "id: #{id}" if type.nil?
case type when PrimitiveType then encode_primitive(type, value)
when CompactType then encode_compact(value)
when ArrayType then encode_array(type, value, registry)
when SequenceType then encode_sequence(type, value, registry)
when TupleType then encode_tuple(type, value, registry)
when StructType then encode_struct(type, value, registry)
when UnitType then []
when VariantType then encode_variant(type, value, registry)
else raise Codec::TypeNotImplemented, "encoding id: #{id}, type: #{type}"
end
end
|
#encode_array(array_type, value, registry) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/scale_rb/encode.rb', line 49
def encode_array(array_type, value, registry)
ScaleRb.logger.debug("Encoding array: #{array_type}, value: #{value}")
len = array_type.len
inner_type_id = array_type.type
_encode_types([inner_type_id] * len, value, registry)
end
|
#encode_compact(value) ⇒ Object
#encode_primitive(type, value) ⇒ Object
#encode_sequence(sequence_type, value, registry) ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/scale_rb/encode.rb', line 59
def encode_sequence(sequence_type, value, registry)
ScaleRb.logger.debug("Encoding sequence: #{sequence_type}, value: #{value}")
len = value.length
inner_type_id = sequence_type.type
encode_compact(len) + _encode_types([inner_type_id] * len, value, registry)
end
|
#encode_struct(struct_type, value, registry) ⇒ Object
82
83
84
85
86
87
88
89
|
# File 'lib/scale_rb/encode.rb', line 82
def encode_struct(struct_type, value, registry)
ScaleRb.logger.debug("Encoding struct: #{struct_type}, value: #{value}")
fields = struct_type.fields
type_ids = fields.map(&:type)
_encode_types(type_ids, value.values, registry)
end
|
#encode_tuple(tuple_type, value, registry) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/scale_rb/encode.rb', line 69
def encode_tuple(tuple_type, value, registry)
ScaleRb.logger.debug("Encoding tuple: #{tuple_type}, value: #{value}")
type_ids = tuple_type.tuple
value = [value] if type_ids.length == 1
_encode_types(type_ids, value, registry)
end
|
#encode_variant(variant_type, value, registry) ⇒ Object
92
93
94
95
96
97
98
99
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
|
# File 'lib/scale_rb/encode.rb', line 92
def encode_variant(variant_type, value, registry)
ScaleRb.logger.debug("Encoding variant: #{variant_type}, value: #{value}")
if variant_type.option?
if value.nil?
name = :None
else
name = :Some
end
else
name = value.is_a?(::Symbol) ? value : value.keys.first
end
variant = variant_type.variants.find { |v| v.name == name }
raise Codec::VariantItemNotFound, "type: #{variant_type}, name: #{value}" if variant.nil?
case variant
when SimpleVariant
ScaleRb::CodecUtils.encode_uint('U8', variant.index)
when TupleVariant
ScaleRb::CodecUtils.encode_uint('U8', variant.index) + encode_tuple(variant.tuple, value.values.first, registry)
when StructVariant
ScaleRb::CodecUtils.encode_uint('U8', variant.index) + encode_struct(variant.struct, value.values.first, registry)
end
end
|