{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Network.HTTP.Client.Util
    ( readPositiveInt
    ) where

import Text.Read (readMaybe)
import Control.Monad (guard)

-- | Read a positive 'Int', accounting for overflow
readPositiveInt :: String -> Maybe Int
readPositiveInt :: String -> Maybe Int
readPositiveInt String
s = do
  Int
i <- String -> Maybe Int
forall a. Read a => String -> Maybe a
readMaybe String
s
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Maybe ()) -> Bool -> Maybe ()
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0
  Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i