nbt 0.2 → 0.3
raw patch · 4 files changed
+32/−30 lines, 4 filesdep +cerealdep −binarydep −data-binary-ieee754
Dependencies added: cereal
Dependencies removed: binary, data-binary-ieee754
Files
- nbt.cabal +3/−4
- src/Data/NBT.hs +15/−15
- test/Tests.hs +12/−9
- test/nbt-tests.cabal +2/−2
nbt.cabal view
@@ -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 @@ 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
src/Data/NBT.hs view
@@ -19,22 +19,22 @@ 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 @@ | 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 @@ | 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 @@ 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 @@ 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
test/Tests.hs view
@@ -5,9 +5,10 @@ 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 @@ 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 @@ 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
test/nbt-tests.cabal view
@@ -12,7 +12,7 @@ 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 @@ test-framework == 0.4.*, test-framework-quickcheck2 == 0.2.*, test-framework-hunit == 0.2.*,- nbt+ nbt == 0.3