on f476ba7 help sign in
elk/compiler/Build/Interface.hs
43 lines 1.6 kB history raw
1{-# OPTIONS_GHC -W #-}
2module Build.Interface (load,isValid) where
4import qualified Data.ByteString.Lazy as L
5import qualified Data.Binary as Binary
7import qualified Build.Print as Print
8import qualified Elm.Compiler.Version as Compiler
9import System.Directory (doesFileExist)
10import AST.Module as Module
13load :: Binary.Binary a => FilePath -> IO a
14load filePath =
15 do exists <- doesFileExist filePath
16 case exists of
17 False -> Print.failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
18 True -> do
19 bytes <- L.readFile filePath
20 case Binary.decodeOrFail bytes of
21 Right (_, _, binaryInfo) -> return binaryInfo
22 Left (_, offset, err) -> Print.failure $ msg offset err
24 where
25 msg offset err = concat
26 [ "Error reading build artifact: ", filePath, "\n"
27 , " '", err, "' at offset ", show offset, "\n"
28 , " The file was generated by a previous build and may be outdated or corrupt.\n"
29 , " Please remove the file and try again."
30 ]
33isValid :: FilePath -> (String, Module.Interface) -> Either String (String, Module.Interface)
34isValid filePath (name, interface) =
35 let version = iVersion interface in
36 if version == Compiler.version
37 then Right (name, interface)
38 else Left $ concat
39 [ "Error reading build artifact: ", filePath, "\n"
40 , " It was generated by version ", version, " of the compiler,\n"
41 , " but you are using version ", Compiler.version, "\n"
42 , " Please remove the file and try again.\n"
43 ]