diff --git a/shard.yml b/shard.yml index 8658c08..0d8fca8 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: bindata -version: 1.7.0 +version: 1.8.0 development_dependencies: ameba: diff --git a/spec/bindata_spec.cr b/spec/bindata_spec.cr index ef2bcab..e2963f6 100644 --- a/spec/bindata_spec.cr +++ b/spec/bindata_spec.cr @@ -1,6 +1,13 @@ require "./helper" describe BinData do + it "should parse an object from a Slice" do + r = Header.new + r.name = "foo" + slice = r.to_slice + Header.from_slice(slice).name.should eq "foo" + end + it "should parse an object from an IO" do io = IO::Memory.new io.write_bytes 5 diff --git a/spec/bitfield_spec.cr b/spec/bitfield_spec.cr index af38605..7ebb7dd 100644 --- a/spec/bitfield_spec.cr +++ b/spec/bitfield_spec.cr @@ -92,7 +92,7 @@ describe BinData::BitField do io = IO::Memory.new io.write_byte(0x80) expect_raises BinData::ParseError, "failed to parse ByteSized.bitfield.header" do - obj = io.read_bytes(ByteSized) + io.read_bytes(ByteSized) end end end diff --git a/src/bindata.cr b/src/bindata.cr index cdf4e10..a7f70ee 100644 --- a/src/bindata.cr +++ b/src/bindata.cr @@ -33,6 +33,11 @@ abstract class BinData IO::ByteFormat::SystemEndian end + def self.from_slice(bytes : Slice, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) + io = IO::Memory.new(bytes) + from_io(io, format) + end + def to_slice io = IO::Memory.new io.write_bytes self