Improve nil handling

dev
Alberto Restifo 2020-05-11 12:48:02 +02:00
parent 67db021c2b
commit 59145cacfe
2 changed files with 25 additions and 7 deletions

View File

@ -60,14 +60,19 @@ class CBOR::Decoder
def read_nil : Nil def read_nil : Nil
read_type(Token::SimpleValueT) do |token| read_type(Token::SimpleValueT) do |token|
case token.value return nil if token.value.is_nil?
when SimpleValue::Null,
SimpleValue::Undefined
nil
else
unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined") unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined")
end end
end end
def read_nil_or
if @current_token.is_a?(Token::SimpleValueT) && @current_token.value.is_nil?
finish_token!
nil
else
yield
end
end end
def consume_array(&block) def consume_array(&block)
@ -76,6 +81,10 @@ class CBOR::Decoder
end end
end end
def read_begin_hash
read_type(Token::MapT, finish_token: false) { |token| }
end
def consume_hash(&block) def consume_hash(&block)
read_type(Token::MapT) do |token| read_type(Token::MapT) do |token|
consume_sequence(token.size) { yield } consume_sequence(token.size) { yield }

View File

@ -18,4 +18,13 @@ enum CBOR::SimpleValue : UInt8
"simple(#{self.value.to_s})" "simple(#{self.value.to_s})"
end end
end end
def is_nil? : Bool
case self
when Null, Undefined
true
else
false
end
end
end end