Class: Nauvisian::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/nauvisian/deserializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Deserializer

Returns a new instance of Deserializer.

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/nauvisian/deserializer.rb', line 5

def initialize(stream)
  raise ArgumentError, "can't read from the given argument" unless stream.respond_to?(:read)

  @stream = stream
end

Instance Method Details

#read_boolObject



41
# File 'lib/nauvisian/deserializer.rb', line 41

def read_bool = read_u8 != 0

#read_bytes(length) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
# File 'lib/nauvisian/deserializer.rb', line 11

def read_bytes(length)
  raise ArgumentError, "nil length" if length.nil?
  raise ArgumentError, "negative length" if length.negative?
  return +"" if length.zero?

  bytes = @stream.read(length)
  raise EOFError if bytes.nil? || bytes.size < length

  bytes
end

#read_dictionaryObject



68
69
70
71
72
73
74
# File 'lib/nauvisian/deserializer.rb', line 68

def read_dictionary
  length = read_u32
  length.times.each_with_object({}) do |_i, dict|
    key = read_str_property
    dict[key] = read_property_tree
  end
end

#read_doubleObject



52
# File 'lib/nauvisian/deserializer.rb', line 52

def read_double = read_bytes(8).unpack1("d")

#read_listObject



62
63
64
65
# File 'lib/nauvisian/deserializer.rb', line 62

def read_list
  length = read_optim_u32
  Array(length) { read_property_tree }
end

#read_optim_tuple(bit_size, length) ⇒ Object



39
# File 'lib/nauvisian/deserializer.rb', line 39

def read_optim_tuple(bit_size, length) = Array.new(length) { read_optim(bit_size) }

#read_optim_u16Object



27
28
29
30
# File 'lib/nauvisian/deserializer.rb', line 27

def read_optim_u16
  byte = read_u8
  byte == 0xFF ? read_u16 : byte
end

#read_optim_u32Object



33
34
35
36
# File 'lib/nauvisian/deserializer.rb', line 33

def read_optim_u32
  byte = read_u8
  byte == 0xFF ? read_u32 : byte
end

#read_property_treeObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/nauvisian/deserializer.rb', line 76

def read_property_tree
  type = read_u8
  _any_type_flag = read_bool

  case type
  when 1
    read_bool
  when 2
    read_double
  when 3
    read_str_property
  when 4
    read_list
  when 5
    read_dictionary
  else
    raise Nauvisian::UnknownPropertyType, type
  end
end

#read_strObject



43
44
45
46
# File 'lib/nauvisian/deserializer.rb', line 43

def read_str
  length = read_optim_u32
  read_bytes(length).force_encoding(Encoding::UTF_8)
end

#read_str_propertyObject



49
# File 'lib/nauvisian/deserializer.rb', line 49

def read_str_property = read_bool ? "" : read_str

#read_u16Object



23
# File 'lib/nauvisian/deserializer.rb', line 23

def read_u16 = read_bytes(2).unpack1("v")

#read_u16_tuple(length) ⇒ Object



38
# File 'lib/nauvisian/deserializer.rb', line 38

def read_u16_tuple(length) = Array.new(length) { read_u16 }

#read_u32Object



24
# File 'lib/nauvisian/deserializer.rb', line 24

def read_u32 = read_bytes(4).unpack1("V")

#read_u8Object



22
# File 'lib/nauvisian/deserializer.rb', line 22

def read_u8 = read_bytes(1).unpack1("C")

#read_version24Object



59
# File 'lib/nauvisian/deserializer.rb', line 59

def read_version24 = Nauvisian::Version24[read_optim_u16, read_optim_u16, read_optim_u16]

#read_version64Object

Assumed: method arguments are evaluated from left to right but… stackoverflow.com/a/36212870/16014712



57
# File 'lib/nauvisian/deserializer.rb', line 57

def read_version64 = Nauvisian::Version64[read_u16, read_u16, read_u16, read_u16]