{-# LANGUAGE PatternSynonyms #-}

-- | Internal module. Not part of the public API.
module Data.PackStream.Get
  ( -- * Primitive decoders
    getNull
  , getBoolean
  , getFloat
    -- * Integer decoders
  , getInt
  , getInt64
    -- * Container decoders
  , getString
  , getBytes
  , getList
  , getDictionary
  ) where

import           Compat.Binary
import           Compat.Prelude
import           Data.PackStream.Get.Internal
import           Data.PackStream.Integer

-- | Deserialize an integer into an 'Int'
--
-- This operation will fail if the encoded integer doesn't fit into the value range of the 'Int' type.
getInt :: Get Int
getInt :: Get Int
getInt = Get Int -> (Int -> Get Int) -> Maybe Int -> Get Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get Int
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"integer out of Int range") Int -> Get Int
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Int -> Get Int) -> Get (Maybe Int) -> Get Int
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< PSInteger -> Maybe Int
forall a. FromPSInteger a => PSInteger -> Maybe a
fromPSInteger (PSInteger -> Maybe Int) -> Get PSInteger -> Get (Maybe Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get PSInteger
forall t. Persist t => Get t
get

-- | Deserialize an integer into an 'Int64'
--
-- This operation will fail if the encoded integer doesn't fit into the value range of the 'Int64' type.
getInt64 :: Get Int64
getInt64 :: Get Int64
getInt64 = Get Int64 -> (Int64 -> Get Int64) -> Maybe Int64 -> Get Int64
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> Get Int64
forall a. String -> Get a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"integer out of Int64 range") Int64 -> Get Int64
forall a. a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe Int64 -> Get Int64) -> Get (Maybe Int64) -> Get Int64
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< PSInteger -> Maybe Int64
forall a. FromPSInteger a => PSInteger -> Maybe a
fromPSInteger (PSInteger -> Maybe Int64) -> Get PSInteger -> Get (Maybe Int64)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get PSInteger
forall t. Persist t => Get t
get