diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index bdc5edc..4bdb5b4 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -60,13 +60,18 @@ class CBOR::Decoder def read_nil : Nil read_type(Token::SimpleValueT) do |token| - case token.value - when SimpleValue::Null, - SimpleValue::Undefined - nil - else - unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined") - end + return nil if token.value.is_nil? + + unexpected_token(token, "SimpleValue::Null or SimpleValue::Undefined") + 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 @@ -76,6 +81,10 @@ class CBOR::Decoder end end + def read_begin_hash + read_type(Token::MapT, finish_token: false) { |token| } + end + def consume_hash(&block) read_type(Token::MapT) do |token| consume_sequence(token.size) { yield } diff --git a/src/cbor/simple_value.cr b/src/cbor/simple_value.cr index a931d9d..b41cae0 100644 --- a/src/cbor/simple_value.cr +++ b/src/cbor/simple_value.cr @@ -18,4 +18,13 @@ enum CBOR::SimpleValue : UInt8 "simple(#{self.value.to_s})" end end + + def is_nil? : Bool + case self + when Null, Undefined + true + else + false + end + end end