-- | doctest utilities
module Flat.Instances.Test (
    tst,
    tstBits,
    asList,
    flatBits,
    allBits,
    prettyShow,
    module Data.Word,
) where

import Data.Word
import Flat.Bits (
    asBytes,
    bits,
    paddedBits,
 )
import Flat.Class (Flat (..))
import Flat.Run (
    flat,
    unflat,
 )
import Flat.Types (NumBits)
import Text.PrettyPrint.HughesPJClass (prettyShow)

-- | Returns: result of flat/unflat test, encoding size in bits, byte encoding
tst :: (Eq a, Flat a) => a -> (Bool, NumBits, [Word8])
tst :: a -> (Bool, NumBits, [Word8])
tst a
v = (ByteString -> Decoded a
forall a b. (Flat a, AsByteString b) => b -> Decoded a
unflat (a -> ByteString
forall a. Flat a => a -> ByteString
flat a
v) Decoded a -> Decoded a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Decoded a
forall a b. b -> Either a b
Right a
v, a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
size a
v NumBits
0, a -> [Word8]
forall a. Flat a => a -> [Word8]
showBytes a
v)

-- | Returns: result of flat/unflat test, encoding size in bits, bits encoding
tstBits :: (Eq a, Flat a) => a -> (Bool, NumBits, String)
tstBits :: a -> (Bool, NumBits, String)
tstBits a
v = (ByteString -> Decoded a
forall a b. (Flat a, AsByteString b) => b -> Decoded a
unflat (a -> ByteString
forall a. Flat a => a -> ByteString
flat a
v) Decoded a -> Decoded a -> Bool
forall a. Eq a => a -> a -> Bool
== a -> Decoded a
forall a b. b -> Either a b
Right a
v, a -> NumBits -> NumBits
forall a. Flat a => a -> NumBits -> NumBits
Flat.Class.size a
v NumBits
0, a -> String
forall a. Flat a => a -> String
flatBits a
v)

-- | Test that container is serialised as a List
asList :: (Eq a1, Eq a2, Flat a1, Flat a2) => (a2 -> a1) -> a2 -> Bool
asList :: (a2 -> a1) -> a2 -> Bool
asList a2 -> a1
f a2
l = a1 -> (Bool, NumBits, [Word8])
forall a. (Eq a, Flat a) => a -> (Bool, NumBits, [Word8])
tst (a2 -> a1
f a2
l) (Bool, NumBits, [Word8]) -> (Bool, NumBits, [Word8]) -> Bool
forall a. Eq a => a -> a -> Bool
== a2 -> (Bool, NumBits, [Word8])
forall a. (Eq a, Flat a) => a -> (Bool, NumBits, [Word8])
tst a2
l

flatBits :: Flat a => a -> String
flatBits :: a -> String
flatBits = Bits -> String
forall a. Pretty a => a -> String
prettyShow (Bits -> String) -> (a -> Bits) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bits
forall a. Flat a => a -> Bits
bits

allBits :: Flat a => a -> String
allBits :: a -> String
allBits = Bits -> String
forall a. Pretty a => a -> String
prettyShow (Bits -> String) -> (a -> Bits) -> a -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bits
forall a. Flat a => a -> Bits
paddedBits

showBytes :: Flat a => a -> [Word8]
showBytes :: a -> [Word8]
showBytes = Bits -> [Word8]
asBytes (Bits -> [Word8]) -> (a -> Bits) -> a -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bits
forall a. Flat a => a -> Bits
bits