From 830981ba5de81c254b7b35440c2427bcf91dacd3 Mon Sep 17 00:00:00 2001 From: Alberto Restifo Date: Fri, 24 Apr 2020 22:50:48 +0200 Subject: [PATCH] Decode Nil --- spec/cbor/from_cbor_spec.cr | 2 ++ src/cbor/decoder.cr | 12 ++++++++++++ src/cbor/from_cbor.cr | 6 +++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spec/cbor/from_cbor_spec.cr b/spec/cbor/from_cbor_spec.cr index fa81ac2..a19b252 100644 --- a/spec/cbor/from_cbor_spec.cr +++ b/spec/cbor/from_cbor_spec.cr @@ -17,6 +17,8 @@ describe "CBOR helpers on basic types" do Time::Format::RFC_3339.parse("2013-03-21T20:04:00Z")}, {Time, Bytes[0xc1, 0x1a, 0x51, 0x4b, 0x67, 0xb0], Time.unix(1363896240)}, {Time, Bytes[0xc1, 0xfb, 0x41, 0xd4, 0x52, 0xd9, 0xec, 0x20, 0x00, 0x00], Time.unix_ms((BigFloat.new(1363896240.5) * 1000).to_u64)}, + {Nil, Bytes[0xf6], nil}, + {Nil, Bytes[0xf7], nil}, ] tests.each do |tt| diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index f33bd4a..0f295ce 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -58,6 +58,18 @@ class CBOR::Decoder end end + 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 + end + end + private def finish_token! @current_token = @lexer.next_token end diff --git a/src/cbor/from_cbor.cr b/src/cbor/from_cbor.cr index b598786..a4bad61 100644 --- a/src/cbor/from_cbor.cr +++ b/src/cbor/from_cbor.cr @@ -23,11 +23,15 @@ def Bool.new(decoder : CBOR::Decoder) decoder.read_bool end +def Nil.new(decoder : CBOR::Decoder) + decoder.read_nil +end + def Slice.new(decoder : CBOR::Decoder) decoder.read_bytes.to_slice end -# Reads the CBOR values a time. The value must be surrounded by a time tag as +# Reads the CBOR values as a time. The value must be surrounded by a time tag as # specified by [Section 2.4.1 of RFC 7049][1]. # # [1]: https://tools.ietf.org/html/rfc7049#section-2.4.1