Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Cooked.Pretty
Description
This module exposes off-chain pretty-printing functions for transaction skeletons, utxo states, addresses, pubkey hashes, values, etc.
We provide the PrettyCooked
class and instances for common Plutus types.
We don't rely on Pretty
from Prettyprinter in order to define better
printers for Plutus types which already have instances of Pretty
. Also,
PrettyCooked
makes it possible to optionally modify pretty printing
settings PrettyCookedOpts
(e.g. length of printed hashes).
Requirements on datum and redeemers
Datums and redeemers are required to have a PrettyCooked
instance.
For trivial datatypes, you can rely on Show by using viaShow
from Prettyprinter: prettyCooked = Prettyprinter.viaShow
.
For more complex datatypes, you can rely on existing PrettyCooked
instances. Prefer implementing the prettyCookedOpt
function and relay the
PrettyCookedOpts
settings to other printers.
@ data Foo = Bar Api.Value | Baz Api.PubkeyHash Api.Value
instance PrettyCooked Foo where prettyCookedOpt pcOpts (Bar value) = Bar + prettyCookedOpt pcOpts value prettyCookedOpt pcOpts (Baz pkh value) = prettyItemize Baz "-" [ "user:" + prettyCookedOpt pcOpts pkh, "deposit:" + prettyCookedOpt pcOpts value ] @
The prettyItemize
function is useful to nicely lay down nested lists of
elements. Since we manipulate regular Doc
values, any
function from Prettyprinter can be used to implement your printers.
How to pretty print?
Pretty printing of transaction skeletons and UTxO states is done automatically by the end-user functions provided in Cooked.MockChain.Testing.
To do it manually, use instances of PrettyCooked
, PrettyCookedList
or
PrettyCookedMaybe
defined in Skeleton
or
MockChain
such as the one for MockChainReturn a UtxoState
.
Synopsis
- type DocCooked = Doc ()
- class PrettyCooked a where
- prettyCookedOpt :: PrettyCookedOpts -> a -> DocCooked
- prettyCooked :: a -> DocCooked
- class PrettyCookedList a where
- prettyCookedOptList :: PrettyCookedOpts -> a -> [DocCooked]
- prettyCookedOptListMaybe :: PrettyCookedOpts -> a -> [Maybe DocCooked]
- prettyCookedList :: a -> [DocCooked]
- class PrettyCookedMaybe a where
- prettyCookedOptMaybe :: PrettyCookedOpts -> a -> Maybe DocCooked
- prettyCookedMaybe :: a -> Maybe DocCooked
- class ToHash a where
- toHash :: a -> BuiltinByteString
- data PrettyCookedOpts = PrettyCookedOpts {}
- data PrettyCookedHashOpts = PrettyCookedHashOpts {}
- data PCOptTxOutRefs
- renderString :: (a -> DocCooked) -> a -> String
- printCookedOpt :: PrettyCooked a => PrettyCookedOpts -> a -> IO ()
- printCooked :: PrettyCooked a => a -> IO ()
- prettyHash :: ToHash a => PrettyCookedOpts -> a -> DocCooked
- prettyItemize :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> DocCooked -> a -> DocCooked
- prettyItemizeNoTitle :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> a -> DocCooked
- prettyItemizeNonEmpty :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> DocCooked -> a -> Maybe DocCooked
- hashNamesFromList :: ToHash a => [(a, String)] -> Map BuiltinByteString String
- defaultHashNames :: Map BuiltinByteString String
- addHashNames :: Map BuiltinByteString String -> PrettyCookedOpts -> PrettyCookedOpts
Documentation
class PrettyCooked a where Source #
Type class of things that can be pretty printed as a single document. You
need to implement either prettyCookedOpt
or prettyCooked
manually,
otherwise calling either of them will resulting in a infinite loop.
Minimal complete definition
Nothing
Methods
prettyCookedOpt :: PrettyCookedOpts -> a -> DocCooked Source #
Pretty prints an element based on some PrettyCookedOpts
prettyCooked :: a -> DocCooked Source #
Pretty prints an element directly
Instances
class PrettyCookedList a where Source #
Type class of things that can be pretty printed as a list of
documents. Similarly to PrettyCooked
, at least of the functions from this
class needs to be manually implemented to avoid infinite loops.
Minimal complete definition
Nothing
Methods
prettyCookedOptList :: PrettyCookedOpts -> a -> [DocCooked] Source #
Pretty prints an element as a list on some PrettyCookedOpts
prettyCookedOptListMaybe :: PrettyCookedOpts -> a -> [Maybe DocCooked] Source #
Pretty prints an element as a list of optional documents
prettyCookedList :: a -> [DocCooked] Source #
Pretty prints an elements as a list
Instances
class PrettyCookedMaybe a where Source #
Type class of things that can be optionally pretty printed as a document
Minimal complete definition
Nothing
Methods
prettyCookedOptMaybe :: PrettyCookedOpts -> a -> Maybe DocCooked Source #
Pretty prints an optional document on some PrettyCookedOpts
prettyCookedMaybe :: a -> Maybe DocCooked Source #
Pretty prints an option document
Instances
PrettyCookedMaybe TxSkelOutDatum Source # | Prints a |
Defined in Cooked.Pretty.Skeleton Methods prettyCookedOptMaybe :: PrettyCookedOpts -> TxSkelOutDatum -> Maybe DocCooked Source # prettyCookedMaybe :: TxSkelOutDatum -> Maybe DocCooked Source # | |
PrettyCookedMaybe (Maybe DocCooked) Source # | |
Defined in Cooked.Pretty.Class Methods prettyCookedOptMaybe :: PrettyCookedOpts -> Maybe DocCooked -> Maybe DocCooked Source # prettyCookedMaybe :: Maybe DocCooked -> Maybe DocCooked Source # |
Hashable elements can be transformed to BuiltinByteString
Methods
toHash :: a -> BuiltinByteString Source #
Instances
data PrettyCookedOpts Source #
A set of option to pilot pretty printing in cooked-validators
Constructors
PrettyCookedOpts | |
Fields
|
Instances
Show PrettyCookedOpts Source # | |
Defined in Cooked.Pretty.Options Methods showsPrec :: Int -> PrettyCookedOpts -> ShowS # show :: PrettyCookedOpts -> String # showList :: [PrettyCookedOpts] -> ShowS # | |
Default PrettyCookedOpts Source # | |
Defined in Cooked.Pretty.Options Methods def :: PrettyCookedOpts # | |
Eq PrettyCookedOpts Source # | |
Defined in Cooked.Pretty.Options Methods (==) :: PrettyCookedOpts -> PrettyCookedOpts -> Bool # (/=) :: PrettyCookedOpts -> PrettyCookedOpts -> Bool # |
data PrettyCookedHashOpts Source #
A set of options to pilot how hashes are pretty printed
Constructors
PrettyCookedHashOpts | |
Fields
|
Instances
Show PrettyCookedHashOpts Source # | |
Defined in Cooked.Pretty.Options Methods showsPrec :: Int -> PrettyCookedHashOpts -> ShowS # show :: PrettyCookedHashOpts -> String # showList :: [PrettyCookedHashOpts] -> ShowS # | |
Default PrettyCookedHashOpts Source # | |
Defined in Cooked.Pretty.Options Methods | |
Eq PrettyCookedHashOpts Source # | |
Defined in Cooked.Pretty.Options Methods (==) :: PrettyCookedHashOpts -> PrettyCookedHashOpts -> Bool # (/=) :: PrettyCookedHashOpts -> PrettyCookedHashOpts -> Bool # |
data PCOptTxOutRefs Source #
Whether to print transaction outputs references.
Constructors
PCOptTxOutRefsHidden | Hide them |
PCOptTxOutRefsFull | Always show them. Warning: this will disable printing similar UTxOs as a group (for
instance |
PCOptTxOutRefsPartial | Show them for UTxOs which are not grouped with similar others. This
avoids the downside of |
Instances
Show PCOptTxOutRefs Source # | |
Defined in Cooked.Pretty.Options Methods showsPrec :: Int -> PCOptTxOutRefs -> ShowS # show :: PCOptTxOutRefs -> String # showList :: [PCOptTxOutRefs] -> ShowS # | |
Eq PCOptTxOutRefs Source # | |
Defined in Cooked.Pretty.Options Methods (==) :: PCOptTxOutRefs -> PCOptTxOutRefs -> Bool # (/=) :: PCOptTxOutRefs -> PCOptTxOutRefs -> Bool # |
renderString :: (a -> DocCooked) -> a -> String Source #
Use this to convert a pretty-printer to a regular show function using default layout options. This is used in Testing because Tasty uses strings.
printCookedOpt :: PrettyCooked a => PrettyCookedOpts -> a -> IO () Source #
Use this in the REPL as an alternative to the default print
function when
dealing with pretty-printable cooked values.
For example, printCookedOpt def runMockChain i0 foo
printCooked :: PrettyCooked a => a -> IO () Source #
Version of printCookedOpt
that uses default pretty printing options.
prettyHash :: ToHash a => PrettyCookedOpts -> a -> DocCooked Source #
Pretty prints hashable elements based on pcOptHashes
in the
PrettyCookedOpts
. This cannot be made an instance as it would be
undecidable (the hope was (ToHash a) => PrettyCooked a
)
prettyItemize :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> DocCooked -> a -> DocCooked Source #
Print an item list with a title
>>>
prettyCookedOpts opts "Foo" "-" ["bar1", "bar2", "bar3"]
Foo - bar1 - bar2 - bar3
prettyItemizeNoTitle :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> a -> DocCooked Source #
Print an item list without a title
prettyItemizeNonEmpty :: PrettyCookedList a => PrettyCookedOpts -> DocCooked -> DocCooked -> a -> Maybe DocCooked Source #
Print an item list with a title, but only when the list is non-empty
hashNamesFromList :: ToHash a => [(a, String)] -> Map BuiltinByteString String Source #
Smart constructor for maps to be used in the "pcOptHashNames" pretty-printing option.
defaultHashNames :: Map BuiltinByteString String Source #
Default hash to names map that assigns Lovelace, Quick, and Permanent to the associated currency symbols. This is used as the default for the pretty-printing option and is recommended to use as a basis to extend with custom names.
addHashNames :: Map BuiltinByteString String -> PrettyCookedOpts -> PrettyCookedOpts Source #
Adds some additional names to these pretty cooked options. This has two practical use cases:
- Users can use it in conjuction to
hashNamesFromList
without having to remember to manually invokedefaultHashNames
- We use it internally to account for names that have been registered during
mockchain runs, such as for names that depend on on-chain data, typically a
TxOutRef
.