-- | A 'Dropper s' is a 'Decoder s ()', that is a decoder that returns nothing
--
--   We use 'Dropper's when we don't care about the result of decoding, for
--   example when we have deprecated some part of the serialised blockchain, but
--   still need to decode old blocks.

module Cardano.Binary.Drop
  ( Dropper
  , dropBytes
  , dropInt32
  , dropList
  , dropMap
  , dropSet
  , dropTuple
  , dropTriple
  , dropWord8
  , dropWord64
  )
where

import Control.Monad (replicateM_)
import Data.Functor (void)
import qualified Codec.CBOR.Decoding as D


type Dropper s = D.Decoder s ()

dropBytes :: Dropper s
dropBytes :: forall s. Dropper s
dropBytes = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall s. Decoder s ByteString
D.decodeBytes

dropInt32 :: Dropper s
dropInt32 :: forall s. Dropper s
dropInt32 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall s. Decoder s Int32
D.decodeInt32

-- | Drop a list of values using the supplied `Dropper` for each element
dropList :: Dropper s -> Dropper s
dropList :: forall s. Dropper s -> Dropper s
dropList Dropper s
dropElems = do
  forall s. Dropper s
D.decodeListLenIndef
  forall r a r' s.
(r -> a -> r) -> r -> (r -> r') -> Decoder s a -> Decoder s r'
D.decodeSequenceLenIndef forall a b. a -> b -> a
const () forall a. a -> a
id Dropper s
dropElems

dropMap :: Dropper s -> Dropper s -> Dropper s
dropMap :: forall s. Dropper s -> Dropper s -> Dropper s
dropMap Dropper s
dropKey Dropper s
dropValue = do
  Int
n <- forall s. Decoder s Int
D.decodeMapLen
  forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n forall a b. (a -> b) -> a -> b
$ Dropper s
dropKey forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Dropper s
dropValue

dropSet :: Dropper s -> Dropper s
dropSet :: forall s. Dropper s -> Dropper s
dropSet Dropper s
dropElem = do
  forall (f :: * -> *) a. Functor f => f a -> f ()
void forall s. Decoder s Word
D.decodeTag
  Int
n <- forall s. Decoder s Int
D.decodeListLen
  forall (m :: * -> *) a. Applicative m => Int -> m a -> m ()
replicateM_ Int
n Dropper s
dropElem

dropTuple :: Dropper s -> Dropper s -> Dropper s
dropTuple :: forall s. Dropper s -> Dropper s -> Dropper s
dropTuple Dropper s
dropA Dropper s
dropB = do
  forall s. Int -> Decoder s ()
D.decodeListLenOf Int
2
  Dropper s
dropA
  Dropper s
dropB

dropTriple :: Dropper s -> Dropper s -> Dropper s -> Dropper s
dropTriple :: forall s. Dropper s -> Dropper s -> Dropper s -> Dropper s
dropTriple Dropper s
dropA Dropper s
dropB Dropper s
dropC = do
  forall s. Int -> Decoder s ()
D.decodeListLenOf Int
3
  Dropper s
dropA
  Dropper s
dropB
  Dropper s
dropC

dropWord8 :: Dropper s
dropWord8 :: forall s. Dropper s
dropWord8 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall s. Decoder s Word8
D.decodeWord8

dropWord64 :: Dropper s
dropWord64 :: forall s. Dropper s
dropWord64 = forall (f :: * -> *) a. Functor f => f a -> f ()
void forall s. Decoder s Word64
D.decodeWord64