1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
{-# LANGUAGE CPP #-}
{-# LANGUAGE RecordWildCards #-}
module Case where
#if __GLASGOW_HASKELL__ < 709
import Control.Applicative ((<$>))
#endif
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as B16
import JSON
import Network.HTTP2.Frame
data CaseSource = CaseSource
{ cs_description :: String
, cs_encodeinfo :: EncodeInfo
, cs_payload :: FramePayload
}
deriving (Show, Read)
data CaseWire = CaseWire
{ wire_description :: String
, wire_hex :: ByteString
, wire_padding :: Maybe Pad
, wire_error :: Maybe [ErrorCode]
}
deriving (Show, Read)
sourceToWire :: CaseSource -> CaseWire
sourceToWire CaseSource{..} =
CaseWire
{ wire_description = cs_description
, wire_hex = wire
, wire_padding = Pad <$> encodePadding cs_encodeinfo
, wire_error = Nothing
}
where
frame = encodeFrame cs_encodeinfo cs_payload
wire = B16.encode frame
wireToCase :: CaseWire -> Case
wireToCase CaseWire{wire_error = Nothing, ..} =
Case
{ description = wire_description
, wire = wire_hex
, frame = Just $ FramePad frm wire_padding
, err = Nothing
}
where
-- fromJust is unsafe
frm = case decodeFrame $ B16.decodeLenient wire_hex of
Left e -> error $ show e
Right r -> r
wireToCase CaseWire{wire_error = Just e, ..} =
Case
{ description = wire_description
, wire = wire_hex
, frame = Nothing
, err = Just e
}
|