{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}

module Data.Bytes.Builder.Class
  ( ToBuilder(..)
  ) where

import Data.Bytes (Bytes)
import Data.Bytes.Builder (Builder)
import Data.ByteString.Short (ShortByteString)
import Data.Int
import Data.Primitive.ByteArray (ByteArray)
import Data.Text.Short (ShortText)
import Data.Word

import qualified Data.Bytes.Builder as Builder

-- | Types that can be encoded as a builder. Human-readable encodings
-- are used when possible. For example, numbers are encoded an ascii-encoded
-- decimal characters. UTF-8 is preferred for textual types. For types
-- that represent arbitrary bytes (e.g. Bytes, ByteString), the bytes
-- are preserved.
--
-- The goal of this typeclass is to reduce the size of builders produced
-- by quasiquotation.
class ToBuilder a where
  toBuilder :: a -> Builder

-- | Identity
instance ToBuilder Builder where
  toBuilder :: Builder -> Builder
toBuilder = Builder -> Builder
forall a. a -> a
id

-- | Uses @bytes@.
instance ToBuilder Bytes where
  toBuilder :: Bytes -> Builder
toBuilder = Bytes -> Builder
Builder.bytes

-- | Uses @byteArray@
instance ToBuilder ByteArray where
  toBuilder :: ByteArray -> Builder
toBuilder = ByteArray -> Builder
Builder.byteArray

-- | Uses @shortByteString@
instance ToBuilder ShortByteString where
  toBuilder :: ShortByteString -> Builder
toBuilder = ShortByteString -> Builder
Builder.shortByteString

-- | Uses @shortTextUtf8@.
instance ToBuilder ShortText where
  toBuilder :: ShortText -> Builder
toBuilder = ShortText -> Builder
Builder.shortTextUtf8

-- | Uses @stringUtf8@
instance ToBuilder String where
  toBuilder :: String -> Builder
toBuilder = String -> Builder
Builder.stringUtf8

-- | Uses @int64Dec@.
instance ToBuilder Int64 where
  toBuilder :: Int64 -> Builder
toBuilder = Int64 -> Builder
Builder.int64Dec

-- | Uses @int32Dec@.
instance ToBuilder Int32 where
  toBuilder :: Int32 -> Builder
toBuilder = Int32 -> Builder
Builder.int32Dec

-- | Uses @int16Dec@.
instance ToBuilder Int16 where
  toBuilder :: Int16 -> Builder
toBuilder = Int16 -> Builder
Builder.int16Dec

-- | Uses @int8Dec@.
instance ToBuilder Int8 where
  toBuilder :: Int8 -> Builder
toBuilder = Int8 -> Builder
Builder.int8Dec

-- | Uses @intDec@.
instance ToBuilder Int where
  toBuilder :: Int -> Builder
toBuilder = Int -> Builder
Builder.intDec

-- | Uses @word64Dec@.
instance ToBuilder Word64 where
  toBuilder :: Word64 -> Builder
toBuilder = Word64 -> Builder
Builder.word64Dec

-- | Uses @word32Dec@.
instance ToBuilder Word32 where
  toBuilder :: Word32 -> Builder
toBuilder = Word32 -> Builder
Builder.word32Dec

-- | Uses @word16Dec@.
instance ToBuilder Word16 where
  toBuilder :: Word16 -> Builder
toBuilder = Word16 -> Builder
Builder.word16Dec

-- | Uses @word8Dec@.
instance ToBuilder Word8 where
  toBuilder :: Word8 -> Builder
toBuilder = Word8 -> Builder
Builder.word8Dec

-- | Uses @wordDec@.
instance ToBuilder Word where
  toBuilder :: Word -> Builder
toBuilder = Word -> Builder
Builder.wordDec

-- | uses @doubleDec@
instance ToBuilder Double where
  toBuilder :: Double -> Builder
toBuilder = Double -> Builder
Builder.doubleDec