on b1bca38 help sign in
elk/compiler/InterfaceSerialization.hs
50 lines 1.8 kB history raw
1module InterfaceSerialization ( loadInterface
2 , interfaceDecode
3 , validVersion
4 ) where
6import qualified Data.ByteString.Lazy as L
7import qualified Data.Binary as Binary
9import Paths_Elm (version)
10import Data.Version (showVersion)
11import System.Directory (doesFileExist)
12import System.IO (hPutStrLn, stderr)
13import System.Exit (exitFailure)
15import SourceSyntax.Module
17loadInterface :: FilePath -> IO (Either String L.ByteString)
18loadInterface filePath = do
19 exists <- doesFileExist filePath
20 if exists
21 then do
22 byteString <- L.readFile filePath
23 return $ Right byteString
25 else
26 return $ Left $ "Unable to find file " ++ filePath ++
27 " for deserialization!"
29interfaceDecode :: Binary.Binary a => FilePath -> L.ByteString ->
30 Either String a
31interfaceDecode filePath bytes = do
32 case Binary.decodeOrFail bytes of
33 Left (_, offset, err) -> do
34 Left $ "Got an error, '" ++ err ++ "' at offset " ++
35 show offset ++ " of " ++ filePath ++ ".\n\n" ++
36 "This error may be due to an outdated or corrupt " ++
37 "artifact from a previous build. Please rebuild " ++
38 filePath ++ " and try again."
40 Right (_, _, binaryInfo) -> Right binaryInfo
42validVersion :: FilePath -> (String, ModuleInterface) ->
43 Either String (String, ModuleInterface)
44validVersion filePath (name, interface) =
45 if (iVersion interface) == showVersion version then
46 Right (name, interface)
47 else
48 Left $ "Found build artifacts created by a different " ++
49 "Elm compiler version. Please rebuild " ++
50 filePath ++ " and try again."