on master help sign in
Make Binary serialization code nicer
5c509f2Evan Czaplicki
compiler/SourceSyntax/Module.hs+9−12
@@ -10,7 +10,6 @@ import SourceSyntax.Expression (LExpr)
10import SourceSyntax.Declaration
10import SourceSyntax.Declaration
11import SourceSyntax.Type
11import SourceSyntax.Type
12import System.FilePath (joinPath)
12import System.FilePath (joinPath)
13−import Control.Monad (liftM)
14
13
15import qualified Elm.Internal.Version as Version
14import qualified Elm.Internal.Version as Version
16
15
@@ -25,20 +24,18 @@ data ImportMethod = As String | Importing [String] | Hiding [String]
25 deriving (Eq, Ord, Show)
24 deriving (Eq, Ord, Show)
26
25
27instance Binary ImportMethod where
26instance Binary ImportMethod where
27+ put method =
28+ let put' n info = putWord8 n >> put info in
29+ case method of
28− put (As s) = do put (0 :: Word8)
30+ As s -> put' 0 s
29− put s
30−
31− put (Importing ss) = do put (1 :: Word8)
31+ Importing ss -> put' 1 ss
32− put ss
33−
34− put (Hiding ss) = do put (2 :: Word8)
32+ Hiding ss -> put' 2 ss
35− put ss
36
33
37 get = do tag <- getWord8
34 get = do tag <- getWord8
38 case tag of
35 case tag of
39− 0 -> liftM As get
36+ 0 -> As <$> get
40− 1 -> liftM Importing get
37+ 1 -> Importing <$> get
41− 2 -> liftM Hiding get
38+ 2 -> Hiding <$> get
42 _ -> error "Error reading valid ImportMethod type from serialized string"
39 _ -> error "Error reading valid ImportMethod type from serialized string"
43
40
44data MetadataModule t v = MetadataModule {
41data MetadataModule t v = MetadataModule {