@@ -25,6 +25,7 @@ import qualified Data.Map as M
2525import Data.Monoid ((<>) )
2626import qualified Data.Set as S
2727import Data.Store
28+ import Data.Store.Core (unsafeEncodeWith )
2829import Data.Store.Version
2930import qualified Data.Text as T
3031import Language.Haskell.TH
@@ -33,65 +34,65 @@ import Path.IO (ensureDir)
3334import Prelude
3435
3536versionedEncodeFile :: Data a => VersionConfig a -> Q Exp
36- versionedEncodeFile vc = [e | \fp x -> storeEncodeFile fp ($(wrapVersion vc) x ) |]
37+ versionedEncodeFile vc = [e | storeEncodeFile $(encodeWithVersionQ vc) $(decodeWithVersionQ vc ) |]
3738
3839versionedDecodeOrLoad :: Data a => VersionConfig a -> Q Exp
39- versionedDecodeOrLoad vc = [| versionedDecodeOrLoadImpl $ (wrapVersion vc) $ (checkVersion vc) | ]
40+ versionedDecodeOrLoad vc = [| versionedDecodeOrLoadImpl $ (encodeWithVersionQ vc) $ (decodeWithVersionQ vc) | ]
4041
4142versionedDecodeFile :: Data a => VersionConfig a -> Q Exp
42- versionedDecodeFile vc = [e | versionedDecodeFileImpl $(checkVersion vc) |]
43+ versionedDecodeFile vc = [e | versionedDecodeFileImpl $(decodeWithVersionQ vc) |]
4344
4445-- | Write to the given file.
4546storeEncodeFile :: (Store a , MonadIO m , MonadLogger m , Eq a )
46- => Path Abs File
47+ => (a -> (Int , Poke () ))
48+ -> Peek a
49+ -> Path Abs File
4750 -> a
4851 -> m ()
49- storeEncodeFile fp x = do
52+ storeEncodeFile pokeFunc peekFunc fp x = do
5053 let fpt = T. pack (toFilePath fp)
5154 $ logDebug $ " Encoding " <> fpt
5255 ensureDir (parent fp)
53- let encoded = encode x
54- assert (decodeEx encoded == x) $ liftIO $ BS. writeFile (toFilePath fp) encoded
56+ let (sz, poker) = pokeFunc x
57+ encoded = unsafeEncodeWith poker sz
58+ assert (decodeExWith peekFunc encoded == x) $ liftIO $ BS. writeFile (toFilePath fp) encoded
5559 $ logDebug $ " Finished writing " <> fpt
5660
5761-- | Read from the given file. If the read fails, run the given action and
5862-- write that back to the file. Always starts the file off with the
5963-- version tag.
6064versionedDecodeOrLoadImpl :: (Store a , Eq a , MonadIO m , MonadLogger m , MonadBaseControl IO m )
61- => (a -> WithVersion a )
62- -> ( WithVersion a -> Either VersionCheckException a )
65+ => (a -> ( Int , Poke () ) )
66+ -> Peek a
6367 -> Path Abs File
6468 -> m a
6569 -> m a
66- versionedDecodeOrLoadImpl wrap check fp mx = do
70+ versionedDecodeOrLoadImpl pokeFunc peekFunc fp mx = do
6771 let fpt = T. pack (toFilePath fp)
6872 $ logDebug $ " Trying to decode " <> fpt
69- mres <- versionedDecodeFileImpl check fp
73+ mres <- versionedDecodeFileImpl peekFunc fp
7074 case mres of
7175 Just x -> do
7276 $ logDebug $ " Success decoding " <> fpt
7377 return x
7478 _ -> do
7579 $ logDebug $ " Failure decoding " <> fpt
7680 x <- mx
77- storeEncodeFile fp (wrap x)
81+ storeEncodeFile pokeFunc peekFunc fp x
7882 return x
7983
8084versionedDecodeFileImpl :: (Store a , MonadIO m , MonadLogger m , MonadBaseControl IO m )
81- => ( WithVersion a -> Either VersionCheckException a )
85+ => Peek a
8286 -> Path loc File
8387 -> m (Maybe a )
84- versionedDecodeFileImpl check fp = do
88+ versionedDecodeFileImpl peekFunc fp = do
8589 mbs <- liftIO (Just <$> BS. readFile (toFilePath fp)) `catch` \ (err :: IOException ) -> do
8690 $ logDebug (" Exception ignored when attempting to load " <> T. pack (toFilePath fp) <> " : " <> T. pack (show err))
8791 return Nothing
8892 case mbs of
8993 Nothing -> return Nothing
9094 Just bs ->
91- liftIO (do decoded <- decodeIO bs
92- return $ case check decoded of
93- Right res -> Just res
94- _ -> Nothing ) `catch` \ (err :: PeekException ) -> do
95+ liftIO (Just <$> decodeIOWith peekFunc bs) `catch` \ (err :: PeekException ) -> do
9596 let fpt = T. pack (toFilePath fp)
9697 $ logDebug (" Error while decoding " <> fpt <> " : " <> T. pack (show err) <> " (this might not be an error, when switching between stack versions)" )
9798 return Nothing
0 commit comments