{-# 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 Prelude hiding ((.))
import qualified Codec.CBOR.Write as CBOR.Write
import Control.Category ((.))
import qualified Data.ByteString as BS
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 -> BSL.ByteString
serialize :: forall a. ToCBOR a => a -> ByteString
serialize =
AllocationStrategy -> ByteString -> Builder -> ByteString
Builder.toLazyByteStringWith AllocationStrategy
strategy forall a. Monoid a => a
mempty 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 forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. ToCBOR a => a -> Encoding
toCBOR
where
strategy :: AllocationStrategy
strategy = Int -> Int -> AllocationStrategy
Builder.safeStrategy Int
1024 Int
4096
serialize' :: ToCBOR a => a -> BS.ByteString
serialize' :: forall a. ToCBOR a => a -> ByteString
serialize' = ByteString -> ByteString
BSL.toStrict forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. ToCBOR a => a -> ByteString
serialize
serializeBuilder :: ToCBOR a => a -> Builder
serializeBuilder :: forall a. ToCBOR a => a -> Builder
serializeBuilder = Encoding -> Builder
CBOR.Write.toBuilder forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. ToCBOR a => a -> Encoding
toCBOR
serializeEncoding :: Encoding -> BSL.ByteString
serializeEncoding :: Encoding -> ByteString
serializeEncoding = forall a. ToCBOR a => a -> ByteString
serialize
{-# DEPRECATED serializeEncoding "Use `serialize` instead, since `Encoding` has `ToCBOR` instance" #-}
serializeEncoding' :: Encoding -> BS.ByteString
serializeEncoding' :: Encoding -> ByteString
serializeEncoding' = forall a. ToCBOR a => a -> ByteString
serialize'
{-# DEPRECATED serializeEncoding' "Use `serialize'` instead, since `Encoding` has `ToCBOR` instance" #-}
encodeNestedCbor :: ToCBOR a => a -> Encoding
encodeNestedCbor :: forall a. ToCBOR a => a -> Encoding
encodeNestedCbor = ByteString -> Encoding
encodeNestedCborBytes forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. ToCBOR a => a -> ByteString
serialize
encodeNestedCborBytes :: BSL.ByteString -> Encoding
encodeNestedCborBytes :: ByteString -> Encoding
encodeNestedCborBytes ByteString
x = Word -> Encoding
encodeTag Word
24 forall a. Semigroup a => a -> a -> a
<> forall a. ToCBOR a => a -> Encoding
toCBOR ByteString
x
nestedCborSizeExpr :: Size -> Size
nestedCborSizeExpr :: Size -> Size
nestedCborSizeExpr Size
x = Size
2 forall a. Num a => a -> a -> a
+ Text -> (Natural -> Natural) -> Size -> Size
apMono Text
"withWordSize" forall s a. (Integral s, Integral a) => s -> a
withWordSize Size
x forall a. Num a => a -> a -> a
+ Size
x
nestedCborBytesSizeExpr :: Size -> Size
nestedCborBytesSizeExpr :: Size -> Size
nestedCborBytesSizeExpr Size
x = Size
2 forall a. Num a => a -> a -> a
+ Text -> (Natural -> Natural) -> Size -> Size
apMono Text
"withWordSize" forall s a. (Integral s, Integral a) => s -> a
withWordSize Size
x forall a. Num a => a -> a -> a
+ Size
x