module Jerimum.PostgreSQL.Types.Int32Array
    -- * CBOR codec
  ( int32ArrayEncoderV0
  , int32ArrayDecoderV0
  , encodeInt32Array
    -- * Text codec
  , parseInt32Array
  , formatInt32Array
    -- * Value constructors
  , fromInt32Array
  ) where

import qualified Codec.CBOR.Decoding               as D
import qualified Codec.CBOR.Encoding               as E
import qualified Data.Attoparsec.Text              as A
import           Data.Int
import qualified Data.Text                         as T
import           Data.Text.Lazy                    (toStrict)
import           Data.Text.Lazy.Builder
import           Data.Text.Lazy.Builder.Int
import           Jerimum.PostgreSQL.Types
import           Jerimum.PostgreSQL.Types.Encoding
import           Jerimum.PostgreSQL.Types.Numeric

v0 :: Version
v0 = 0

encodeInt32Array :: Maybe [Int32] -> E.Encoding
encodeInt32Array = encodeWithVersion v0 int32ArrayEncoderV0

int32ArrayParser :: A.Parser [Int32]
int32ArrayParser = array1Parser (A.signed A.decimal)

parseInt32Array :: T.Text -> Maybe [Int32]
parseInt32Array = runParser "parseInt32Array: parse error" int32ArrayParser

formatInt32Array :: [Int32] -> T.Text
formatInt32Array = toStrict . toLazyText . buildArray decimal

fromInt32Array :: Maybe [Int32] -> Value
fromInt32Array = mkValue (ArrayType 1 TInt32) encodeInt32Array

int32ArrayEncoderV0 :: [Int32] -> E.Encoding
int32ArrayEncoderV0 = arrayEncoderV0 int32EncoderV0

int32ArrayDecoderV0 :: D.Decoder s [Int32]
int32ArrayDecoderV0 = arrayDecoderV0 int32DecoderV0
