Sha256: e6d78e4c095937c589f7384a9726e15e249d6fc31756f99cde8209a3fcc17204

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Veriform
  # Build Veriform::Objects from Veriform's self-describing messages
  class Decoder
    # Create a new decoder object which will construct a Veriform::Object tree
    def initialize
      @stack = [Veriform::Object.new]
    end

    # Add a uint64 to the current object
    def uint64(id, value)
      raise TypeError, "expected Integer, got #{value.class}" unless value.is_a?(Integer)
      @stack.last[id] = value
    end

    # Add binary data to the current object
    def binary(id, value)
      raise TypeError, "expected String, got #{value.class}" unless value.is_a?(String)
      raise EncodingError, "expected BINARY encoding, got #{value.encoding}" unless value.encoding == Encoding::BINARY
      @stack.last[id] = value
    end

    # Push down the internal stack, constructing a new Veriform::Object
    def begin_nested
      @stack << Veriform::Object.new
    end

    # Complete the pushdown, adding the newly constructed object to the next one in the stack
    def end_nested(id)
      value = @stack.pop
      raise StateError, "not inside a nested message" if @stack.empty?
      @stack.last[id] = value
    end

    # Finish decoding, returning the parent Veriform::Object
    def finish
      result = @stack.pop
      raise StateError, "objects remaining in stack" unless @stack.empty?
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
veriform-0.0.0 lib/veriform/decoder.rb