Make Binary serialization code nicer

This commit is contained in:
Evan Czaplicki 2014-01-02 17:42:27 -08:00
parent 662fd7669a
commit 5c509f2f1e

View file

@ -10,7 +10,6 @@ import SourceSyntax.Expression (LExpr)
import SourceSyntax.Declaration import SourceSyntax.Declaration
import SourceSyntax.Type import SourceSyntax.Type
import System.FilePath (joinPath) import System.FilePath (joinPath)
import Control.Monad (liftM)
import qualified Elm.Internal.Version as Version import qualified Elm.Internal.Version as Version
@ -25,20 +24,18 @@ data ImportMethod = As String | Importing [String] | Hiding [String]
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
instance Binary ImportMethod where instance Binary ImportMethod where
put (As s) = do put (0 :: Word8) put method =
put s let put' n info = putWord8 n >> put info in
case method of
put (Importing ss) = do put (1 :: Word8) As s -> put' 0 s
put ss Importing ss -> put' 1 ss
Hiding ss -> put' 2 ss
put (Hiding ss) = do put (2 :: Word8)
put ss
get = do tag <- getWord8 get = do tag <- getWord8
case tag of case tag of
0 -> liftM As get 0 -> As <$> get
1 -> liftM Importing get 1 -> Importing <$> get
2 -> liftM Hiding get 2 -> Hiding <$> get
_ -> error "Error reading valid ImportMethod type from serialized string" _ -> error "Error reading valid ImportMethod type from serialized string"
data MetadataModule t v = MetadataModule { data MetadataModule t v = MetadataModule {