rlp 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf98346ec86ff3aba23c41e47c045cabb289c3b8
4
- data.tar.gz: 812e1c73e84ca3895d8d9ea7d1f8719fbc221ce4
3
+ metadata.gz: 3a7e6cfb6c2ff329ee45d38680304abe966afa38
4
+ data.tar.gz: 9a9c72a1be75da655162c63dbc351c10e621aadc
5
5
  SHA512:
6
- metadata.gz: 6f02f081b0843fa2d37abbba64e46e5d0721c315dfa59033223b9c2308537171948fafca767967d0f49846272391930969824542e338f02a7dd152a1cae20d25
7
- data.tar.gz: ce2fff4c4eaa42e0874fe5bfac6743df67c513fbf03f3f5b9843530a85499e7a2e1bd5e34b57b2698e99f16eda8e4c2bc4de2df24ec654d6ecfff362a12abe53
6
+ metadata.gz: 5ae8922fc6fd5b0b641d03103218ebc2368562156cf1220469ab15dbcbde92436dd8a543473339c036d70e59af227c9f44614df75bc5b36f9bf188b0be20dd5a
7
+ data.tar.gz: 54c60053ea2ccbb85efa95d0ce310a1415854cf4873f478cc0c163118fceef6b130e005d361b71cf09308f80c3bf757362820249e7fc4cfcff2287ce8a4078a9
data/lib/rlp.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  require 'rlp/constant'
2
4
  require 'rlp/data'
3
5
  require 'rlp/error'
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Constant
3
5
  SHORT_LENGTH_LIMIT = 56
@@ -6,7 +8,7 @@ module RLP
6
8
  PRIMITIVE_PREFIX_OFFSET = 0x80
7
9
  LIST_PREFIX_OFFSET = 0xc0
8
10
 
9
- BYTE_ZERO = "\x00".force_encoding('ascii-8bit').freeze
10
- BYTE_EMPTY = ''.force_encoding('ascii-8bit').freeze
11
+ BYTE_ZERO = "\x00".freeze
12
+ BYTE_EMPTY = ''.freeze
11
13
  end
12
14
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  ##
2
4
  # A wrapper to represent already RLP encoded data
3
5
  #
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Decode
3
5
  include Constant
@@ -14,13 +16,15 @@ module RLP
14
16
  # is updated whenever one of its fields changes or prevent such changes
15
17
  # entirely ({RLP::Sedes::Serializable} does the latter).
16
18
  #
17
- # @param sedes [#deserialize] an object implementing a function
18
- # `deserialize(code)` which will be applied after decoding, or `nil` if
19
- # no deserialization should be performed
20
- # @param options [Hash] additional keyword arguments that will be passed to
21
- # the deserializer
22
- # @param strict [Boolean] if false inputs that are longer than necessary
23
- # don't cause an exception
19
+ # @param options (Hash) deserialization options:
20
+ #
21
+ # * sedes (Object) an object implementing a function `deserialize(code)`
22
+ # which will be applied after decoding, or `nil` if no deserialization
23
+ # should be performed
24
+ # * strict (Boolean) if false inputs that are longer than necessary don't
25
+ # cause an exception
26
+ # * (any options left) (Hash) additional keyword arguments passed to the
27
+ # deserializer
24
28
  #
25
29
  # @return [Object] the decoded and maybe deserialized object
26
30
  #
@@ -28,8 +32,10 @@ module RLP
28
32
  # the root item and `strict` is true
29
33
  # @raise [RLP::Error::DeserializationError] if the deserialization fails
30
34
  #
31
- def decode(rlp, sedes: nil, strict: true, options: {})
35
+ def decode(rlp, **options)
32
36
  rlp = str_to_bytes(rlp)
37
+ sedes = options.delete(:sedes)
38
+ strict = options.has_key?(:strict) ? options.delete(:strict) : true
33
39
 
34
40
  begin
35
41
  item, next_start = consume_item(rlp, 0)
@@ -40,8 +46,7 @@ module RLP
40
46
  raise DecodingError.new("RLP string ends with #{rlp.size - next_start} superfluous bytes", rlp) if next_start != rlp.size && strict
41
47
 
42
48
  if sedes
43
- # FIXME: lazy man's kwargs
44
- obj = sedes.is_a?(Sedes::Serializable) ?
49
+ obj = sedes.instance_of?(Class) && sedes.include?(Sedes::Serializable) ?
45
50
  sedes.deserialize(item, **options) :
46
51
  sedes.deserialize(item)
47
52
 
@@ -102,7 +107,7 @@ module RLP
102
107
  elsif b0 < LIST_PREFIX_OFFSET + SHORT_LENGTH_LIMIT # short list
103
108
  [:list, b0 - LIST_PREFIX_OFFSET, start + 1]
104
109
  else # long list
105
- raise DecodingError.new('Length starts with zero bytes", rlp') if rlp.slice(start+1) == BYTE_ZERO
110
+ raise DecodingError.new('Length starts with zero bytes', rlp) if rlp.slice(start+1) == BYTE_ZERO
106
111
 
107
112
  ll = b0 - LIST_PREFIX_OFFSET - SHORT_LENGTH_LIMIT + 1
108
113
  l = big_endian_to_int rlp[(start+1)...(start+1+ll)]
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module DecodeLazy
3
5
  include Decode
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Encode
3
5
  include Constant
@@ -1,7 +1,9 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Error
3
5
 
4
- class RLPException < Exception; end
6
+ class RLPException < StandardError; end
5
7
 
6
8
  class EncodingError < RLPException
7
9
  attr :obj
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  ##
3
5
  # A RLP encoded list which decodes itself when necessary.
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  require_relative 'sedes/big_endian_int'
2
4
  require_relative 'sedes/binary'
