Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

0.3 release switches to cereal

  • Loading branch information...
commit d2043bcdbf3f1f161daba82185bbf1bcafb116d6 1 parent 0732a3c
@acfoltzer authored
Showing with 32 additions and 30 deletions.
  1. +3 −4 nbt.cabal
  2. +15 −15 src/Data/NBT.hs
  3. +12 −9 test/Tests.hs
  4. +2 −2 test/nbt-tests.cabal
View
7 nbt.cabal
@@ -1,5 +1,5 @@
Name: nbt
-Version: 0.2
+Version: 0.3
Synopsis: A parser/serializer for Minecraft's Named Binary Tag (NBT) data format.
Description: This package includes a data type for the NBT file format, notably used to represent saved data in Minecraft.
Homepage: https://github.com/acfoltzer/nbt
@@ -29,8 +29,7 @@ Library
Exposed-modules: Data.NBT
hs-source-dirs: src
Build-depends: base == 4.*,
- binary == 0.5.*,
bytestring == 0.9.*,
- data-binary-ieee754 == 0.4.*,
+ cereal >= 0.3.4 && < 0.4,
utf8-string == 0.3.*
- ghc-options: -Wall -O2
+ ghc-options: -Wall
View
30 src/Data/NBT.hs
@@ -19,22 +19,22 @@ NBT specification for details:
module Data.NBT where
-import Data.Binary (
- Binary (..)
+import Data.Serialize (
+ Serialize (..)
, getWord8
, putWord8
)
-import Data.Binary.Get (
+import Data.Serialize.Get (
Get
- , getLazyByteString
+ , getByteString
, lookAhead
, skip
)
-import Data.Binary.Put ( putLazyByteString )
-import Data.Binary.IEEE754
+import Data.Serialize.Put ( putByteString )
+import Data.Serialize.IEEE754
-import qualified Data.ByteString.Lazy as B
-import qualified Data.ByteString.Lazy.UTF8 as UTF8 ( fromString, toString )
+import qualified Data.ByteString as B
+import qualified Data.ByteString.UTF8 as UTF8 ( fromString, toString )
import Data.Int ( Int8, Int16, Int32, Int64 )
import Control.Applicative ( (<$>) )
@@ -55,7 +55,7 @@ data TagType = EndType
| CompoundType
deriving (Show, Eq, Enum)
-instance Binary TagType where
+instance Serialize TagType where
get = fmap (toEnum . fromIntegral) getWord8
put = putWord8 . fromIntegral . fromEnum
@@ -75,7 +75,7 @@ data NBT = EndTag
| CompoundTag (Maybe String) [NBT]
deriving (Show, Eq)
-instance Binary NBT where
+instance Serialize NBT where
get = get >>= \ty ->
case ty of
EndType -> return EndTag
@@ -107,11 +107,11 @@ instance Binary NBT where
getDouble n = DoubleTag n <$> getFloat64be
getByteArray n = do
len <- get :: Get Int32
- ByteArrayTag n len <$> getLazyByteString (toEnum $ fromEnum len)
+ ByteArrayTag n len <$> getByteString (toEnum $ fromEnum len)
getString n = do
len <- get :: Get Int16
StringTag n len <$> UTF8.toString
- <$> getLazyByteString (toEnum $ fromEnum len)
+ <$> getByteString (toEnum $ fromEnum len)
getList n = do
ty <- get :: Get TagType
len <- get :: Get Int32
@@ -176,17 +176,17 @@ instance Binary NBT where
CompoundTag Nothing ts -> putCompound ts
where
-- name and payload helpers
- putName n = putString n
+ putName = putString
putByte = put
putShort = put
putInt = put
putLong = put
putFloat = putFloat32be
putDouble = putFloat64be
- putByteArray len bs = put len >> putLazyByteString bs
+ putByteArray len bs = put len >> putByteString bs
putString str = let bs = UTF8.fromString str
len = fromIntegral (B.length bs)
- in put (len :: Int16) >> putLazyByteString bs
+ in put (len :: Int16) >> putByteString bs
putList ty len ts = put ty >> put len >> forM_ ts put
putCompound ts = forM_ ts put >> put EndTag
View
21 test/Tests.hs
@@ -5,9 +5,10 @@ module Main where
import Data.NBT
import qualified Codec.Compression.GZip as GZip
-import qualified Data.ByteString.Lazy as B
-import Data.Binary ( Binary (..), decode, encode )
-import qualified Data.ByteString.Lazy.UTF8 as UTF8 ( fromString, toString )
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as L
+import Data.Serialize ( Serialize (..), decode, encode )
+import qualified Data.ByteString.UTF8 as UTF8 ( fromString, toString )
import Control.Applicative
import Control.Monad
@@ -22,8 +23,10 @@ import Test.HUnit
instance Arbitrary TagType where
arbitrary = toEnum <$> choose (0, 10)
+eitherErr = either error id
+
prop_TagType :: TagType -> Bool
-prop_TagType ty = decode (encode ty) == ty
+prop_TagType ty = eitherErr (decode (encode ty)) == ty
instance Arbitrary NBT where
arbitrary = do
@@ -59,15 +62,15 @@ instance Arbitrary NBT where
mkArb ty (Just name)
prop_NBTroundTrip :: NBT -> Bool
-prop_NBTroundTrip nbt = decode (encode nbt) == nbt
+prop_NBTroundTrip nbt = eitherErr (decode (encode nbt)) == nbt
testWorld = do
- fileL <- GZip.decompress <$> B.readFile "testWorld/level.dat"
- let file = B.pack (B.unpack fileL)
- dec = (decode file :: NBT)
+ fileL <- GZip.decompress <$> L.readFile "testWorld/level.dat"
+ let file = B.pack (L.unpack fileL)
+ dec = eitherErr (decode file) :: NBT
enc = encode dec
enc @?= file
- decode enc @?= dec
+ eitherErr (decode enc) @?= dec
tests = [
testProperty "Tag roundtrip" prop_TagType
View
4 test/nbt-tests.cabal
@@ -12,7 +12,7 @@ Executable nbt-tests
Main-is: Tests.hs
Build-depends: base == 4.*,
bytestring == 0.9.*,
- binary == 0.5.*,
+ cereal >= 0.3.4 && < 0.4,
zlib == 0.5.*,
utf8-string == 0.3.*,
HUnit == 1.2.*,
@@ -20,4 +20,4 @@ Executable nbt-tests
test-framework == 0.4.*,
test-framework-quickcheck2 == 0.2.*,
test-framework-hunit == 0.2.*,
- nbt
+ nbt == 0.3
Please sign in to comment.
Something went wrong with that request. Please try again.