{-# 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 = FlatTerm -> String
forall a. Show a => a -> String
show (FlatTerm -> String)
-> (Encoding -> FlatTerm) -> Encoding -> String
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
(Int -> Tokens -> ShowS)
-> (Tokens -> String) -> ([Tokens] -> ShowS) -> Show Tokens
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Tokens -> ShowS
showsPrec :: Int -> Tokens -> ShowS
$cshow :: Tokens -> String
show :: Tokens -> String
$cshowList :: [Tokens] -> ShowS
showList :: [Tokens] -> ShowS
Show,Tokens -> Tokens -> Bool
(Tokens -> Tokens -> Bool)
-> (Tokens -> Tokens -> Bool) -> Eq Tokens
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Tokens -> Tokens -> Bool
== :: Tokens -> Tokens -> Bool
$c/= :: Tokens -> Tokens -> Bool
/= :: 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 = Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE mappend #-}
mconcat :: [Encoding] -> Encoding
mconcat = (Encoding -> Encoding -> Encoding)
-> Encoding -> [Encoding] -> Encoding
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
(<>) Encoding
forall a. Monoid a => a
mempty
{-# INLINE mconcat #-}
encodeWord :: Word -> Encoding
encodeWord :: Word -> Encoding
encodeWord = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Word -> Tokens -> Tokens) -> Word -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word8 -> Tokens -> Tokens) -> Word8 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord (Word -> Tokens -> Tokens)
-> (Word8 -> Word) -> Word8 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord16 :: Word16 -> Encoding
encodeWord16 :: Word16 -> Encoding
encodeWord16 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Word16 -> Tokens -> Tokens) -> Word16 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord (Word -> Tokens -> Tokens)
-> (Word16 -> Word) -> Word16 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord32 :: Word32 -> Encoding
encodeWord32 :: Word32 -> Encoding
encodeWord32 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Word32 -> Tokens -> Tokens) -> Word32 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word -> Tokens -> Tokens
TkWord (Word -> Tokens -> Tokens)
-> (Word32 -> Word) -> Word32 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeWord64 :: Word64 -> Encoding
encodeWord64 :: Word64 -> Encoding
encodeWord64 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Word64 -> Tokens -> Tokens) -> Word64 -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Int -> Tokens -> Tokens) -> Int -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Int8 -> Tokens -> Tokens) -> Int8 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt (Int -> Tokens -> Tokens)
-> (Int8 -> Int) -> Int8 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt16 :: Int16 -> Encoding
encodeInt16 :: Int16 -> Encoding
encodeInt16 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Int16 -> Tokens -> Tokens) -> Int16 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt (Int -> Tokens -> Tokens)
-> (Int16 -> Int) -> Int16 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt32 :: Int32 -> Encoding
encodeInt32 :: Int32 -> Encoding
encodeInt32 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Int32 -> Tokens -> Tokens) -> Int32 -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Tokens -> Tokens
TkInt (Int -> Tokens -> Tokens)
-> (Int32 -> Int) -> Int32 -> Tokens -> Tokens
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral
encodeInt64 :: Int64 -> Encoding
encodeInt64 :: Int64 -> Encoding
encodeInt64 = (Tokens -> Tokens) -> Encoding
Encoding ((Tokens -> Tokens) -> Encoding)
-> (Int64 -> Tokens -> Tokens) -> Int64 -> 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 ((Tokens -> Tokens) -> Encoding)
-> (ByteString -> Tokens -> Tokens) -> ByteString -> 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 ((Tokens -> Tokens) -> Encoding)
-> (SlicedByteArray -> Tokens -> Tokens)
-> SlicedByteArray
-> 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 ((Tokens -> Tokens) -> Encoding)
-> (Text -> Tokens -> Tokens) -> Text -> 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 ((Tokens -> Tokens) -> Encoding)
-> (SlicedByteArray -> Tokens -> Tokens)
-> SlicedByteArray
-> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word -> Tokens -> Tokens) -> Word -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word -> Tokens -> Tokens) -> Word -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word -> Tokens -> Tokens) -> Word -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word64 -> Tokens -> Tokens) -> Word64 -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Word8 -> Tokens -> Tokens) -> Word8 -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Float -> Tokens -> Tokens) -> Float -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Float -> Tokens -> Tokens) -> Float -> 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 ((Tokens -> Tokens) -> Encoding)
-> (Double -> Tokens -> Tokens) -> Double -> 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 ((Tokens -> Tokens) -> Encoding)
-> (ByteString -> Tokens -> Tokens) -> ByteString -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Tokens -> Tokens
TkEncoded