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