@@ -1,31 +0,0 @@
1−{-# OPTIONS_GHC -W #-}
2−{-# LANGUAGE FlexibleContexts #-}
3−module Build.Artifact (read) where
4−
5−import Prelude hiding (read)
6−import Control.Monad.Error (MonadError, throwError, MonadIO, liftIO)
7−import qualified Data.ByteString.Lazy as LBS
8−import qualified Data.Binary as Binary
9−import System.Directory (doesFileExist)
10−
11−
12−read :: (MonadIO m, MonadError String m, Binary.Binary a) => FilePath -> m a
13−read filePath =
14− do exists <- liftIO (doesFileExist filePath)
15− if exists then decode else throwError notFound
16− where
17− decode =
18− do bytes <- liftIO (LBS.readFile filePath)
19− case Binary.decodeOrFail bytes of
20− Right (_, _, binaryInfo) -> return binaryInfo
21− Left (_, _, _) -> throwError corrupted
22−
23− corrupted =
24− concat
25− [ "Error reading build artifact ", filePath, "\n"
26− , " The file was generated by a previous build and may be outdated or corrupt.\n"
27− , " Please remove the file and try again."
28− ]
29−
30− notFound =
31− "Unable to find file " ++ filePath ++ " for deserialization!"