11{-# LANGUAGE OverloadedStrings #-}
2+ {-# LANGUAGE ScopedTypeVariables #-}
23-- | Tag a Binary instance with the stack version number to ensure we're
34-- reading a compatible format.
45module Data.Binary.VersionTagged
56 ( taggedDecodeOrLoad
67 , taggedEncodeFile
8+ , BinarySchema (.. )
79 ) where
810
911import Control.Monad.IO.Class (MonadIO , liftIO )
1012import Data.Binary (Binary (.. ), encodeFile , decodeFileOrFail , putWord8 , getWord8 )
1113import Control.Exception.Enclosed (tryIO )
12- import qualified Paths_stack
13- import Stack.Types.Version (Version , fromCabalVersion )
1414import System.FilePath (takeDirectory )
1515import System.Directory (createDirectoryIfMissing )
1616import qualified Data.ByteString as S
1717import Data.ByteString (ByteString )
1818import Control.Monad (forM_ , when )
19-
20- tag :: Version
21- tag = fromCabalVersion Paths_stack. version
19+ import Data.Proxy
2220
2321magic :: ByteString
24- magic = " STACK"
22+ magic = " stack"
23+
24+ -- | A @Binary@ instance that also has a schema version
25+ class Binary a => BinarySchema a where
26+ binarySchema :: Proxy a -> Int
2527
2628newtype WithTag a = WithTag a
27- instance Binary a => Binary (WithTag a ) where
29+ instance forall a . BinarySchema a => Binary (WithTag a ) where
2830 get = do
2931 forM_ (S. unpack magic) $ \ w -> do
3032 w' <- getWord8
3133 when (w /= w')
3234 $ fail " Mismatched magic string, forcing a recompute"
3335 tag' <- get
34- if tag == tag'
36+ if binarySchema ( Proxy :: Proxy a ) == tag'
3537 then fmap WithTag get
3638 else fail " Mismatched tags, forcing a recompute"
3739 put (WithTag x) = do
3840 mapM_ putWord8 $ S. unpack magic
39- put tag
41+ put (binarySchema ( Proxy :: Proxy a ))
4042 put x
4143
4244-- | Write to the given file, with a version tag.
43- taggedEncodeFile :: (Binary a , MonadIO m )
45+ taggedEncodeFile :: (BinarySchema a , MonadIO m )
4446 => FilePath
4547 -> a
4648 -> m ()
@@ -51,7 +53,7 @@ taggedEncodeFile fp x = liftIO $ do
5153-- | Read from the given file. If the read fails, run the given action and
5254-- write that back to the file. Always starts the file off with the version
5355-- tag.
54- taggedDecodeOrLoad :: (Binary a , MonadIO m )
56+ taggedDecodeOrLoad :: (BinarySchema a , MonadIO m )
5557 => FilePath
5658 -> m a
5759 -> m a
0 commit comments