module Haskus.Binary.Put
( Put
, PutM
, runPut
, runPutM
, putBuffer
, putByteString
, putPadding
, putPaddingAlign
, putWord8
, putWord16le
, putWord16be
, putWord32le
, putWord32be
, putWord64le
, putWord64be
)
where
import qualified Data.ByteString as BS
import qualified Data.Serialize.Put as BP
import Data.Serialize.Put (Put,PutM)
import Data.Bifunctor
import Haskus.Utils.Flow (replicateM_)
import Haskus.Binary.Buffer
import Haskus.Number.Word
runPut :: Put -> Buffer
runPut = Buffer . BP.runPut
runPutM :: PutM a -> (a,Buffer)
runPutM = second Buffer . BP.runPutM
putBuffer :: Buffer -> Put
putBuffer (Buffer bs) = BP.putByteString bs
putByteString :: BS.ByteString -> Put
putByteString = BP.putByteString
putPadding :: Word -> Put
putPadding n = replicateM_ (fromIntegral n) (BP.putWord8 0x00)
putPaddingAlign :: Word -> Word -> Put
putPaddingAlign n al = putPadding n'
where
n' = case n `mod` al of
0 -> 0
x -> al - fromIntegral x
putWord8 :: Word8 -> Put
putWord8 = BP.putWord8
putWord16le :: Word16 -> Put
putWord16le = BP.putWord16le
putWord16be :: Word16 -> Put
putWord16be = BP.putWord16be
putWord32le :: Word32 -> Put
putWord32le = BP.putWord32le
putWord32be :: Word32 -> Put
putWord32be = BP.putWord32be
putWord64le :: Word64 -> Put
putWord64le = BP.putWord64le
putWord64be :: Word64 -> Put
putWord64be = BP.putWord64be