@@ -1,46 +1,46 @@
1{-# OPTIONS_GHC -W #-}
1{-# OPTIONS_GHC -W #-}
2−module Build.Interface (load,decode,isValid) where
2+module Build.Interface (load,isValid) where
4import qualified Data.ByteString.Lazy as L
4import qualified Data.ByteString.Lazy as L
5import qualified Data.Binary as Binary
5import qualified Data.Binary as Binary
7import qualified Elm.Internal.Version as Version
7import qualified Elm.Internal.Version as Version
8import System.Directory (doesFileExist)
8import System.Directory (doesFileExist)
10import SourceSyntax.Module
12import SourceSyntax.Module
12−load :: FilePath -> IO (Either String L.ByteString)
14+load :: Binary.Binary a => FilePath -> IO a
13−load filePath = do
15+load filePath =
14− exists <- doesFileExist filePath
16+ do exists <- doesFileExist filePath
15− if exists
17+ case exists of
17− byteString <- L.readFile filePath
18− return $ Right byteString
21− return $ Left $ "Unable to find file " ++ filePath ++
18+ False -> failure $ "Unable to find file " ++ filePath ++ " for deserialization!"
22− " for deserialization!"
19+ True -> do
23−
20+ bytes <- L.readFile filePath
24−decode :: Binary.Binary a => FilePath -> L.ByteString -> Either String a
25−decode filePath bytes =
26− case Binary.decodeOrFail bytes of
21+ case Binary.decodeOrFail bytes of
27− Right (_, _, binaryInfo) -> Right binaryInfo
22+ Right (_, _, binaryInfo) -> return binaryInfo
29− Left (_, offset, err) ->
23+ Left (_, offset, err) -> failure $ msg offset err
26+ failure err = do hPutStrLn stderr err
30− Left $ concat $
29+ msg offset err = concat
31− [ "Error reading build artifact: ", filePath, "\n"
30+ [ "Error reading build artifact: ", filePath, "\n"
32− , " The exact error was '", err, "' at offset ", show offset, ".\n"
31+ , " '", err, "' at offset ", show offset, "\n"
33− , " The file was generated by a previous build and may be outdated or corrupt.\n"
32+ , " The file was generated by a previous build and may be outdated or corrupt.\n"
34− , " Please remove the file and try again."
33+ , " Please remove the file and try again."
37isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
36isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
38isValid filePath (name, interface) =
37isValid filePath (name, interface) =
38+ let version = iVersion interface in
39− if iVersion interface == Version.elmVersion
39+ if version == Version.elmVersion
40 then Right (name, interface)
40 then Right (name, interface)
41 else Left $ concat
41 else Left $ concat
42 [ "Error reading build artifact: ", filePath, "\n"
42 [ "Error reading build artifact: ", filePath, "\n"
43− , " It was generated by a different version of the compiler: "
43+ , " It was generated by version ", show version, " of the compiler,\n"
44− , show (iVersion interface), "\n"
44+ , " but you are using version ", show Version.elmVersion, "\n"
45 , " Please remove the file and try again.\n"
45 , " Please remove the file and try again.\n"