{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Flat.AsBin(AsBin,unbin) where
import qualified Data.ByteString as B
import Flat.Bits (bits, fromBools, toBools)
import Flat.Class (Flat (..))
import Flat.Decoder.Prim (binOf)
import Flat.Decoder.Types (Get (Get, runGet),
GetResult (GetResult),
S (S, currPtr, usedBits))
import Flat.Run (unflatRawWithOffset)
import Foreign (plusPtr)
import Text.PrettyPrint.HughesPJClass (Doc, Pretty (pPrint),
prettyShow, text)
data AsBin a = AsBin {
forall a. AsBin a -> ByteString
repr :: B.ByteString
,forall a. AsBin a -> Int
offsetBits :: Int
} deriving Int -> AsBin a -> ShowS
forall a. Int -> AsBin a -> ShowS
forall a. [AsBin a] -> ShowS
forall a. AsBin a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AsBin a] -> ShowS
$cshowList :: forall a. [AsBin a] -> ShowS
show :: AsBin a -> String
$cshow :: forall a. AsBin a -> String
showsPrec :: Int -> AsBin a -> ShowS
$cshowsPrec :: forall a. Int -> AsBin a -> ShowS
Show
instance Flat a => Pretty (AsBin a) where
pPrint :: AsBin a -> Doc
pPrint :: AsBin a -> Doc
pPrint AsBin a
r = let n :: a -> [a]
n = forall a. Int -> a -> [a]
replicate (forall a. AsBin a -> Int
offsetBits AsBin a
r) in String -> Doc
text forall a b. (a -> b) -> a -> b
$ forall {a}. a -> [a]
n Char
'_' forall a. [a] -> [a] -> [a]
++ (forall a. Int -> [a] -> [a]
drop (forall a. AsBin a -> Int
offsetBits AsBin a
r) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Pretty a => a -> String
prettyShow forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Bool] -> Bits
fromBools forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall {a}. a -> [a]
n Bool
False forall a. [a] -> [a] -> [a]
++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bits -> [Bool]
toBools forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Flat a => a -> Bits
bits forall a b. (a -> b) -> a -> b
$ forall a. Flat a => AsBin a -> a
unbin AsBin a
r)
unbin :: Flat a => AsBin a -> a
unbin :: forall a. Flat a => AsBin a -> a
unbin AsBin a
a =
case forall b a. AsByteString b => Get a -> b -> Int -> Decoded a
unflatRawWithOffset forall {a}. Flat a => Get a
dec (forall a. AsBin a -> ByteString
repr AsBin a
a) (forall a. AsBin a -> Int
offsetBits AsBin a
a) of
Right a
a -> a
a
Left DecodeException
e -> forall a. HasCallStack => String -> a
error (forall a. Show a => a -> String
show DecodeException
e)
where
dec :: Get a
dec = forall a. (Ptr Word8 -> S -> IO (GetResult a)) -> Get a
Get forall a b. (a -> b) -> a -> b
$ \Ptr Word8
end S
s -> do
GetResult S
s' a
a <- forall a. Get a -> Ptr Word8 -> S -> IO (GetResult a)
runGet forall {a}. Flat a => Get a
decode Ptr Word8
end S
s
let s'' :: S
s'' = Ptr Word8 -> Int -> S
S (S -> Ptr Word8
currPtr S
s' forall a b. Ptr a -> Int -> Ptr b
`plusPtr` if S -> Int
usedBits S
s' forall a. Eq a => a -> a -> Bool
== Int
0 then Int
0 else Int
1) Int
0
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. S -> a -> GetResult a
GetResult S
s'' a
a
instance Flat a => Flat (AsBin a) where
size :: AsBin a -> Int -> Int
size = forall a. HasCallStack => String -> a
error String
"unused"
encode :: AsBin a -> Encoding
encode = forall a. HasCallStack => String -> a
error String
"unused"
decode :: Flat a => Get (AsBin a)
decode :: Flat a => Get (AsBin a)
decode = forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry forall a. ByteString -> Int -> AsBin a
AsBin forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Get a -> Get (ByteString, Int)
binOf (forall {a}. Flat a => Get a
decode :: Get a)