~arestifo/crystal-cbor

crystal-cbor: UUID - this time without opt-in functionality. v2 APPLIED

I don't know how to add commits to a previous patchset, so I added
everything again.

Karchnu (3):
  UUID.
  Add CBOR UUID encoding and decoding tests.
  CBOR UUID encoding and decoding: no longer opt-in.

 spec/cbor_uuid.cr     | 15 +++++++++++++++
 src/cbor/from_cbor.cr | 31 +++++++++++++++++++++++++++++++
 src/cbor/to_cbor.cr   | 14 ++++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 spec/cbor_uuid.cr

-- 
2.30.2
#531379 .build.yml success
crystal-cbor/patches/.build.yml: SUCCESS in 28s

[UUID - this time without opt-in functionality.][0] v2 from [~karchnu][1]

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

✓ #531379 SUCCESS crystal-cbor/patches/.build.yml https://builds.sr.ht/~arestifo/job/531379
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/23442/mbox | git am -3
Learn more about email & git

[PATCH crystal-cbor v2 1/3] UUID. Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 src/cbor/uuid.cr | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
 create mode 100644 src/cbor/uuid.cr

diff --git a/src/cbor/uuid.cr b/src/cbor/uuid.cr
new file mode 100644
index 0000000..53c346d
--- /dev/null
+++ b/src/cbor/uuid.cr
@@ -0,0 +1,47 @@
require "../cbor"
require "uuid"

struct UUID
  # Creates UUID from CBOR using `CBOR::Decoder`.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # require "cbor"
  # require "uuid"
  # require "uuid/cbor"
  #
  # class Example
  #   include CBOR::Serializable
  #
  #   property id : UUID
  # end
  #
  # hash = {"id" => "ba714f86-cac6-42c7-8956-bcf5105e1b81"}
  # example = Example.from_cbor hash.to_cbor
  # example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
  # ```
  def self.new(pull : CBOR::Decoder)
    # Either the UUID was encoded as String or bytes (smaller).
    case pull.current_token
    when CBOR::Token::StringT
      new(pull.read_string)
    when CBOR::Token::BytesT
      new(pull.read_bytes)
    else
      raise "trying to get an UUID, but CBOR value isn't a string nor bytes: #{pull.current_token}"
    end
  end

  # Returns UUID as CBOR value.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
  # uuid.to_cbor
  # ```
  def to_cbor(cbor : CBOR::Encoder)
    cbor.write(@bytes.to_slice)
  end
end
-- 
2.30.2

[PATCH crystal-cbor v2 2/3] Add CBOR UUID encoding and decoding tests. Export this patch

From: Karchnu <karchnu@karchnu.fr>

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

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

describe CBOR do
  describe "UUID" do
    it "UUID#to_cbor" do
      uuid = UUID.new "fc47eb8e-b13c-481e-863a-8f8c47a550f2"
      uuid.to_cbor.hexstring.should eq "50fc47eb8eb13c481e863a8f8c47a550f2"
    end

    it "UUID#from_cbor" do
      uuid = UUID.from_cbor "50fc47eb8eb13c481e863a8f8c47a550f2".hexbytes
      uuid.to_cbor.hexstring.should eq "50fc47eb8eb13c481e863a8f8c47a550f2"
    end
  end
end
-- 
2.30.2

[PATCH crystal-cbor v2 3/3] CBOR UUID encoding and decoding: no longer opt-in. Export this patch

From: Karchnu <karchnu@karchnu.fr>

---
 src/cbor/from_cbor.cr | 31 ++++++++++++++++++++++++++++
 src/cbor/to_cbor.cr   | 14 +++++++++++++
 src/cbor/uuid.cr      | 47 -------------------------------------------
 3 files changed, 45 insertions(+), 47 deletions(-)
 delete mode 100644 src/cbor/uuid.cr

diff --git a/src/cbor/from_cbor.cr b/src/cbor/from_cbor.cr
index e2610e7..d9a8938 100644
--- a/src/cbor/from_cbor.cr
+++ b/src/cbor/from_cbor.cr
@@ -1,3 +1,5 @@
require "uuid"

def Object.from_cbor(string_or_io)
  parser = CBOR::Decoder.new(string_or_io)
  new(parser)
@@ -255,3 +257,32 @@ def Union.new(decoder : CBOR::Decoder)
    {% end %}
  {% end %}
end

struct UUID
  # Creates UUID from CBOR using `CBOR::Decoder`.
  #
  # ```
  # require "cbor"
  #
  # class Example
  #   include CBOR::Serializable
  #
  #   property id : UUID
  # end
  #
  # hash = {"id" => "ba714f86-cac6-42c7-8956-bcf5105e1b81"}
  # example = Example.from_cbor hash.to_cbor
  # example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
  # ```
  def self.new(pull : CBOR::Decoder)
    # Either the UUID was encoded as String or bytes (smaller).
    case pull.current_token
    when CBOR::Token::StringT
      new(pull.read_string)
    when CBOR::Token::BytesT
      new(pull.read_bytes)
    else
      raise "trying to get an UUID, but CBOR value isn't a string nor bytes: #{pull.current_token}"
    end
  end
end
diff --git a/src/cbor/to_cbor.cr b/src/cbor/to_cbor.cr
index dedd90b..02a5b53 100644
--- a/src/cbor/to_cbor.cr
+++ b/src/cbor/to_cbor.cr
@@ -1,3 +1,5 @@
require "uuid"

class Object
  def to_cbor : Bytes
    encoder = CBOR::Encoder.new
@@ -130,3 +132,15 @@ end
#   def to_cbor(encoder : CBOR::Encoder)
#   end
# end

struct UUID
  # Returns UUID as CBOR value.
  #
  # ```
  # uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
  # uuid.to_cbor
  # ```
  def to_cbor(cbor : CBOR::Encoder)
    cbor.write(@bytes.to_slice)
  end
end
diff --git a/src/cbor/uuid.cr b/src/cbor/uuid.cr
deleted file mode 100644
index 53c346d..0000000
--- a/src/cbor/uuid.cr
@@ -1,47 +0,0 @@
require "../cbor"
require "uuid"

struct UUID
  # Creates UUID from CBOR using `CBOR::Decoder`.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # require "cbor"
  # require "uuid"
  # require "uuid/cbor"
  #
  # class Example
  #   include CBOR::Serializable
  #
  #   property id : UUID
  # end
  #
  # hash = {"id" => "ba714f86-cac6-42c7-8956-bcf5105e1b81"}
  # example = Example.from_cbor hash.to_cbor
  # example.id # => UUID(ba714f86-cac6-42c7-8956-bcf5105e1b81)
  # ```
  def self.new(pull : CBOR::Decoder)
    # Either the UUID was encoded as String or bytes (smaller).
    case pull.current_token
    when CBOR::Token::StringT
      new(pull.read_string)
    when CBOR::Token::BytesT
      new(pull.read_bytes)
    else
      raise "trying to get an UUID, but CBOR value isn't a string nor bytes: #{pull.current_token}"
    end
  end

  # Returns UUID as CBOR value.
  #
  # NOTE: `require "uuid/cbor"` is required to opt-in to this feature.
  #
  # ```
  # uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
  # uuid.to_cbor
  # ```
  def to_cbor(cbor : CBOR::Encoder)
    cbor.write(@bytes.to_slice)
  end
end
-- 
2.30.2
crystal-cbor/patches/.build.yml: SUCCESS in 28s

[UUID - this time without opt-in functionality.][0] v2 from [~karchnu][1]

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

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