Commit 31eddce7 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺
Browse files

Add C.D.Compat.Binary.{encodeFile,decodeFileOrFail'}

parent f91e65a5
......@@ -9,11 +9,12 @@
module Distribution.Compat.Binary
( decodeOrFailIO
, decodeFileOrFail'
#if __GLASGOW_HASKELL__ >= 708 || MIN_VERSION_binary(0,7,0)
, module Data.Binary
#else
, Binary(..)
, decode, encode
, decode, encode, encodeFile
#endif
) where
......@@ -33,15 +34,21 @@ import Data.ByteString.Lazy (ByteString)
import Data.Binary
-- | Lazily reconstruct a value previously written to a file.
decodeFileOrFail' :: Binary a => FilePath -> IO (Either String a)
decodeFileOrFail' f = either (Left . snd) Right `fmap` decodeFileOrFail f
#else
import Data.Binary.Get
import Data.Binary.Put
import qualified Data.ByteString.Lazy as BSL
import Distribution.Compat.Binary.Class
import Distribution.Compat.Binary.Generic ()
-- | Decode a value from a lazy ByteString, reconstructing the original structure.
-- | Decode a value from a lazy ByteString, reconstructing the
-- original structure.
--
decode :: Binary a => ByteString -> a
decode = runGet get
......@@ -52,6 +59,14 @@ encode :: Binary a => a -> ByteString
encode = runPut . put
{-# INLINE encode #-}
-- | Lazily reconstruct a value previously written to a file.
decodeFileOrFail' :: Binary a => FilePath -> IO (Either String a)
decodeFileOrFail' f = decodeOrFailIO =<< BSL.readFile f
-- | Lazily serialise a value to a file
encodeFile :: Binary a => FilePath -> a -> IO ()
encodeFile f = BSL.writeFile f . encode
#endif
decodeOrFailIO :: Binary a => ByteString -> IO (Either String a)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment