Rename InterfaceSerialization file and change the function names in it

This commit is contained in:
Evan Czaplicki 2014-01-04 15:19:30 +01:00
parent 7e64531d2e
commit 170006534d
5 changed files with 57 additions and 63 deletions

View file

@ -60,7 +60,6 @@ Library
Transform.SortDefinitions,
Transform.Substitute,
Metadata.Prelude,
InterfaceSerialization,
Parse.Binop,
Parse.Declaration,
Parse.Expression,
@ -87,6 +86,7 @@ Library
Build.Dependencies,
Build.File,
Build.Flags,
Build.Interface,
Build.Print,
Build.Source,
Build.Utils,
@ -140,7 +140,6 @@ Executable elm
Transform.SortDefinitions,
Transform.Substitute,
Metadata.Prelude,
InterfaceSerialization,
Parse.Binop,
Parse.Declaration,
Parse.Expression,
@ -167,6 +166,7 @@ Executable elm
Build.Dependencies,
Build.File,
Build.Flags,
Build.Interface,
Build.Print,
Build.Source,
Build.Utils,

View file

@ -15,10 +15,10 @@ import qualified Data.ByteString.Lazy as L
import qualified Build.Utils as Utils
import qualified Build.Flags as Flag
import qualified Build.Source as Source
import qualified Build.Interface as Interface
import qualified Build.Print as Print
import qualified Build.Source as Source
import qualified Generate.JavaScript as JS
import qualified InterfaceSerialization as IS
import qualified Parse.Module as Parser
import qualified SourceSyntax.Module as M
@ -57,9 +57,9 @@ alreadyCompiled flags filePath = do
retrieve :: Flag.Flags -> Map.Map String M.ModuleInterface -> FilePath
-> IO (String, M.ModuleInterface)
retrieve flags interfaces filePath = do
bytes <- IS.loadInterface (Utils.elmi flags filePath)
let binary = IS.interfaceDecode (Utils.elmi flags filePath) =<< bytes
case IS.validVersion filePath =<< binary of
bytes <- Interface.load (Utils.elmi flags filePath)
let binary = Interface.decode (Utils.elmi flags filePath) =<< bytes
case Interface.isValid filePath =<< binary of
Right (name, interface) ->
do when (Flag.print_types flags) (Print.interfaceTypes interfaces interface)
return (name, interface)

View file

@ -0,0 +1,46 @@
{-# OPTIONS_GHC -W #-}
module Build.Interface (load,decode,isValid) where
import qualified Data.ByteString.Lazy as L
import qualified Data.Binary as Binary
import qualified Elm.Internal.Version as Version
import System.Directory (doesFileExist)
import SourceSyntax.Module
load :: FilePath -> IO (Either String L.ByteString)
load filePath = do
exists <- doesFileExist filePath
if exists
then do
byteString <- L.readFile filePath
return $ Right byteString
else
return $ Left $ "Unable to find file " ++ filePath ++
" for deserialization!"
decode :: Binary.Binary a => FilePath -> L.ByteString -> Either String a
decode filePath bytes =
case Binary.decodeOrFail bytes of
Right (_, _, binaryInfo) -> Right binaryInfo
Left (_, offset, err) ->
Left $ concat $
[ "Error reading build artifact: ", filePath, "\n"
, " The exact error was '", err, "' at offset ", show offset, ".\n"
, " The file was generated by a previous build and may be outdated or corrupt.\n"
, " Please remove the file and try again."
]
isValid :: FilePath -> (String, ModuleInterface) -> Either String (String, ModuleInterface)
isValid filePath (name, interface) =
if iVersion interface == Version.elmVersion
then Right (name, interface)
else Left $ concat
[ "Error reading build artifact: ", filePath, "\n"
, " It was generated by a different version of the compiler: "
, show (iVersion interface), "\n"
, " Please remove the file and try again.\n"
]

View file

@ -1,51 +0,0 @@
{-# OPTIONS_GHC -W #-}
module InterfaceSerialization ( loadInterface
, interfaceDecode
, validVersion
) where
import qualified Data.ByteString.Lazy as L
import qualified Data.Binary as Binary
import qualified Elm.Internal.Version as Version
import System.Directory (doesFileExist)
import SourceSyntax.Module
loadInterface :: FilePath -> IO (Either String L.ByteString)
loadInterface filePath = do
exists <- doesFileExist filePath
if exists
then do
byteString <- L.readFile filePath
return $ Right byteString
else
return $ Left $ "Unable to find file " ++ filePath ++
" for deserialization!"
interfaceDecode :: Binary.Binary a =>
FilePath -> L.ByteString -> Either String a
interfaceDecode filePath bytes = do
case Binary.decodeOrFail bytes of
Right (_, _, binaryInfo) -> Right binaryInfo
Left (_, offset, err) ->
Left $ concat [ "Error reading build artifact: ", filePath, "\n"
, " The exact error was '", err, "' at offset ", show offset, ".\n"
, " The file was generated by a previous build and may be outdated or corrupt.\n"
, " Please remove the file and try again."
]
validVersion :: FilePath -> (String, ModuleInterface) ->
Either String (String, ModuleInterface)
validVersion filePath (name, interface) =
if iVersion interface == Version.elmVersion then
Right (name, interface)
else
Left $ concat
[ "Error reading build artifact: ", filePath, "\n"
, " It was generated by a different version of the compiler: "
, show (iVersion interface), "\n"
, " Please remove the file and try again.\n"
]

View file

@ -7,8 +7,7 @@ import qualified Paths_Elm as Path
import System.Exit
import System.IO
import SourceSyntax.Module
import qualified InterfaceSerialization as IS
import qualified Build.Interface as Interface
add :: Bool -> Module def -> Module def
add noPrelude (Module name exs ims decls) = Module name exs (customIms ++ ims) decls
@ -49,9 +48,9 @@ hasInterfaces ifaces = Right ifaces
readDocs :: FilePath -> IO Interfaces
readDocs filePath = do
bytes <- IS.loadInterface filePath
let interfaces = IS.interfaceDecode filePath =<< bytes
case mapM (IS.validVersion filePath) =<< hasInterfaces =<< interfaces of
bytes <- Interface.load filePath
let interfaces = Interface.decode filePath =<< bytes
case mapM (Interface.isValid filePath) =<< hasInterfaces =<< interfaces of
Right ifaces -> return $ Map.fromList ifaces
Left err -> do
hPutStrLn stderr err