on fa264dd help sign in
elk/src/Build/Artifact.hs
31 lines 1.1 kB history raw
1{-# OPTIONS_GHC -W #-}
2{-# LANGUAGE FlexibleContexts #-}
3module Build.Artifact (read) where
5import Prelude hiding (read)
6import Control.Monad.Error (MonadError, throwError, MonadIO, liftIO)
7import qualified Data.ByteString.Lazy as LBS
8import qualified Data.Binary as Binary
9import System.Directory (doesFileExist)
12read :: (MonadIO m, MonadError String m, Binary.Binary a) => FilePath -> m a
13read 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
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 ]
30 notFound =
31 "Unable to find file " ++ filePath ++ " for deserialization!"