In the last chapter, you built a library named FilePack that will allow you to combine files with various types of data into a single archive. Throughout that chapter, you learned about using existential types to deal with heterogeneous collections of data. Although you were able to build a library with a nice API for creating archives, there was one glaring problem: after creating a file pack you had no way of unpacking the data.

In this chapter, we’ll work on the other half of the file packing program: decoding a file pack that can contain many different types of data. Just like when we were encoding data, we’ll want to support arbitrary data types that can be defined by the user, and we’ll want to support file packs that are made up of files with different types.

A File Archive Builder

Before we get started on the underlying problem of parsing our file archive, let’s review the code that we built in the last chapter that we’ll be re-using heavily throughout this chapter. If you already have a project with the code you built in the last chapter, then you can re-use that project in this chapter as well. If not, make sure to create a new project and copy the example code so that you’ll have everything you need as you work through the project in this chapter.

We started by creating a new record, FileData , that would let us store a file with some metadata:

​  ​ {-# LANGUAGE RecordWildCards #-} ​
​  ​ {-# LANGUAGE FlexibleInstances #-} ​
​  ​ {-# LANGUAGE ExistentialQuantification #-} ​
​  ​ {-# LANGUAGE TypeApplications #-} ​
​  
​  ​ module ​ ​ FilePackParser ​ ​ where ​
​  ​ import ​ ​ Data.Bits ​ (​ shift ​, (.&.), (.|.))
​  ​ import ​ ​ Data.ByteString ​ (​ ByteString ​)
​  ​ import ​ ​ qualified ​ ​ Data.ByteString ​ ​ as ​ BS
​  ​ import ​ ​ qualified ​ ​ Data.ByteString.Char8 ​ ​ as ​ BC
​  ​ import ​ ​ Data.Text ​ (​ Text ​)
​  ​ import ​ ​ Data.Text.Encoding ​ (​ decodeUtf8 ​, ​ encodeUtf8 ​)
​  ​ import ​ ​ Data.Word ​
​  ​ import ​ ​ System.Posix.Types ​ (​ CMode ​ (..), ​ FileMode ​)
​  
​  ​ data ​ ​ FileData ​ a = ​ FileData ​
​  { fileName :: ​ FilePath ​
​  , fileSize :: ​ Word32 ​
​  , filePermissions :: ​ FileMode ​
​  , fileData :: a
​  } ​ deriving ​ (​ Eq ​, ​ Show ​)

To make it easier for us to encode and decode file archives, we defined two new type classes: Encode and Decode . We also created some short instances for many common types that we expected we’d be using throughout our code:

​  ​ class ​ ​ Decode ​ a ​ where ​
​  decode :: ​ ByteString ​ -> ​ Either ​ ​ String ​ a
​  ​ instance ​ ​ Encode ​ ​ ByteString ​ ​ where ​
​  encode = id
​  ​ instance ​ ​ Decode ​ ​ ByteString ​ ​ where ​
​  decode = ​ Right ​
​  ​ instance ​ ​ Encode ​ ​ Text ​ ​ where ​
​  encode = encodeUtf8
​  ​ instance ​ ​ Decode ​ ​ Text ​ ​ where ​
​  decode = ​ Right ​ . decodeUtf8
​  ​ instance ​ ​ Encode ​ ​ String ​ ​ where ​
​  encode = ​ BC ​.pack
​  ​ instance ​ ​ Decode ​ ​ String ​ ​ where ​
​  decode = ​ Right ​ . ​ BC ​.unpack
​  ​ instance ​ ​ Encode ​ ​ FileMode ​ ​ where ​
​  encode (​ CMode ​ fMode) = encode fMode
​  encodeWithSize (​ CMode ​ fMode) = encodeWithSize fMode
​  ​ instance ​ ​ Decode ​ ​ FileMode ​ ​ where ​
​  decode = fmap ​ CMode ​ . decode

We knew that we’d want to be able to encode Word32 values, but we decided to pack these directly into the ByteString , rather than printing them and storing the ASCII-encoded representations of the numbers. We wrote a couple of helper functions to help us write Encode and Decode instances for Word32 :

​  word32ToBytes :: ​ Word32 ​ -> (​ Word8 ​, ​ Word8 ​, ​ Word8 ​, ​ Word8 ​)
​  word32ToBytes word =
​  ​ let ​ a = fromIntegral $ 255 .&. word
​  b = fromIntegral $ 255 .&. shift word (-8)
​  c = fromIntegral $ 255 .&. shift word (-16)
​  d = fromIntegral $ 255 .&. shift word (-24)
​  ​ in ​ (a,b,c,d)
​  
​  word32FromBytes :: (​ Word8 ​, ​ Word8 ​, ​ Word8 ​, ​ Word8 ​) -> ​ Word32 ​
​  word32FromBytes (a,b,c,d) =
​  ​ let ​ a' = fromIntegral a
​  b' = shift (fromIntegral b) 8
​  c' = shift (fromIntegral c) 16
​  d' = shift (fromIntegral d) 24
​  ​ in ​ a' .|. b' .|. c' .|. d'
​  
​  word32ToByteString :: ​ Word32 ​ -> ​ ByteString ​
​  word32ToByteString word =
​  ​ let ​ (a,b,c,d) = word32ToBytes word
​  ​ in ​ ​ BS ​.pack [a,b,c,d]
​  
​  bytestringToWord32 :: ​ ByteString ​ -> ​ Either ​ ​ String ​ ​ Word32 ​
​  bytestringToWord32 bytestring =
​  ​ case ​ ​ BS ​.unpack bytestring ​ of ​
​  [a,b,c,d] -> ​ Right ​ $ word32FromBytes (a,b,c,d)
​  ​ _ ​ ->
​  ​ let ​ l = show $ ​ BS ​.length bytestring
​  ​ in ​ ​ Left ​ (​ "Expecting 4 bytes but got " ​ <> l)
​  
​  ​ instance ​ ​ Encode ​ ​ Word32 ​ ​ where ​
​  encode = word32ToByteString
​  encodeWithSize w =
​  ​ let ​ (a, b, c, d) = word32ToBytes w
​  ​ in ​ ​ BS ​.pack [4,0,0,0,a,b,c,d]
​  
​  ​ instance ​ ​ Decode ​ ​ Word32 ​ ​ where ​
​  decode = bytestringToWord32

With all of our Encode instances in place, we were able to also define an Encode instance for FileData :

​  ​ instance ​ ​ Encode ​ a => ​ Encode ​ (​ FileData ​ a) ​ where ​
​  encode ​ FileData ​{..} = encode $
​  encodeWithSize fileName
​  <> encodeWithSize fileSize
​  <> encodeWithSize filePermissions
​  <> encodeWithSize fileData

Finally, to support packing more than one file into the archive, we also added Encode instances for lists and tuples, and created a new FilePack type that contains a list of Packable values that can describe a heterogeneous collection of encodable file data:

​  ​ instance ​ (​ Encode ​ a, ​ Encode ​ b) => ​ Encode ​ (a,b) ​ where ​
​  encode (a,b) = encode $ encodeWithSize a <> encodeWithSize b
​  ​ instance ​ ​ {-# OVERLAPPABLE #-} ​ ​ Encode ​ a => ​ Encode ​ [a] ​ where ​
​  encode = encode . foldMap encodeWithSize
​  
​  ​ data ​ ​ Packable ​ = forall a. ​ Encode ​ a =>
​  ​ Packable ​ { getPackable :: ​ FileData ​ a }
​  
​  ​ instance ​ ​ Encode ​ ​ Packable ​ ​ where ​
​  encode (​ Packable ​ p) = encode p
​  
​  ​ newtype ​ ​ FilePack ​ = ​ FilePack ​ [​ Packable ​]
​  
​  ​ instance ​ ​ Encode ​ ​ FilePack ​ ​ where ​
​  encode (​ FilePack ​ p) = encode p