{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Cardano.Binary.Serialize
( serialize
, serialize'
, serializeBuilder
, serializeEncoding
, serializeEncoding'
, encodeNestedCbor
, encodeNestedCborBytes
, nestedCborSizeExpr
, nestedCborBytesSizeExpr
)
where
import Cardano.Prelude
import qualified Codec.CBOR.Write as CBOR.Write
import Data.ByteString.Builder (Builder)
import qualified Data.ByteString.Builder.Extra as Builder
import qualified Data.ByteString.Lazy as BSL
import Cardano.Binary.ToCBOR
(Encoding, Size, ToCBOR(..), apMono, encodeTag, withWordSize)
serialize :: ToCBOR a => a -> LByteString
serialize :: a -> LByteString
serialize = Encoding -> LByteString
serializeEncoding (Encoding -> LByteString) -> (a -> Encoding) -> a -> LByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR
serialize' :: ToCBOR a => a -> ByteString
serialize' :: a -> ByteString
serialize' = LByteString -> ByteString
BSL.toStrict (LByteString -> ByteString)
-> (a -> LByteString) -> a -> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> LByteString
forall a. ToCBOR a => a -> LByteString
serialize
serializeBuilder :: ToCBOR a => a -> Builder
serializeBuilder :: a -> Builder
serializeBuilder = Encoding -> Builder
CBOR.Write.toBuilder (Encoding -> Builder) -> (a -> Encoding) -> a -> Builder
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR
serializeEncoding :: Encoding -> LByteString
serializeEncoding :: Encoding -> LByteString
serializeEncoding =
AllocationStrategy -> LByteString -> Builder -> LByteString
Builder.toLazyByteStringWith AllocationStrategy
strategy LByteString
forall a. Monoid a => a
mempty (Builder -> LByteString)
-> (Encoding -> Builder) -> Encoding -> LByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Encoding -> Builder
CBOR.Write.toBuilder
where
strategy :: AllocationStrategy
strategy = Int -> Int -> AllocationStrategy
Builder.safeStrategy Int
1024 Int
4096
serializeEncoding' :: Encoding -> ByteString
serializeEncoding' :: Encoding -> ByteString
serializeEncoding' = LByteString -> ByteString
BSL.toStrict (LByteString -> ByteString)
-> (Encoding -> LByteString) -> Encoding -> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Encoding -> LByteString
serializeEncoding
encodeNestedCbor :: ToCBOR a => a -> Encoding
encodeNestedCbor :: a -> Encoding
encodeNestedCbor = LByteString -> Encoding
encodeNestedCborBytes (LByteString -> Encoding) -> (a -> LByteString) -> a -> Encoding
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> LByteString
forall a. ToCBOR a => a -> LByteString
serialize
encodeNestedCborBytes :: LByteString -> Encoding
encodeNestedCborBytes :: LByteString -> Encoding
encodeNestedCborBytes LByteString
x = Word -> Encoding
encodeTag Word
24 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> LByteString -> Encoding
forall a. ToCBOR a => a -> Encoding
toCBOR LByteString
x
nestedCborSizeExpr :: Size -> Size
nestedCborSizeExpr :: Size -> Size
nestedCborSizeExpr Size
x = Size
2 Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Text -> (Natural -> Natural) -> Size -> Size
apMono Text
"withWordSize" Natural -> Natural
forall s a. (Integral s, Integral a) => s -> a
withWordSize Size
x Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
x
nestedCborBytesSizeExpr :: Size -> Size
nestedCborBytesSizeExpr :: Size -> Size
nestedCborBytesSizeExpr Size
x = Size
2 Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Text -> (Natural -> Natural) -> Size -> Size
apMono Text
"withWordSize" Natural -> Natural
forall s a. (Integral s, Integral a) => s -> a
withWordSize Size
x Size -> Size -> Size
forall a. Num a => a -> a -> a
+ Size
x