3
5
  require_relative 'sedes/list'
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  class BigEndianInt
@@ -1,7 +1,10 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  class Binary
4
- include RLP::Utils
6
+ include Error
7
+ include Utils
5
8
 
6
9
  Infinity = 1.0 / 0.0
7
10
 
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  ##
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  ##
@@ -32,6 +34,9 @@ module RLP
32
34
  begin
33
35
  result.push sedes.serialize(element)
34
36
  rescue SerializationError => e
37
+ puts "List.serialize"
38
+ puts e
39
+ puts e.backtrace[0,20].join("\n")
35
40
  raise ListSerializationError.new(obj: obj, element_exception: e, index: i)
36
41
  end
37
42
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  ##
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Sedes
3
5
  ##
@@ -32,6 +34,10 @@ module RLP
32
34
  end
33
35
  end
34
36
 
37
+ def inherit_serializable_fields!
38
+ @serializable_fields = superclass.serializable_fields
39
+ end
40
+
35
41
  def serializable_fields
36
42
  @serializable_fields
37
43
  end
@@ -50,11 +56,16 @@ module RLP
50
56
  begin
51
57
  serializable_sedes.serialize(field_values)
52
58
  rescue ListSerializationError => e
59
+ puts "Serializable.serialize"
60
+ puts e
61
+ puts e.backtrace[0,20].join("\n")
53
62
  raise ObjectSerializationError.new(obj: obj, sedes: self, list_exception: e)
54
63
  end
55
64
  end
56
65
 
57
- def deserialize(serial, exclude: nil, extra: {})
66
+ def deserialize(serial, **options)
67
+ exclude = options.delete(:exclude)
68
+
58
69
  begin
59
70
  values = serializable_sedes.deserialize(serial)
60
71
  rescue ListDeserializationError => e
@@ -64,10 +75,18 @@ module RLP
64
75
  params = Hash[*serializable_fields.keys.zip(values).flatten(1)]
65
76
  params.delete_if {|field, value| exclude.include?(field) } if exclude
66
77
 
67
- obj = self.new params.merge(extra)
78
+ obj = self.new params.merge(options)
68
79
  obj.instance_variable_set :@_mutable, false
69
80
  obj
70
81
  end
82
+
83
+ ##
84
+ # Create a new sedes considering only a reduced set of fields.
85
+ #
86
+ def exclude(excluded_fields)
87
+ fields = serializable_fields.dup.delete_if {|k, v| excluded_fields.include?(k) }
88
+ Class.new(self).tap {|cls| cls.set_serializable_fields fields }
89
+ end
71
90
  end
72
91
 
73
92
  class <<self
@@ -79,34 +98,49 @@ module RLP
79
98
  attr_accessor :_cached_rlp
80
99
 
81
100
  def initialize(*args)
82
- serializable_initialize(*args)
101
+ serializable_initialize parse_field_args(args)
83
102
  end
84
103
 
85
- def serializable_initialize(*args)
86
- options = args.last.is_a?(Hash) ? args.pop : {}
104
+ ##
105
+ # Mimic python's argument syntax, accept both normal arguments and named
106
+ # arguments. Normal argument overrides named argument.
107
+ #
108
+ def parse_field_args(args)
109
+ h = {}
87
110
 
111
+ options = args.last.is_a?(Hash) ? args.pop : {}
88
112
  field_set = self.class.serializable_fields.keys
89
113
 
90
- self.class.serializable_fields.keys.zip(args).each do |(field, arg)|
91
- break unless arg
92
- _set_field field, arg
114
+ fields = self.class.serializable_fields.keys[0,args.size]
115
+ fields.zip(args).each do |(field, arg)|
116
+ h[field] = arg
93
117
  field_set.delete field
94
118
  end
95
119
 
96
120
  options.each do |field, value|
97
121
  if field_set.include?(field)
98
- _set_field field, value
122
+ h[field] = value
99
123
  field_set.delete field
100
124
  end
101
125
  end
102
126
 
127
+ h
128
+ end
129
+
130
+ def serializable_initialize(fields)
131
+ make_mutable!
132
+
133
+ field_set = self.class.serializable_fields.keys
134
+ fields.each do |field, value|
135
+ _set_field field, value
136
+ field_set.delete field
137
+ end
138
+
103
139
  raise TypeError, "Not all fields initialized" unless field_set.size == 0
104
140
  end
105
141
 
106
142
  def _set_field(field, value)
107
- unless instance_variable_defined?(:@_mutable)
108
- @_mutable = true
109
- end
143
+ make_mutable! unless instance_variable_defined?(:@_mutable)
110
144
 
111
145
  if mutable? || !self.class.serializable_fields.has_key?(field)
112
146
  instance_variable_set :"@#{field}", value
@@ -125,7 +159,7 @@ module RLP
125
159
  end
126
160
 
127
161
  def make_immutable!
128
- @_mutable = true
162
+ make_mutable!
129
163
  self.class.serializable_fields.keys.each do |field|
130
164
  ::RLP::Utils.make_immutable! send(field)
131
165
  end
@@ -133,6 +167,11 @@ module RLP
133
167
  @_mutable = false
134
168
  self
135
169
  end
170
+
171
+ def make_mutable!
172
+ @_mutable = true
173
+ end
174
+
136
175
  end
137
176
  end
138
177
  end
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
4
  module Utils
3
5
  class <<self
@@ -1,3 +1,5 @@
1
+ # -*- encoding : ascii-8bit -*-
2
+
1
3
  module RLP
2
- VERSION = '0.5.3'
4
+ VERSION = '0.6.0'
3
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rlp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Xie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake