From a058ab3ccf10b1eb6b215cbeb006f118bd8b6a2d Mon Sep 17 00:00:00 2001 From: Karchnu Date: Thu, 26 Nov 2020 17:05:22 +0100 Subject: [PATCH] Decode simple value (Bool or Nil). --- src/cbor/decoder.cr | 8 ++++++++ src/cbor/from_cbor.cr | 21 +++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/cbor/decoder.cr b/src/cbor/decoder.cr index 12ad40a..c64d726 100644 --- a/src/cbor/decoder.cr +++ b/src/cbor/decoder.cr @@ -47,6 +47,14 @@ class CBOR::Decoder end end + def read_simple_value : Bool? + case token = @current_token + when Token::SimpleValueT + finish_token! + token.value.to_t + end + end + def read_string : String case token = @current_token when Token::StringT diff --git a/src/cbor/from_cbor.cr b/src/cbor/from_cbor.cr index 32d5453..8aa080b 100644 --- a/src/cbor/from_cbor.cr +++ b/src/cbor/from_cbor.cr @@ -218,13 +218,22 @@ end def Union.new(decoder : CBOR::Decoder) {% begin %} case decoder.current_token - {% if T.includes? Nil %} + {% if T.includes? Nil || T.includes? Bool %} when CBOR::Token::SimpleValueT - return decoder.read_nil - {% end %} - {% if T.includes? Bool %} - when CBOR::Token::SimpleValueT - return decoder.read_bool + # This value could be either a boolean or nil. + value = decoder.read_simple_value + case value + {% if T.includes? Bool %} + when Bool + return value + {% end %} + {% if T.includes? Nil %} + when Nil + return nil + {% end %} + else + raise "value is neither Bool or Nil" + end {% end %} {% if T.includes? String %} when CBOR::Token::StringT