-- | Parse and write bits easily. Parsing can be done either in a monadic
-- style, or more efficiently, using the 'Applicative' style. Writing is
-- monadic style only. See "Data.Binary.Bits.Get" and "Data.Binary.Bits.Put",
-- respectively.
module Data.Binary.Bits ( BinaryBit(putBits, getBits) ) where

import qualified Data.Binary.Bits.Get as Get
import qualified Data.Binary.Bits.Put as Put
import qualified Data.Word as Word

class BinaryBit a where
  putBits :: Int -> a -> Put.BitPut ()
  getBits :: Int -> Get.BitGet a

instance BinaryBit Bool where
  putBits :: Int -> Bool -> BitPut ()
putBits = (Bool -> BitPut ()) -> Int -> Bool -> BitPut ()
forall a b. a -> b -> a
const Bool -> BitPut ()
Put.putBool
  getBits :: Int -> BitGet Bool
getBits = BitGet Bool -> Int -> BitGet Bool
forall a b. a -> b -> a
const BitGet Bool
Get.getBool

instance BinaryBit Word.Word8 where
  putBits :: Int -> Word8 -> BitPut ()
putBits = Int -> Word8 -> BitPut ()
Put.putWord8
  getBits :: Int -> BitGet Word8
getBits = Int -> BitGet Word8
Get.getWord8

instance BinaryBit Word.Word16 where
  putBits :: Int -> Word16 -> BitPut ()
putBits = Int -> Word16 -> BitPut ()
Put.putWord16be
  getBits :: Int -> BitGet Word16
getBits = Int -> BitGet Word16
Get.getWord16be

instance BinaryBit Word.Word32 where
  putBits :: Int -> Word32 -> BitPut ()
putBits = Int -> Word32 -> BitPut ()
Put.putWord32be
  getBits :: Int -> BitGet Word32
getBits = Int -> BitGet Word32
Get.getWord32be

instance BinaryBit Word.Word64 where
  putBits :: Int -> Word64 -> BitPut ()
putBits = Int -> Word64 -> BitPut ()
Put.putWord64be
  getBits :: Int -> BitGet Word64
getBits = Int -> BitGet Word64
Get.getWord64be