From f5b97a6acefb933199733fd7b446039985bf841f Mon Sep 17 00:00:00 2001 From: Stephen von Takach Date: Wed, 21 Sep 2022 19:08:01 +1000 Subject: [PATCH] feat: bump version and move exceptions to their own file --- .gitignore | 3 ++- shard.lock | 2 +- shard.yml | 2 +- spec/bitfield_spec.cr | 2 +- src/bindata.cr | 45 +++------------------------------------ src/bindata/exceptions.cr | 41 +++++++++++++++++++++++++++++++++++ 6 files changed, 49 insertions(+), 46 deletions(-) create mode 100644 src/bindata/exceptions.cr diff --git a/.gitignore b/.gitignore index 22d79d9..6ea62a4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ lib app *.dwarf *.DS_Store -bin/ameba +bin + diff --git a/shard.lock b/shard.lock index c09d522..0831592 100644 --- a/shard.lock +++ b/shard.lock @@ -2,5 +2,5 @@ version: 2.0 shards: ameba: git: https://github.com/veelenga/ameba.git - version: 0.14.0 + version: 1.1.0 diff --git a/shard.yml b/shard.yml index ac429be..a12eaf2 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: bindata -version: 1.10.0 +version: 1.11.0 crystal: ">= 1.0.0" development_dependencies: diff --git a/spec/bitfield_spec.cr b/spec/bitfield_spec.cr index 55b16cd..1650721 100644 --- a/spec/bitfield_spec.cr +++ b/spec/bitfield_spec.cr @@ -91,7 +91,7 @@ describe BinData::BitField do it "should raise an error when there is not enough data" do io = IO::Memory.new io.write_byte(0x80) - + ex = expect_raises BinData::ParseError, "Failed to parse ByteSized.bitfield.header" do io.read_bytes(ByteSized) end diff --git a/src/bindata.cr b/src/bindata.cr index 9a98f00..8e8628f 100644 --- a/src/bindata.cr +++ b/src/bindata.cr @@ -1,44 +1,7 @@ -abstract class BinData - class CustomException < Exception - getter klass : String? - getter field : String? - getter field_type : String? - - def initialize(message, ex : Exception) - super(message, ex) - end - - def initialize(message) - super(message) - end - end - - class VerificationException < CustomException; end - - class WritingVerificationException < VerificationException - def initialize(@klass, @field, @field_type) - super("Failed to verify writing #{field_type} at #{klass}.#{field}") - end - end - - class ReadingVerificationException < VerificationException - def initialize(@klass, @field, @field_type) - super("Failed to verify reading #{field_type} at #{klass}.#{field}") - end - end - - class ParseError < CustomException - def initialize(@klass, @field, ex : Exception) - super("Failed to parse #{klass}.#{field}", ex) - end - end - - class WriteError < CustomException - def initialize(@klass, @field, ex : Exception) - super("Failed to write #{klass}.#{field}", ex) - end - end +require "./bindata/exceptions" +require "./bindata/bitfield" +abstract class BinData INDEX = [-1] BIT_PARTS = [] of Nil CUSTOM_TYPES = [] of BinData.class @@ -519,5 +482,3 @@ abstract class BinData property {{name.id}} : Bytes = {% if default %} {{default}}.to_slice {% else %} Bytes.new(0) {% end %} end end - -require "./bindata/bitfield" diff --git a/src/bindata/exceptions.cr b/src/bindata/exceptions.cr new file mode 100644 index 0000000..0ca47e4 --- /dev/null +++ b/src/bindata/exceptions.cr @@ -0,0 +1,41 @@ +abstract class BinData + class CustomException < Exception + getter klass : String? + getter field : String? + getter field_type : String? + + def initialize(message, ex : Exception) + super(message, ex) + end + + def initialize(message) + super(message) + end + end + + class VerificationException < CustomException; end + + class WritingVerificationException < VerificationException + def initialize(@klass, @field, @field_type) + super("Failed to verify writing #{field_type} at #{klass}.#{field}") + end + end + + class ReadingVerificationException < VerificationException + def initialize(@klass, @field, @field_type) + super("Failed to verify reading #{field_type} at #{klass}.#{field}") + end + end + + class ParseError < CustomException + def initialize(@klass, @field, ex : Exception) + super("Failed to parse #{klass}.#{field}", ex) + end + end + + class WriteError < CustomException + def initialize(@klass, @field, ex : Exception) + super("Failed to write #{klass}.#{field}", ex) + end + end +end