{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Cardano.Ledger.Api.DebugTools where

import Cardano.Ledger.Binary (
  Annotator,
  DecCBOR (..),
  DecShareCBOR,
  EncCBOR,
  Version,
  decNoShareCBOR,
  decodeFull',
  decodeFullAnnotator,
  decodeFullDecoder',
 )
import Cardano.Ledger.Binary.Encoding (serialize')
import Cardano.Ledger.Core (Era, eraProtVerLow)
import Control.Exception (Exception, throwIO)
import Control.Monad.IO.Class (MonadIO (..))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS

readCBORWith ::
  (MonadIO m, Exception e) => (Version -> BS.ByteString -> Either e a) -> Version -> FilePath -> m a
readCBORWith :: forall (m :: * -> *) e a.
(MonadIO m, Exception e) =>
(Version -> ByteString -> Either e a) -> Version -> FilePath -> m a
readCBORWith Version -> ByteString -> Either e a
dec Version
version FilePath
path = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
  ByteString
dat <- FilePath -> IO ByteString
BS.readFile FilePath
path
  case Version -> ByteString -> Either e a
dec Version
version ByteString
dat of
    Right a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
    Left e
err -> forall e a. Exception e => e -> IO a
throwIO e
err

readCBOR :: (DecCBOR a, MonadIO m) => Version -> FilePath -> m a
readCBOR :: forall a (m :: * -> *).
(DecCBOR a, MonadIO m) =>
Version -> FilePath -> m a
readCBOR = forall (m :: * -> *) e a.
(MonadIO m, Exception e) =>
(Version -> ByteString -> Either e a) -> Version -> FilePath -> m a
readCBORWith forall a.
DecCBOR a =>
Version -> ByteString -> Either DecoderError a
decodeFull'

readCBORNoShare :: (MonadIO m, DecShareCBOR a) => Version -> FilePath -> m a
readCBORNoShare :: forall (m :: * -> *) a.
(MonadIO m, DecShareCBOR a) =>
Version -> FilePath -> m a
readCBORNoShare = forall (m :: * -> *) e a.
(MonadIO m, Exception e) =>
(Version -> ByteString -> Either e a) -> Version -> FilePath -> m a
readCBORWith (\Version
v ByteString
bs -> forall a.
Version
-> Text
-> (forall s. Decoder s a)
-> ByteString
-> Either DecoderError a
decodeFullDecoder' Version
v Text
"DecodeNoShare" forall a s. DecShareCBOR a => Decoder s a
decNoShareCBOR ByteString
bs)

readCBORAnnotated :: (MonadIO m, DecCBOR (Annotator a)) => Version -> FilePath -> m a
readCBORAnnotated :: forall (m :: * -> *) a.
(MonadIO m, DecCBOR (Annotator a)) =>
Version -> FilePath -> m a
readCBORAnnotated = forall (m :: * -> *) e a.
(MonadIO m, Exception e) =>
(Version -> ByteString -> Either e a) -> Version -> FilePath -> m a
readCBORWith (\Version
v ByteString
bs -> forall a.
Version
-> Text
-> (forall s. Decoder s (Annotator a))
-> ByteString
-> Either DecoderError a
decodeFullAnnotator Version
v Text
"DecodeAnnotated" forall a s. DecCBOR a => Decoder s a
decCBOR (ByteString -> ByteString
LBS.fromStrict ByteString
bs))

writeCBOR :: (EncCBOR a, MonadIO m) => Version -> FilePath -> a -> m ()
writeCBOR :: forall a (m :: * -> *).
(EncCBOR a, MonadIO m) =>
Version -> FilePath -> a -> m ()
writeCBOR Version
version FilePath
path = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
BS.writeFile FilePath
path forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. EncCBOR a => Version -> a -> ByteString
serialize' Version
version

readEraCBOR :: forall t era m. (Era era, DecCBOR (t era), MonadIO m) => FilePath -> m (t era)
readEraCBOR :: forall (t :: * -> *) era (m :: * -> *).
(Era era, DecCBOR (t era), MonadIO m) =>
FilePath -> m (t era)
readEraCBOR = forall a (m :: * -> *).
(DecCBOR a, MonadIO m) =>
Version -> FilePath -> m a
readCBOR (forall era. Era era => Version
eraProtVerLow @era)

writeEraCBOR :: forall t era m. (Era era, EncCBOR (t era), MonadIO m) => FilePath -> t era -> m ()
writeEraCBOR :: forall (t :: * -> *) era (m :: * -> *).
(Era era, EncCBOR (t era), MonadIO m) =>
FilePath -> t era -> m ()
writeEraCBOR = forall a (m :: * -> *).
(EncCBOR a, MonadIO m) =>
Version -> FilePath -> a -> m ()
writeCBOR (forall era. Era era => Version
eraProtVerLow @era)