{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
-- |
-- Conversions from Bencoded @ByteString@s to Haskell values.
--
module Data.Bencode.Decode
  (
    -- * Quick start
    -- $quick

    -- * Parser
    Parser
  , decode
  , decodeMaybe

    -- * String parsers
  , string
  , stringEq
  , text
  , textEq

    -- * Integer parsers
  , integer
  , int
  , intEq
  , int64
  , int32
  , int16
  , int8
  , word
  , word64
  , word32
  , word16
  , word8

    -- * List parsers
  , list
  , index
  , elem
  , list'
  , Elems

    -- * Dictionary parsers
  , dict
  , field
  , field'
  , dict'
  , Fields

    -- * Miscellaneous
  , value
  , fail
  , mapMaybe
  , mapOrFail

    -- * Recipes #recipes#
    -- $recipes
  ) where

import Prelude hiding (elem, fail)
import Control.Applicative
import Control.Monad hiding (fail)
import Control.Monad.ST
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Control.Monad.Trans.State.Strict
import Data.Int
import Data.Word
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.Foldable as F
import qualified Data.IntSet as IS
import qualified Data.Map as M
import qualified Data.Primitive.Array as A
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified GHC.Exts as X

import Data.Bencode.Type (Value(..))
import qualified Data.Bencode.Util as Util
import qualified Data.Bencode.AST as AST

newtype ParseResult a = ParseResult { forall a. ParseResult a -> Either String a
unParseResult :: Either String a }
  deriving (forall a b. a -> ParseResult b -> ParseResult a
forall a b. (a -> b) -> ParseResult a -> ParseResult b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> ParseResult b -> ParseResult a
$c<$ :: forall a b. a -> ParseResult b -> ParseResult a
fmap :: forall a b. (a -> b) -> ParseResult a -> ParseResult b
$cfmap :: forall a b. (a -> b) -> ParseResult a -> ParseResult b
Functor, Functor ParseResult
forall a. a -> ParseResult a
forall a b. ParseResult a -> ParseResult b -> ParseResult a
forall a b. ParseResult a -> ParseResult b -> ParseResult b
forall a b. ParseResult (a -> b) -> ParseResult a -> ParseResult b
forall a b c.
(a -> b -> c) -> ParseResult a -> ParseResult b -> ParseResult c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. ParseResult a -> ParseResult b -> ParseResult a
$c<* :: forall a b. ParseResult a -> ParseResult b -> ParseResult a
*> :: forall a b. ParseResult a -> ParseResult b -> ParseResult b
$c*> :: forall a b. ParseResult a -> ParseResult b -> ParseResult b
liftA2 :: forall a b c.
(a -> b -> c) -> ParseResult a -> ParseResult b -> ParseResult c
$cliftA2 :: forall a b c.
(a -> b -> c) -> ParseResult a -> ParseResult b -> ParseResult c
<*> :: forall a b. ParseResult (a -> b) -> ParseResult a -> ParseResult b
$c<*> :: forall a b. ParseResult (a -> b) -> ParseResult a -> ParseResult b
pure :: forall a. a -> ParseResult a
$cpure :: forall a. a -> ParseResult a
Applicative, Applicative ParseResult
forall a. a -> ParseResult a
forall a b. ParseResult a -> ParseResult b -> ParseResult b
forall a b. ParseResult a -> (a -> ParseResult b) -> ParseResult b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> ParseResult a
$creturn :: forall a. a -> ParseResult a
>> :: forall a b. ParseResult a -> ParseResult b -> ParseResult b
$c>> :: forall a b. ParseResult a -> ParseResult b -> ParseResult b
>>= :: forall a b. ParseResult a -> (a -> ParseResult b) -> ParseResult b
$c>>= :: forall a b. ParseResult a -> (a -> ParseResult b) -> ParseResult b
Monad)

failResult :: String -> ParseResult a
failResult :: forall a. String -> ParseResult a
failResult = forall a. Either String a -> ParseResult a
ParseResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> Either a b
Left
{-# INLINE failResult #-}

instance Alternative ParseResult where
  empty :: forall a. ParseResult a
empty = forall a. String -> ParseResult a
failResult String
"Alternative.empty"
  ParseResult a
l <|> :: forall a. ParseResult a -> ParseResult a -> ParseResult a
<|> ParseResult a
r = forall a. Either String a -> ParseResult a
ParseResult forall a b. (a -> b) -> a -> b
$ forall a. ParseResult a -> Either String a
unParseResult ParseResult a
l forall a. Semigroup a => a -> a -> a
<> forall a. ParseResult a -> Either String a
unParseResult ParseResult a
r
  -- Discards left error, not ideal

instance MonadPlus ParseResult
-- Does not satisfy MonadPlus laws because of the failure String
-- But required for Alternative (StateT _ ParseResult)

-- | A parser from a Bencode value to a Haskell value.
newtype Parser a = Parser { forall a. Parser a -> ReaderT Value ParseResult a
runParser_ :: ReaderT AST.Value ParseResult a }
  deriving (forall a b. a -> Parser b -> Parser a
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Parser b -> Parser a
$c<$ :: forall a b. a -> Parser b -> Parser a
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
$cfmap :: forall a b. (a -> b) -> Parser a -> Parser b
Functor, Functor Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Parser a -> Parser b -> Parser a
$c<* :: forall a b. Parser a -> Parser b -> Parser a
*> :: forall a b. Parser a -> Parser b -> Parser b
$c*> :: forall a b. Parser a -> Parser b -> Parser b
liftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
$cliftA2 :: forall a b c. (a -> b -> c) -> Parser a -> Parser b -> Parser c
<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
$c<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
pure :: forall a. a -> Parser a
$cpure :: forall a. a -> Parser a
Applicative, Applicative Parser
forall a. Parser a
forall a. Parser a -> Parser [a]
forall a. Parser a -> Parser a -> Parser a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. Parser a -> Parser [a]
$cmany :: forall a. Parser a -> Parser [a]
some :: forall a. Parser a -> Parser [a]
$csome :: forall a. Parser a -> Parser [a]
<|> :: forall a. Parser a -> Parser a -> Parser a
$c<|> :: forall a. Parser a -> Parser a -> Parser a
empty :: forall a. Parser a
$cempty :: forall a. Parser a
Alternative, Applicative Parser
forall a. a -> Parser a
forall a b. Parser a -> Parser b -> Parser b
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Parser a
$creturn :: forall a. a -> Parser a
>> :: forall a b. Parser a -> Parser b -> Parser b
$c>> :: forall a b. Parser a -> Parser b -> Parser b
>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
$c>>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
Monad)

runParser :: Parser a -> AST.Value -> ParseResult a
runParser :: forall a. Parser a -> Value -> ParseResult a
runParser = forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> ReaderT Value ParseResult a
runParser_
{-# INLINE runParser #-}

liftP :: ParseResult a -> Parser a
liftP :: forall a. ParseResult a -> Parser a
liftP = forall a. ReaderT Value ParseResult a -> Parser a
Parser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
{-# INLINE liftP #-}

failParser :: String -> Parser a
failParser :: forall a. String -> Parser a
failParser = forall a. ParseResult a -> Parser a
liftP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. String -> ParseResult a
failResult
{-# INLINE failParser #-}

-- | Decode a value from the given @ByteString@. If decoding fails, returns
-- @Left@ with a failure message. The message is a short human-readable error
-- description and should not be relied on programmatically.
decode :: Parser a -> B.ByteString -> Either String a
decode :: forall a. Parser a -> ByteString -> Either String a
decode Parser a
p ByteString
s = ByteString -> Either String Value
AST.parseOnly ByteString
s forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. ParseResult a -> Either String a
unParseResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p

-- | Decode a value from the given @ByteString@. If decoding fails, returns
-- @Nothing@.
decodeMaybe :: Parser a -> B.ByteString -> Maybe a
decodeMaybe :: forall a. Parser a -> ByteString -> Maybe a
decodeMaybe Parser a
p = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Parser a -> ByteString -> Either String a
decode Parser a
p

errTypeMismatch :: String -> AST.Value -> ParseResult a
errTypeMismatch :: forall a. String -> Value -> ParseResult a
errTypeMismatch String
a Value
b = forall a. String -> ParseResult a
failResult forall a b. (a -> b) -> a -> b
$ String
"TypeMismatch " forall a. [a] -> [a] -> [a]
++ String
a forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ String
b'
  where
    b' :: String
b' = case Value
b of
      AST.String ByteString
_  -> String
"String"
      AST.Integer ByteString
_ -> String
"Integer"
      AST.List Array Value
_    -> String
"List"
      AST.Dict Array KeyValue
_    -> String
"Dict"

-- Parsers below are all marked INLINE because they match on the AST
-- constructor and return Eithers. When inlined, GHC is able to optimize the
-- nested case matches using "case merging" and get rid of the intemeditate
-- Eithers using "case-of-case".

stringDirect :: Parser B.ByteString
stringDirect :: Parser ByteString
stringDirect = forall a. ReaderT Value ParseResult a -> Parser a
Parser forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Value
v -> case Value
v of
  AST.String ByteString
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
s
  Value
_            -> forall a. String -> Value -> ParseResult a
errTypeMismatch String
"String" Value
v
{-# INLINE stringDirect #-}

integerDirect :: Parser B.ByteString
integerDirect :: Parser ByteString
integerDirect = forall a. ReaderT Value ParseResult a -> Parser a
Parser forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Value
v -> case Value
v of
  AST.Integer ByteString
s -> forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
s
  Value
_             -> forall a. String -> Value -> ParseResult a
errTypeMismatch String
"Integer" Value
v
{-# INLINE integerDirect #-}

listDirect :: Parser (A.Array AST.Value)
listDirect :: Parser (Array Value)
listDirect = forall a. ReaderT Value ParseResult a -> Parser a
Parser forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Value
v -> case Value
v of
  AST.List Array Value
a -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Array Value
a
  Value
_          -> forall a. String -> Value -> ParseResult a
errTypeMismatch String
"List" Value
v
{-# INLINE listDirect #-}

dictDirect :: Parser (A.Array AST.KeyValue)
dictDirect :: Parser (Array KeyValue)
dictDirect = forall a. ReaderT Value ParseResult a -> Parser a
Parser forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Value
v -> case Value
v of
  AST.Dict Array KeyValue
a -> forall (f :: * -> *) a. Applicative f => a -> f a
pure Array KeyValue
a
  Value
_          -> forall a. String -> Value -> ParseResult a
errTypeMismatch String
"Dict" Value
v
{-# INLINE dictDirect #-}

-- | Decode a Bencode string as a ByteString. Fails on a non-string.
string :: Parser B.ByteString
string :: Parser ByteString
string = Parser ByteString
stringDirect
{-# INLINE string #-}

-- | Decode a Bencode integer as an Integer. Fails on a non-integer.
integer :: Parser Integer
integer :: Parser Integer
integer = ByteString -> Integer
toI forall (m :: * -> *) a b. Monad m => (a -> b) -> m a -> m b
<$!> Parser ByteString
integerDirect
  where
    -- BC.readInteger will be making redundant digit checks since we already
    -- know it to be a valid integer.
    -- But it has an efficient divide-and-conquer algorithm compared to the
    -- simple but O(n^2) foldl' (\acc x -> acc * 10 + x) 0.
    -- We can reimplement the algorithm without the redundant checks if we
    -- really want.
    toI :: ByteString -> Integer
toI ByteString
s = case ByteString -> Maybe (Integer, ByteString)
BC.readInteger ByteString
s of
      Maybe (Integer, ByteString)
Nothing    -> forall a. HasCallStack => String -> a
error String
"Data.Bencode.Decode.integer: should not happen"
      Just (Integer
i,ByteString
_) -> Integer
i
{-# INLINE integer #-}

-- | Decode a Bencode list with the given parser for elements. Fails on a
-- non-list or if any element in the list fails to parse.
list :: Parser a -> Parser (V.Vector a)
list :: forall a. Parser a -> Parser (Vector a)
list Parser a
p = Parser (Array Value)
listDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. ParseResult a -> Parser a
liftP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b.
(a -> ParseResult b) -> Array a -> ParseResult (Vector b)
traverseAToV (forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p)
{-# INLINE list #-}

traverseAToV :: (a -> ParseResult b) -> A.Array a -> ParseResult (V.Vector b)
traverseAToV :: forall a b.
(a -> ParseResult b) -> Array a -> ParseResult (Vector b)
traverseAToV a -> ParseResult b
f Array a
a = forall a. (forall s. ST s a) -> a
runST forall a b. (a -> b) -> a -> b
$ do
  let n :: Int
n = forall a. Array a -> Int
A.sizeofArray Array a
a
  MVector (PrimState (ST s)) b
v <- forall (m :: * -> *) a.
PrimMonad m =>
Int -> m (MVector (PrimState m) a)
VM.new Int
n
  let loop :: Int -> ST s (Maybe String)
loop Int
i | Int
i forall a. Eq a => a -> a -> Bool
== Int
n = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing
      loop Int
i = case a -> ParseResult b
f (forall a. Array a -> Int -> a
A.indexArray Array a
a Int
i) of
        ParseResult (Left String
e)  -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. a -> Maybe a
Just String
e)
        ParseResult (Right b
x) -> forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> Int -> a -> m ()
VM.write MVector (PrimState (ST s)) b
v Int
i b
x forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Int -> ST s (Maybe String)
loop (Int
iforall a. Num a => a -> a -> a
+Int
1)
  Maybe String
res <- Int -> ST s (Maybe String)
loop Int
0
  forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *) a.
PrimMonad m =>
MVector (PrimState m) a -> m (Vector a)
V.unsafeFreeze MVector (PrimState (ST s)) b
v) (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. String -> ParseResult a
failResult) Maybe String
res
{-# INLINABLE traverseAToV #-}

-- | Decode a Bencode dict with the given parser for values. Fails on a
-- non-dict or if any value in the dict fails to parse.
dict :: Parser a -> Parser (M.Map B.ByteString a)
dict :: forall a. Parser a -> Parser (Map ByteString a)
dict Parser a
p =
  Parser (Array KeyValue)
dictDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. ParseResult a -> Parser a
liftP forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall k a. [(k, a)] -> Map k a
M.fromDistinctAscList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse KeyValue -> ParseResult (ByteString, a)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
.  forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList
  where
    f :: KeyValue -> ParseResult (ByteString, a)
f (AST.KeyValue ByteString
k Value
v) = (,) ByteString
k forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p Value
v
{-# INLINE dict #-}

-- | Succeeds only on a Bencode string that equals the given string.
stringEq :: B.ByteString -> Parser ()
stringEq :: ByteString -> Parser ()
stringEq ByteString
s = Parser ByteString
stringDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \ByteString
s' ->
  if ByteString
s forall a. Eq a => a -> a -> Bool
== ByteString
s'
  then forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  else forall a. String -> Parser a
failParser forall a b. (a -> b) -> a -> b
$ String
"StringNotEq " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ByteString
s forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ByteString
s'
{-# INLINE stringEq #-}

-- | Decode a bencode string as UTF-8 text. Fails on a non-string or if the
-- string is not valid UTF-8.
text :: Parser T.Text
text :: Parser Text
text =
  Parser ByteString
stringDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>=
  forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const (forall a. String -> Parser a
failParser String
"UTF8DecodeFailure")) forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either UnicodeException Text
T.decodeUtf8'
{-# INLINE text #-}

-- | Succeeds only on a Bencode string that equals the given text.
textEq :: T.Text -> Parser ()
textEq :: Text -> Parser ()
textEq Text
t = Parser Text
text forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
t' ->
  if Text
t forall a. Eq a => a -> a -> Bool
== Text
t'
  then forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  else forall a. String -> Parser a
failParser forall a b. (a -> b) -> a -> b
$ String
"TextNotEq " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Text
t forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Text
t'
{-# INLINE textEq #-}

-- | Decode a Bencode integer as an @Int@. Fails on a non-integer or if the
-- integer is out of bounds for an @Int@.
int :: Parser Int
int :: Parser Int
int = Parser ByteString
integerDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. String -> Parser a
failParser String
"IntOutOfBounds") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Int
go
  where
    go :: ByteString -> Maybe Int
go ByteString
s = case ByteString -> Maybe (Char, ByteString)
BC.uncons ByteString
s of
      Just (Char
'-', ByteString
s') -> Bool -> ByteString -> Maybe Int
Util.readKnownNaturalAsInt Bool
True ByteString
s'
      Maybe (Char, ByteString)
_              -> Bool -> ByteString -> Maybe Int
Util.readKnownNaturalAsInt Bool
False ByteString
s
{-# INLINE int #-}

-- | Succeeds only on a Bencode integer that equals the given value.
intEq :: Int -> Parser ()
intEq :: Int -> Parser ()
intEq Int
i = Parser Int
int forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
i' ->
  if Int
i forall a. Eq a => a -> a -> Bool
== Int
i'
  then forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
  else forall a. String -> Parser a
failParser forall a b. (a -> b) -> a -> b
$ String
"IntNotEq " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i'
{-# INLINE intEq #-}

-- | Decode a Bencode integer as a @Word@. Fails on a non-integer or if the
-- integer is out of bounds for a @Word@.
word :: Parser Word
word :: Parser Word
word = Parser ByteString
integerDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. String -> Parser a
failParser String
"WordOutOfBounds") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Word
go
  where
    go :: ByteString -> Maybe Word
go ByteString
s = case ByteString -> Maybe (Char, ByteString)
BC.uncons ByteString
s of
      Just (Char
'-',ByteString
_) -> forall a. Maybe a
Nothing
      Maybe (Char, ByteString)
_            -> ByteString -> Maybe Word
Util.readKnownNaturalAsWord ByteString
s
{-# INLINE word #-}

-- | Decode a @Value@. Always succeeds for valid Bencode.
value :: Parser Value
value :: Parser Value
value = ByteString -> Value
String  forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ByteString
string
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Integer -> Value
Integer forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser Integer
integer
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Vector Value -> Value
List    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Parser a -> Parser (Vector a)
list Parser Value
value
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Map ByteString Value -> Value
Dict    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Parser a -> Parser (Map ByteString a)
dict Parser Value
value

-- | Always fails with the given message.
fail :: String -> Parser a
fail :: forall a. String -> Parser a
fail = forall a. String -> Parser a
failParser forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"Fail: " forall a. [a] -> [a] -> [a]
++ )
{-# INLINE fail #-}

-- | Decode a value with the given parser for the given key. Fails on a
-- non-dict, if the key is absent, or if the value parser fails.
--
-- If keys should not be left over in the dict, use 'field'' and 'dict''
-- instead.
--
-- ==== __Examples__
-- @
-- data File = File { name :: Text, size :: Int }
--
-- fileParser :: D.'Parser' File
-- fileParser =
--   File \<$> D.field "name" D.'text'
--        \<*> D.field "size" D.'int'
-- @
field :: B.ByteString -> Parser a -> Parser a
field :: forall a. ByteString -> Parser a -> Parser a
field ByteString
k Parser a
p = do
  Array KeyValue
a <- Parser (Array KeyValue)
dictDirect
  case ByteString -> Array KeyValue -> (# (# #) | (# Int#, Value #) #)
binarySearch ByteString
k Array KeyValue
a of
    (# (# #)
_ |            #) -> forall a. String -> Parser a
failParser forall a b. (a -> b) -> a -> b
$ String
"KeyNotFound " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ByteString
k
    (#   | (# Int#
_, Value
x #) #) -> forall a. ParseResult a -> Parser a
liftP forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p Value
x
{-# INLINE field #-}

-- | Decode a value with the given parser for the given key. Convert to a
-- @Parser@ with 'dict''.
--
-- @since 0.1.1.0
field' :: B.ByteString -> Parser a -> Fields a
field' :: forall a. ByteString -> Parser a -> Fields a
field' ByteString
k Parser a
p = forall a.
ReaderT (Array KeyValue) (StateT IntSet ParseResult) a -> Fields a
Fields forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Array KeyValue
a -> case ByteString -> Array KeyValue -> (# (# #) | (# Int#, Value #) #)
binarySearch ByteString
k Array KeyValue
a of
  (# (# #)
_ |             #) -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. String -> ParseResult a
failResult forall a b. (a -> b) -> a -> b
$ String
"KeyNotFound " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ByteString
k
  (#   | (# Int#
i#, Value
v #) #) -> forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p Value
v) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify' (Int -> IntSet -> IntSet
IS.insert (Int# -> Int
X.I# Int#
i#))
{-# INLINE field' #-}

-- | Create a @Parser@ from a 'Fields'. Fails on a non-dict, if a key is
-- absent, or if any value fails to parse. Also fails if there are leftover
-- unparsed keys in the dict.
--
-- If leftover keys should be ignored, use 'field' instead.
--
-- ==== __Examples__
-- @
-- data File = File { name :: Text, size :: Int }
--
-- fileParser :: D.'Parser' File
-- fileParser = D.dict' $
--   File \<$> D.'field'' "name" D.'text'
--        \<*> D.'field'' "size" D.'int'
-- @
--
-- @since 0.1.1.0
dict' :: Fields a -> Parser a
dict' :: forall a. Fields a -> Parser a
dict' Fields a
fs = do
  Array KeyValue
a <- Parser (Array KeyValue)
dictDirect
  forall a. ParseResult a -> Parser a
liftP forall a b. (a -> b) -> a -> b
$ do
    (a
v, IntSet
is) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (forall a.
Fields a -> ReaderT (Array KeyValue) (StateT IntSet ParseResult) a
runFields Fields a
fs) Array KeyValue
a) IntSet
IS.empty
    if IntSet -> Int
IS.size IntSet
is forall a. Eq a => a -> a -> Bool
== forall a. Array a -> Int
A.sizeofArray Array KeyValue
a
    then forall (f :: * -> *) a. Applicative f => a -> f a
pure a
v
    else let i :: Int
i = forall a. [a] -> a
head forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
filter (Int -> IntSet -> Bool
`IS.notMember` IntSet
is) [Int
0..]
             AST.KeyValue ByteString
k Value
_ = forall a. Array a -> Int -> a
A.indexArray Array KeyValue
a Int
i
         in forall a. String -> ParseResult a
failResult forall a b. (a -> b) -> a -> b
$ String
"UnrecognizedKey " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show ByteString
k
{-# INLINE dict' #-}

-- | Key-value parsers. See 'dict'' and 'field''.
newtype Fields a = Fields
  { forall a.
Fields a -> ReaderT (Array KeyValue) (StateT IntSet ParseResult) a
runFields ::
      ReaderT (A.Array AST.KeyValue) (StateT IS.IntSet ParseResult) a
  } deriving (forall a b. a -> Fields b -> Fields a
forall a b. (a -> b) -> Fields a -> Fields b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Fields b -> Fields a
$c<$ :: forall a b. a -> Fields b -> Fields a
fmap :: forall a b. (a -> b) -> Fields a -> Fields b
$cfmap :: forall a b. (a -> b) -> Fields a -> Fields b
Functor, Functor Fields
forall a. a -> Fields a
forall a b. Fields a -> Fields b -> Fields a
forall a b. Fields a -> Fields b -> Fields b
forall a b. Fields (a -> b) -> Fields a -> Fields b
forall a b c. (a -> b -> c) -> Fields a -> Fields b -> Fields c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Fields a -> Fields b -> Fields a
$c<* :: forall a b. Fields a -> Fields b -> Fields a
*> :: forall a b. Fields a -> Fields b -> Fields b
$c*> :: forall a b. Fields a -> Fields b -> Fields b
liftA2 :: forall a b c. (a -> b -> c) -> Fields a -> Fields b -> Fields c
$cliftA2 :: forall a b c. (a -> b -> c) -> Fields a -> Fields b -> Fields c
<*> :: forall a b. Fields (a -> b) -> Fields a -> Fields b
$c<*> :: forall a b. Fields (a -> b) -> Fields a -> Fields b
pure :: forall a. a -> Fields a
$cpure :: forall a. a -> Fields a
Applicative, Applicative Fields
forall a. Fields a
forall a. Fields a -> Fields [a]
forall a. Fields a -> Fields a -> Fields a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. Fields a -> Fields [a]
$cmany :: forall a. Fields a -> Fields [a]
some :: forall a. Fields a -> Fields [a]
$csome :: forall a. Fields a -> Fields [a]
<|> :: forall a. Fields a -> Fields a -> Fields a
$c<|> :: forall a. Fields a -> Fields a -> Fields a
empty :: forall a. Fields a
$cempty :: forall a. Fields a
Alternative, Applicative Fields
forall a. a -> Fields a
forall a b. Fields a -> Fields b -> Fields b
forall a b. Fields a -> (a -> Fields b) -> Fields b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Fields a
$creturn :: forall a. a -> Fields a
>> :: forall a b. Fields a -> Fields b -> Fields b
$c>> :: forall a b. Fields a -> Fields b -> Fields b
>>= :: forall a b. Fields a -> (a -> Fields b) -> Fields b
$c>>= :: forall a b. Fields a -> (a -> Fields b) -> Fields b
Monad)
-- We could use WriterT (CPS) but StateT is a teeny bit more efficient because
-- we can do IS.insert x instead of IS.union (IS.singleton x).

-- | Decode a list element with the given parser at the given (0-based) index.
-- Fails on a non-list, if the index is out of bounds, or if the element parser
-- fails.
--
-- Also see 'elem' and 'list''.
--
-- ==== __Examples__
-- @
-- data File = File { name :: Text, size :: Int }
--
-- fileParser :: D.'Parser' File
-- fileParser =
--   File \<$> D.index 0 D.'text'
--        \<*> D.index 1 D.'int'
-- @
--
-- @since 0.1.1.0
index :: Int -> Parser a -> Parser a
index :: forall a. Int -> Parser a -> Parser a
index Int
i Parser a
_ | Int
i forall a. Ord a => a -> a -> Bool
< Int
0 = forall a. String -> Parser a
failParser String
"IndexOutOfBounds"
index Int
i Parser a
p = do
  Array Value
a <- Parser (Array Value)
listDirect
  if Int
i forall a. Ord a => a -> a -> Bool
< forall a. Array a -> Int
A.sizeofArray Array Value
a
  then forall a. ParseResult a -> Parser a
liftP forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p (forall a. Array a -> Int -> a
A.indexArray Array Value
a Int
i)
  else forall a. String -> Parser a
failParser String
"IndexOutOfBounds"
{-# INLINE index #-}

-- | Decode the next list element with the given parser. Convert to a @Parser@
-- with 'list''.
--
-- @since 0.1.1.0
elem :: Parser a -> Elems a
elem :: forall a. Parser a -> Elems a
elem Parser a
p = forall a.
ReaderT (Array Value) (StateT Int ParseResult) a -> Elems a
Elems forall a b. (a -> b) -> a -> b
$ forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT forall a b. (a -> b) -> a -> b
$ \Array Value
a -> do
  Int
i <- forall (m :: * -> *) s. Monad m => StateT s m s
get
  if Int
i forall a. Ord a => a -> a -> Bool
< forall a. Array a -> Int
A.sizeofArray Array Value
a
  then forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (forall a. Parser a -> Value -> ParseResult a
runParser Parser a
p (forall a. Array a -> Int -> a
A.indexArray Array Value
a Int
i)) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* (forall (m :: * -> *) s. Monad m => s -> StateT s m ()
put forall a b. (a -> b) -> a -> b
$! Int
iforall a. Num a => a -> a -> a
+Int
1)
  else forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall a b. (a -> b) -> a -> b
$ forall a. String -> ParseResult a
failResult String
"ListElemsExhausted"
{-# INLINE elem #-}

-- | Create a @Parser@ from an @Elems@. Fails on a non-list, if the number of
-- elements does not match the @Elems@ exactly, or if any element parser fails.
--
-- ==== __Examples__
-- @
-- data File = File { name :: Text, size :: Int }
--
-- fileParser :: D.'Parser' File
-- fileParser = D.list' $
--   File \<$> D.'elem' D.'text'
--        \<*> D.'elem' D.'int'
-- @
--
-- @since 0.1.1.0
list' :: Elems a -> Parser a
list' :: forall a. Elems a -> Parser a
list' Elems a
es = do
  Array Value
a <- Parser (Array Value)
listDirect
  forall a. ParseResult a -> Parser a
liftP forall a b. (a -> b) -> a -> b
$ do
    (a
x, Int
i) <- forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (forall a.
Elems a -> ReaderT (Array Value) (StateT Int ParseResult) a
runElems Elems a
es) Array Value
a) Int
0
    if Int
i forall a. Eq a => a -> a -> Bool
== forall a. Array a -> Int
A.sizeofArray Array Value
a
    then forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
    else forall a. String -> ParseResult a
failResult forall a b. (a -> b) -> a -> b
$ String
"ListElemsLeft"
{-# INLINE list' #-}

-- | List elements parser. See 'elem' and 'list''.
newtype Elems a = Elems
  { forall a.
Elems a -> ReaderT (Array Value) (StateT Int ParseResult) a
runElems :: ReaderT (A.Array AST.Value) (StateT Int ParseResult) a
  } deriving (forall a b. a -> Elems b -> Elems a
forall a b. (a -> b) -> Elems a -> Elems b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> Elems b -> Elems a
$c<$ :: forall a b. a -> Elems b -> Elems a
fmap :: forall a b. (a -> b) -> Elems a -> Elems b
$cfmap :: forall a b. (a -> b) -> Elems a -> Elems b
Functor, Functor Elems
forall a. a -> Elems a
forall a b. Elems a -> Elems b -> Elems a
forall a b. Elems a -> Elems b -> Elems b
forall a b. Elems (a -> b) -> Elems a -> Elems b
forall a b c. (a -> b -> c) -> Elems a -> Elems b -> Elems c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. Elems a -> Elems b -> Elems a
$c<* :: forall a b. Elems a -> Elems b -> Elems a
*> :: forall a b. Elems a -> Elems b -> Elems b
$c*> :: forall a b. Elems a -> Elems b -> Elems b
liftA2 :: forall a b c. (a -> b -> c) -> Elems a -> Elems b -> Elems c
$cliftA2 :: forall a b c. (a -> b -> c) -> Elems a -> Elems b -> Elems c
<*> :: forall a b. Elems (a -> b) -> Elems a -> Elems b
$c<*> :: forall a b. Elems (a -> b) -> Elems a -> Elems b
pure :: forall a. a -> Elems a
$cpure :: forall a. a -> Elems a
Applicative, Applicative Elems
forall a. Elems a
forall a. Elems a -> Elems [a]
forall a. Elems a -> Elems a -> Elems a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. Elems a -> Elems [a]
$cmany :: forall a. Elems a -> Elems [a]
some :: forall a. Elems a -> Elems [a]
$csome :: forall a. Elems a -> Elems [a]
<|> :: forall a. Elems a -> Elems a -> Elems a
$c<|> :: forall a. Elems a -> Elems a -> Elems a
empty :: forall a. Elems a
$cempty :: forall a. Elems a
Alternative, Applicative Elems
forall a. a -> Elems a
forall a b. Elems a -> Elems b -> Elems b
forall a b. Elems a -> (a -> Elems b) -> Elems b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> Elems a
$creturn :: forall a. a -> Elems a
>> :: forall a b. Elems a -> Elems b -> Elems b
$c>> :: forall a b. Elems a -> Elems b -> Elems b
>>= :: forall a b. Elems a -> (a -> Elems b) -> Elems b
$c>>= :: forall a b. Elems a -> (a -> Elems b) -> Elems b
Monad)

-- | Decode a Bencode integer as an @Int64@. Fails on a non-integer or if the
-- integer is out of bounds for an @Int64@.
--
-- @since 0.1.1.0
int64 :: Parser Int64
int64 :: Parser Int64
int64 = Parser ByteString
integerDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. String -> Parser a
failParser String
"IntOutOfBounds") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Int64
go
  where
    go :: ByteString -> Maybe Int64
go ByteString
s = case ByteString -> Maybe (Char, ByteString)
BC.uncons ByteString
s of
      Just (Char
'-', ByteString
s') -> Bool -> ByteString -> Maybe Int64
Util.readKnownNaturalAsInt64 Bool
True ByteString
s'
      Maybe (Char, ByteString)
_              -> Bool -> ByteString -> Maybe Int64
Util.readKnownNaturalAsInt64 Bool
False ByteString
s
{-# INLINE int64 #-}

-- | Decode a Bencode integer as an @Int32@. Fails on a non-integer or if the
-- integer is out of bounds for an @Int32@.
--
-- @since 0.1.1.0
int32 :: Parser Int32
int32 :: Parser Int32
int32 = forall a. (Bounded a, Integral a) => Parser a
intL32
{-# INLINE int32 #-}

-- | Decode a Bencode integer as an @Int16@. Fails on a non-integer or if the
-- integer is out of bounds for an @Int16@.
--
-- @since 0.1.1.0
int16 :: Parser Int16
int16 :: Parser Int16
int16 = forall a. (Bounded a, Integral a) => Parser a
intL32
{-# INLINE int16 #-}

-- | Decode a Bencode integer as an @Int8@. Fails on a non-integer or if the
-- integer is out of bounds for an @Int8@.
--
-- @since 0.1.1.0
int8 :: Parser Int8
int8 :: Parser Int8
int8 = forall a. (Bounded a, Integral a) => Parser a
intL32
{-# INLINE int8 #-}

-- Parse an Int(<=32) via Int.
intL32 :: forall a. (Bounded a, Integral a) => Parser a
intL32 :: forall a. (Bounded a, Integral a) => Parser a
intL32 = Parser Int
int forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Int
i ->
  if forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
minBound :: a) forall a. Ord a => a -> a -> Bool
<= Int
i Bool -> Bool -> Bool
&& Int
i forall a. Ord a => a -> a -> Bool
<= forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
maxBound :: a)
  then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
i
  else forall a. String -> Parser a
failParser String
"IntOutOfBounds"
{-# INLINE intL32 #-}

-- | Decode a Bencode integer as a @Word64@. Fails on a non-integer or if the
-- integer is out of bounds for a @Word64@.
--
-- @since 0.1.1.0
word64 :: Parser Word64
word64 :: Parser Word64
word64 = Parser ByteString
integerDirect forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a. String -> Parser a
failParser String
"WordOutOfBounds") forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe Word64
go
  where
    go :: ByteString -> Maybe Word64
go ByteString
s = case ByteString -> Maybe (Char, ByteString)
BC.uncons ByteString
s of
      Just (Char
'-', ByteString
_) -> forall a. Maybe a
Nothing
      Maybe (Char, ByteString)
_             -> ByteString -> Maybe Word64
Util.readKnownNaturalAsWord64 ByteString
s
{-# INLINE word64 #-}

-- | Decode a Bencode integer as a @Word32@. Fails on a non-integer or if the
-- integer is out of bounds for a @Word32@.
--
-- @since 0.1.1.0
word32 :: Parser Word32
word32 :: Parser Word32
word32 = forall a. (Bounded a, Integral a) => Parser a
wordL32
{-# INLINE word32 #-}

-- | Decode a Bencode integer as a @Word16@. Fails on a non-integer or if the
-- integer is out of bounds for a @Word16@.
--
-- @since 0.1.1.0
word16 :: Parser Word16
word16 :: Parser Word16
word16 = forall a. (Bounded a, Integral a) => Parser a
wordL32
{-# INLINE word16 #-}

-- | Decode a Bencode integer as a @Word8@. Fails on a non-integer or if the
-- integer is out of bounds for a @Word8@.
--
-- @since 0.1.1.0
word8 :: Parser Word8
word8 :: Parser Word8
word8 = forall a. (Bounded a, Integral a) => Parser a
wordL32
{-# INLINE word8 #-}

-- Parse a Word(<=32) via Word.
wordL32 :: forall a. (Bounded a, Integral a) => Parser a
wordL32 :: forall a. (Bounded a, Integral a) => Parser a
wordL32 = Parser Word
word forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Word
i ->
  if forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
minBound :: a) forall a. Ord a => a -> a -> Bool
<= Word
i Bool -> Bool -> Bool
&& Word
i forall a. Ord a => a -> a -> Bool
<= forall a b. (Integral a, Num b) => a -> b
fromIntegral (forall a. Bounded a => a
maxBound :: a)
  then forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$! forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
i
  else forall a. String -> Parser a
failParser String
"WordOutOfBounds"
{-# INLINE wordL32 #-}

-- | Run the function on the parsed value, fail with 'empty' if the result is
-- @Nothing@.
--
-- @since 0.1.1.0
mapMaybe :: (a -> Maybe b) -> Parser a -> Parser b
mapMaybe :: forall a b. (a -> Maybe b) -> Parser a -> Parser b
mapMaybe a -> Maybe b
f Parser a
p = Parser a
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall (f :: * -> *) a. Alternative f => f a
empty forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f
{-# INLINE mapMaybe #-}

-- | Run the function on the parsed value, fail with 'fail' if the result is a
-- @Left@.
--
-- @since 0.1.1.0
mapOrFail :: (a -> Either String b) -> Parser a -> Parser b
mapOrFail :: forall a b. (a -> Either String b) -> Parser a -> Parser b
mapOrFail a -> Either String b
f Parser a
p = Parser a
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. String -> Parser a
fail forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either String b
f
{-# INLINE mapOrFail #-}

-- | Binary search. The array must be sorted by key.
binarySearch
  :: B.ByteString
  -> A.Array AST.KeyValue
  -> (# (# #) | (# X.Int#,  AST.Value #) #)
binarySearch :: ByteString -> Array KeyValue -> (# (# #) | (# Int#, Value #) #)
binarySearch ByteString
k Array KeyValue
a = Int -> Int -> (# (# #) | (# Int#, Value #) #)
go Int
0 (forall a. Array a -> Int
A.sizeofArray Array KeyValue
a)
  where
    go :: Int -> Int -> (# (# #) | (# Int#, Value #) #)
go Int
l Int
r | Int
l forall a. Eq a => a -> a -> Bool
== Int
r = (# (# #) | #)
    go Int
l Int
r = case forall a. Ord a => a -> a -> Ordering
compare ByteString
k ByteString
k' of
      Ordering
LT -> Int -> Int -> (# (# #) | (# Int#, Value #) #)
go Int
l Int
m
      Ordering
EQ -> (# | (# case Int
m of X.I# Int#
m# -> Int#
m#, Value
v #) #)
      Ordering
GT -> Int -> Int -> (# (# #) | (# Int#, Value #) #)
go (Int
mforall a. Num a => a -> a -> a
+Int
1) Int
r
      where
        -- Overflow, careful!
        m :: Int
m = forall a b. (Integral a, Num b) => a -> b
fromIntegral ((forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
lforall a. Num a => a -> a -> a
+Int
r) :: Word) forall a. Integral a => a -> a -> a
`div` Word
2) :: Int
        AST.KeyValue ByteString
k' Value
v = forall a. Array a -> Int -> a
A.indexArray Array KeyValue
a Int
m
{-# INLINABLE binarySearch #-}
-- binarySearch returns an unboxed type, which serves as an equivalent of
-- Maybe (Int, Value). This is to avoid allocating the (,) and the I#. This
-- won't be necessary if GHC gets
-- https://gitlab.haskell.org/ghc/ghc/-/issues/14259.
-- A (not-so-good) alternative is to inline binarySearch.

------------------------------
-- Documentation
------------------------------

-- $quick
-- Decoding is done using parsers. This module defines parsers that can be
-- composed to build parsers for arbitrary types.
--
-- @
-- data File = File
--   { hash :: ByteString
--   , size :: Integer
--   , tags :: Vector Text
--   } deriving Show
-- @
--
-- Assuming a @File@ is encoded as a Bencode dictionary with the field names as
-- keys and appropriate value types, a parser for @File@ can be defined as
--
-- @
-- {-# LANGUAGE OverloadedStrings #-}
-- import qualified Data.Bencode.Decode as D
--
-- fileParser :: D.'Parser' File
-- fileParser =
--   File \<\$> D.'field' "hash" D.'string'
--        \<*> D.'field' "size" D.'integer'
--        \<*> D.'field' "tags" (D.'list' D.'text')
-- @
--
-- The parser can then be run on a @ByteString@ with 'decode'.
--
-- >>> D.decode fileParser "d4:hash4:xxxx4:sizei1024e4:tagsl4:work6:backupee"
-- Right (File {hash = "xxxx", size = 1024, tags = ["work","backup"]})
--
-- Of course, invalid Bencode or Bencode that does not satisfy the @File@ parser
-- will fail to decode.
--
-- >>> D.decode fileParser "d4:hash4:xxxx4:tagsl4:work6:backupee"
-- Left "KeyNotFound \"size\""
--
-- For more examples, see the [Recipes](#g:recipes) section at the end of this
-- page.


-- $recipes
-- Recipes for some common and uncommon usages.
--
-- The following preface is assumed.
--
-- @
-- {-# LANGUAGE OverloadedStrings #-}
-- import Data.ByteString (ByteString)
-- import Data.Text (Text)
-- import qualified Data.Bencode.Decode as D
-- @
--
-- === Decode an optional field
--
-- @
-- import Control.Applicative ('optional')
--
-- data File = File { name :: Text, size :: Maybe Int } deriving Show
--
-- fileParser :: D.'Parser' File
-- fileParser =
--   File
--     \<$> D.'field' "name" D.'text'
--     \<*> optional (D.'field' "size" D.'int')
-- @
--
-- >>> D.decode fileParser "d4:name9:hello.txt4:sizei16ee"
-- Right (File {name = "hello.txt", size = Just 16})
-- >>> D.decode fileParser "d4:name9:hello.txte"
-- Right (File {name = "hello.txt", size = Nothing})
--
-- === Decode an enum
--
-- @
-- import Control.Applicative ('(<|>)')
--
-- data Color = Red | Green | Blue deriving Show
--
-- colorParser :: D.'Parser' Color
-- colorParser =
--       Red   \<$ D.'stringEq' "red"
--   \<|> Green \<$ D.'stringEq' "green"
--   \<|> Blue  \<$ D.'stringEq' "blue"
--   \<|> D.'fail' "unknown color"
-- @
--
-- >>> D.decode colorParser "5:green"
-- Right Green
-- >>> D.decode colorParser "5:black"
-- Left "Fail: unknown color"
--
-- === Decode a dict, failing on leftover keys
--
-- @
-- data File = File { name :: Text, size :: Int } deriving Show
--
-- fileParser :: D.'Parser' File
-- fileParser = D.'dict'' $
--   File
--     \<$> D.'field'' "name" D.'text'
--     \<*> D.'field'' "size" D.'int'
-- @
--
-- >>> D.decode fileParser "d4:name9:hello.txt4:sizei32ee"
-- Right (File {name = "hello.txt", size = 32})
-- >>> D.decode fileParser "d6:hiddeni1e4:name9:hello.txt4:sizei32ee"
-- Left "UnrecognizedKey \"hidden\""
--
-- === Decode differently based on dict contents
--
-- @
-- data Response = Response
--   { id_    :: Int
--   , result :: Either Text ByteString
--   } deriving Show
--
-- responseParser :: D.'Parser' Response
-- responseParser = do
--   id_ <- D.'field' "id" D.'int'
--   status <- D.'field' "status" D.'string'
--   case status of
--     "failure" -> do
--       reason <- D.'field' "reason" D.'text'
--       pure $ Response id_ (Left reason)
--     "success" -> do
--       data_ <- D.'field' "data" D.'string'
--       pure $ Response id_ (Right data_)
--     _ -> D.'fail' "unknown status"
-- @
--
-- >>> D.decode responseParser "d2:idi42e6:reason12:unauthorized6:status7:failuree"
-- Right (Response {id_ = 42, result = Left "unauthorized"})
-- >>> D.decode responseParser "d4:data4:00002:idi42e6:status7:successe"
-- Right (Response {id_ = 42, result = Right "0000"})
--
-- === Decode nested dicts
--
-- @
-- data File = File { name :: Text, size :: Int } deriving Show
--
-- fileParser :: D.'Parser' File
-- fileParser =
--   File
--     \<$> D.'field' "name" D.'text'
--     \<*> D.'field' "metadata" (D.'field' "info" (D.'field' "size" D.'int'))
-- @
--
-- >>> D.decode fileParser "d8:metadatad4:infod4:sizei32eee4:name9:hello.txte"
-- Right (File {name = "hello.txt", size = 32})
--
-- === Decode a heterogeneous list
--
-- @
-- data File = File { name :: Text, size :: Int } deriving Show
--
-- fileParser :: D.'Parser' File
-- fileParser = D.'list'' $
--   File
--     \<$> D.'elem' D.'text'
--     \<*> D.'elem' D.'int'
-- @
--
-- >>> D.decode fileParser "l9:hello.txti32ee"
-- Right (File {name = "hello.txt", size = 32})
--