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