on ed4be06 help sign in
elk/compiler/InterfaceSerialization.hs
52 lines 2.0 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 qualified Elm.Internal.Version as Version
10import System.Directory (doesFileExist)
11import System.IO (hPutStrLn, stderr)
12import System.Exit (exitFailure)
14import SourceSyntax.Module
16loadInterface :: FilePath -> IO (Either String L.ByteString)
17loadInterface filePath = do
18 exists <- doesFileExist filePath
19 if exists
20 then do
21 byteString <- L.readFile filePath
22 return $ Right byteString
24 else
25 return $ Left $ "Unable to find file " ++ filePath ++
26 " for deserialization!"
28interfaceDecode :: Binary.Binary a =>
29 FilePath -> L.ByteString -> Either String a
30interfaceDecode filePath bytes = do
31 case Binary.decodeOrFail bytes of
32 Right (_, _, binaryInfo) -> Right binaryInfo
34 Left (_, offset, err) ->
35 Left $ concat [ "Error reading build artifact: ", filePath, "\n"
36 , " The exact error was '", err, "' at offset ", show offset, ".\n"
37 , " The file was generated by a previous build and may be outdated or corrupt.\n"
38 , " Please remove the file and try again."
39 ]
41validVersion :: FilePath -> (String, ModuleInterface) ->
42 Either String (String, ModuleInterface)
43validVersion filePath (name, interface) =
44 if iVersion interface == Version.elmVersion then
45 Right (name, interface)
46 else
47 Left $ concat
48 [ "Error reading build artifact: ", filePath, "\n"
49 , " It was generated by a different version of the compiler: "
50 , show (iVersion interface), "\n"
51 , " Please remove the file and try again.\n"
52 ]