{-# LANGUAGE CPP #-}
module Codec.CBOR.Encoding
(
Encoding(..)
, Tokens(..)
, encodeWord
, encodeWord8
, encodeWord16
, encodeWord32
, encodeWord64
, encodeInt
, encodeInt8
, encodeInt16
, encodeInt32
, encodeInt64
, encodeInteger
, encodeBytes
, encodeBytesIndef
, encodeByteArray
, encodeString
, encodeStringIndef
, encodeUtf8ByteArray
, encodeListLen
, encodeListLenIndef
, encodeMapLen
, encodeMapLenIndef
, encodeBreak
, encodeTag
, encodeTag64
, encodeBool
, encodeUndef
, encodeNull
, encodeSimple
, encodeFloat16
, encodeFloat
, encodeDouble
, encodePreEncoded
) where
#include "cbor.h"
import Data.Int
import Data.Word
import Data.Semigroup
import qualified Data.ByteString as B
import qualified Data.Text as T
import Codec.CBOR.ByteArray.Sliced (SlicedByteArray)
import Prelude hiding (encodeFloat)
import {-# SOURCE #-} qualified Codec.CBOR.FlatTerm as FlatTerm
newtype Encoding = Encoding (Tokens -> Tokens)
instance Show Encoding where
show :: Encoding -> String
show = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. Encoding -> FlatTerm
FlatTerm.toFlatTerm
data Tokens =
TkWord {-# UNPACK #-} !Word Tokens
| TkWord64 {-# UNPACK #-} !Word64 Tokens
| TkInt {-# UNPACK #-} !Int Tokens
| TkInt64 {-# UNPACK #-} !Int64 Tokens
| TkBytes {-# UNPACK #-} !B.ByteString Tokens
| TkBytesBegin Tokens
| TkByteArray {-# UNPACK #-} !SlicedByteArray Tokens
| TkString {-# UNPACK #-} !T.Text Tokens
| TkUtf8ByteArray {-# UNPACK #-} !SlicedByteArray Tokens
| TkStringBegin Tokens
| TkListLen {-# UNPACK #-} !Word Tokens
| TkListBegin Tokens
| TkMapLen {-# UNPACK #-} !Word Tokens
| TkMapBegin Tokens
| TkTag {-# UNPACK #-} !Word Tokens
| TkTag64 {-# UNPACK #-} !Word64 Tokens
| TkInteger !Integer Tokens
| TkNull Tokens
| TkUndef Tokens
| TkBool !Bool Tokens
| TkSimple {-# UNPACK #-} !Word8 Tokens
| TkFloat16 {-# UNPACK #-} !Float Tokens
| TkFloat32 {-# UNPACK #-} !Float Tokens
| TkFloat64 {-# UNPACK #-} !Double Tokens
| TkBreak Tokens
| TkEncoded {-# UNPACK #-} !B.ByteString Tokens
| TkEnd
deriving (Int -> Tokens -> ShowS
[Tokens] -> ShowS
Tokens -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Tokens] -> ShowS
$cshowList :: [Tokens] -> ShowS
show :: Tokens -> String
$cshow :: Tokens -> String
showsPrec :: Int -> Tokens -> ShowS
$cshowsPrec :: Int -> Tokens -> ShowS
Show,Tokens -> Tokens -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Tokens -> Tokens -> Bool
$c/= :: Tokens -> Tokens -> Bool
== :: Tokens -> Tokens -> Bool
$c== :: Tokens -> Tokens -> Bool
Eq)
instance Semigroup Encoding where
Encoding Tokens -> Tokens
b1 <> :: Encoding -> Encoding -> Encoding
<> Encoding Tokens -> Tokens
b2 = (Tokens -> Tokens) -> Encoding
Encoding (\Tokens
ts -> Tokens -> Tokens
b1 (Tokens -> Tokens
b2 Tokens
ts))
{-# INLINE (<>) #-}
instance Monoid Encoding where
mempty :: Encoding
mempty = (Tokens -> Tokens) -> Encoding
Encoding (\Tokens
ts -> Tokens
ts)
{-# INLINE mempty #-}
mappend :: Encoding -> Encoding -> Encoding
mappend = forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mappend #-}
mconcat :: [Encoding] -> Encoding
mconcat = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Semigroup a => a -> a -> a
(<>) forall a. Monoid a => a
mempty
{-# INLINE mconcat #-}
encodeWord :: Word -> Encoding
encodeWord :: Word -> Encoding
encodeWord = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord
encodeWord8 :: Word8 -> Encoding
encodeWord8 :: Word8 -> Encoding
encodeWord8 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord16 :: Word16 -> Encoding
encodeWord16 :: Word16 -> Encoding
encodeWord16 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord32 :: Word32 -> Encoding
encodeWord32 :: Word32 -> Encoding
encodeWord32 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord64 :: Word64 -> Encoding
encodeWord64 :: Word64 -> Encoding
encodeWord64 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Tokens -> Tokens
TkWord64
encodeInt :: Int -> Encoding
encodeInt :: Int -> Encoding
encodeInt = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt
encodeInt8 :: Int8 -> Encoding
encodeInt8 :: Int8 -> Encoding
encodeInt8 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt16 :: Int16 -> Encoding
encodeInt16 :: Int16 -> Encoding
encodeInt16 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt32 :: Int32 -> Encoding
encodeInt32 :: Int32 -> Encoding
encodeInt32 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt64 :: Int64 -> Encoding
encodeInt64 :: Int64 -> Encoding
encodeInt64 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Tokens -> Tokens
TkInt64
encodeInteger :: Integer -> Encoding
encodeInteger :: Integer -> Encoding
encodeInteger Integer
n = (Tokens -> Tokens) -> Encoding
Encoding (Integer -> Tokens -> Tokens
TkInteger Integer
n)
encodeBytes :: B.ByteString -> Encoding
encodeBytes :: ByteString -> Encoding
encodeBytes = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Tokens -> Tokens
TkBytes
encodeByteArray :: SlicedByteArray -> Encoding
encodeByteArray :: SlicedByteArray -> Encoding
encodeByteArray = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. SlicedByteArray -> Tokens -> Tokens
TkByteArray
encodeBytesIndef :: Encoding
encodeBytesIndef :: Encoding
encodeBytesIndef = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkBytesBegin
encodeString :: T.Text -> Encoding
encodeString :: Text -> Encoding
encodeString = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Tokens -> Tokens
TkString
encodeStringIndef :: Encoding
encodeStringIndef :: Encoding
encodeStringIndef = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkStringBegin
encodeUtf8ByteArray :: SlicedByteArray -> Encoding
encodeUtf8ByteArray :: SlicedByteArray -> Encoding
encodeUtf8ByteArray = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. SlicedByteArray -> Tokens -> Tokens
TkUtf8ByteArray
encodeListLen :: Word -> Encoding
encodeListLen :: Word -> Encoding
encodeListLen = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkListLen
encodeListLenIndef :: Encoding
encodeListLenIndef :: Encoding
encodeListLenIndef = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkListBegin
encodeMapLen :: Word -> Encoding
encodeMapLen :: Word -> Encoding
encodeMapLen = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkMapLen
encodeMapLenIndef :: Encoding
encodeMapLenIndef :: Encoding
encodeMapLenIndef = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkMapBegin
encodeBreak :: Encoding
encodeBreak :: Encoding
encodeBreak = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkBreak
encodeTag :: Word -> Encoding
encodeTag :: Word -> Encoding
encodeTag = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkTag
encodeTag64 :: Word64 -> Encoding
encodeTag64 :: Word64 -> Encoding
encodeTag64 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Tokens -> Tokens
TkTag64
encodeBool :: Bool -> Encoding
encodeBool :: Bool -> Encoding
encodeBool Bool
b = (Tokens -> Tokens) -> Encoding
Encoding (Bool -> Tokens -> Tokens
TkBool Bool
b)
encodeUndef :: Encoding
encodeUndef :: Encoding
encodeUndef = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkUndef
encodeNull :: Encoding
encodeNull :: Encoding
encodeNull = (Tokens -> Tokens) -> Encoding
Encoding Tokens -> Tokens
TkNull
encodeSimple :: Word8 -> Encoding
encodeSimple :: Word8 -> Encoding
encodeSimple = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Tokens -> Tokens
TkSimple
encodeFloat16 :: Float -> Encoding
encodeFloat16 :: Float -> Encoding
encodeFloat16 = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Tokens -> Tokens
TkFloat16
encodeFloat :: Float -> Encoding
encodeFloat :: Float -> Encoding
encodeFloat = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Tokens -> Tokens
TkFloat32
encodeDouble :: Double -> Encoding
encodeDouble :: Double -> Encoding
encodeDouble = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Tokens -> Tokens
TkFloat64
encodePreEncoded :: B.ByteString -> Encoding
encodePreEncoded :: ByteString -> Encoding
encodePreEncoded = (Tokens -> Tokens) -> Encoding
Encoding forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Tokens -> Tokens
TkEncoded