~arestifo/crystal-cbor

crystal-cbor: Decode simple values (Bool or Nil) (with crystal tool format patch) v2 PROPOSED

This patchset fixes the decoding of simple values, in particular the
union of Booleans and Nil. I also added some tests.

Now compliant
with "crystal tool format".

Karchnu (4):
  Decode simple value (Bool or Nil).
  Fixing (Bool | Nil) union in the from_cbor.
  Add CBOR simple value encoding tests (Bool or Nil).
  Applying patch from "crystal tool format".

 spec/cbor_simple-values.cr | 14 ++++++++++++++
 src/cbor/decoder.cr        |  8 ++++++++
 src/cbor/from_cbor.cr      | 21 ++++++++++++++-------
 3 files changed, 36 insertions(+), 7 deletions(-)
 create mode 100644 spec/cbor_simple-values.cr

-- 
2.30.2
#531523 .build.yml success
crystal-cbor/patches/.build.yml: SUCCESS in 27s

[Decode simple values (Bool or Nil) (with crystal tool format patch)][0] v2 from [~karchnu][1]

[0]: https://lists.sr.ht/~arestifo/crystal-cbor/patches/23446
[1]: mailto:karchnu@karchnu.fr

✓ #531523 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/531523
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~arestifo/crystal-cbor/patches/23446/mbox | git am -3
Learn more about email & git

[PATCH crystal-cbor v2 1/4] Decode simple value (Bool or Nil). Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 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 510fb45..b86a043 100644
--- a/src/cbor/decoder.cr
+++ b/src/cbor/decoder.cr
@@ -42,6 +42,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 e2610e7..4977cb7 100644
--- a/src/cbor/from_cbor.cr
+++ b/src/cbor/from_cbor.cr
@@ -208,13 +208,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
-- 
2.30.2

[PATCH crystal-cbor v2 2/4] Fixing (Bool | Nil) union in the from_cbor. Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 src/cbor/from_cbor.cr | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/cbor/from_cbor.cr b/src/cbor/from_cbor.cr
index 4977cb7..5c822cd 100644
--- a/src/cbor/from_cbor.cr
+++ b/src/cbor/from_cbor.cr
@@ -208,7 +208,6 @@ end
def Union.new(decoder : CBOR::Decoder)
  {% begin %}
    case decoder.current_token
    {% if T.includes? Nil || T.includes? Bool %}
    when CBOR::Token::SimpleValueT
      # This value could be either a boolean or nil.
      value = decoder.read_simple_value
@@ -224,7 +223,6 @@ def Union.new(decoder : CBOR::Decoder)
      else
        raise "value is neither Bool or Nil"
      end
    {% end %}
    {% if T.includes? String %}
    when CBOR::Token::StringT
      return decoder.read_string
-- 
2.30.2

[PATCH crystal-cbor v2 3/4] Add CBOR simple value encoding tests (Bool or Nil). Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 spec/cbor_simple-values.cr | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 spec/cbor_simple-values.cr

diff --git a/spec/cbor_simple-values.cr b/spec/cbor_simple-values.cr
new file mode 100644
index 0000000..f35c85a
--- /dev/null
+++ b/spec/cbor_simple-values.cr
@@ -0,0 +1,15 @@
require "./spec_helper"

describe CBOR do
  describe "basics: to_cbor" do
    it "union (Nil | Bool) (nil)" do
      value = (Nil | Bool).from_cbor(nil.to_cbor).to_cbor
      value.hexstring.should eq "f6"
    end
    it "union (Nil | Bool) (false)" do
      value = (Nil | Bool).from_cbor(false.to_cbor).to_cbor
      value.hexstring.should eq "f4"
    end
  end
end

-- 
2.30.2

[PATCH crystal-cbor v2 4/4] Applying patch from "crystal tool format". Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 spec/cbor_simple-values.cr | 1 -
 1 file changed, 1 deletion(-)

diff --git a/spec/cbor_simple-values.cr b/spec/cbor_simple-values.cr
index f35c85a..ea53737 100644
--- a/spec/cbor_simple-values.cr
+++ b/spec/cbor_simple-values.cr
@@ -12,4 +12,3 @@ describe CBOR do
    end
  end
end

-- 
2.30.2
crystal-cbor/patches/.build.yml: SUCCESS in 27s

[Decode simple values (Bool or Nil) (with crystal tool format patch)][0] v2 from [~karchnu][1]

[0]: https://lists.sr.ht/~arestifo/crystal-cbor/patches/23446
[1]: mailto:karchnu@karchnu.fr

✓ #531523 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/531523