-- |
--   Copyright   :  (c) Sam Truzjan 2013
--   License     :  BSD3
--   Maintainer  :  pxqr.sta@gmail.com
--   Stability   :  stable
--   Portability :  portable
--
--   Types for working with bencode data.

{-# LANGUAGE DeriveGeneric #-}

module Data.BEncode.Types
       ( -- * Types
         BInteger
       , BString
       , BList
       , BDict
       , BValue (..)

         -- * Predicates
       , isInteger
       , isString
       , isList
       , isDict
       ) where

import Control.DeepSeq
import Data.ByteString
import GHC.Generics (Generic)

import Data.BEncode.BDict

-- | A bencode "integer".
type BInteger = Integer

-- | A raw bencode string.
type BString  = ByteString

-- | A plain bencode list.
type BList    = [BValue]

-- | A bencode dictionary.
type BDict    = BDictMap BValue

-- | 'BValue' is straightforward ADT for b-encoded values. Please
-- note that since dictionaries are sorted, in most cases we can
-- compare BEncoded values without serialization and vice versa.
-- Lists is not required to be sorted through.
--
data BValue
  = BInteger !BInteger -- ^ bencode integers;
  | BString  !BString  -- ^ bencode strings;
  | BList     BList    -- ^ list of bencode values;
  | BDict     BDict    -- ^ bencode key-value dictionary.
    deriving (Int -> BValue -> ShowS
[BValue] -> ShowS
BValue -> String
(Int -> BValue -> ShowS)
-> (BValue -> String) -> ([BValue] -> ShowS) -> Show BValue
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BValue] -> ShowS
$cshowList :: [BValue] -> ShowS
show :: BValue -> String
$cshow :: BValue -> String
showsPrec :: Int -> BValue -> ShowS
$cshowsPrec :: Int -> BValue -> ShowS
Show, ReadPrec [BValue]
ReadPrec BValue
Int -> ReadS BValue
ReadS [BValue]
(Int -> ReadS BValue)
-> ReadS [BValue]
-> ReadPrec BValue
-> ReadPrec [BValue]
-> Read BValue
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [BValue]
$creadListPrec :: ReadPrec [BValue]
readPrec :: ReadPrec BValue
$creadPrec :: ReadPrec BValue
readList :: ReadS [BValue]
$creadList :: ReadS [BValue]
readsPrec :: Int -> ReadS BValue
$creadsPrec :: Int -> ReadS BValue
Read, BValue -> BValue -> Bool
(BValue -> BValue -> Bool)
-> (BValue -> BValue -> Bool) -> Eq BValue
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BValue -> BValue -> Bool
$c/= :: BValue -> BValue -> Bool
== :: BValue -> BValue -> Bool
$c== :: BValue -> BValue -> Bool
Eq, Eq BValue
Eq BValue
-> (BValue -> BValue -> Ordering)
-> (BValue -> BValue -> Bool)
-> (BValue -> BValue -> Bool)
-> (BValue -> BValue -> Bool)
-> (BValue -> BValue -> Bool)
-> (BValue -> BValue -> BValue)
-> (BValue -> BValue -> BValue)
-> Ord BValue
BValue -> BValue -> Bool
BValue -> BValue -> Ordering
BValue -> BValue -> BValue
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BValue -> BValue -> BValue
$cmin :: BValue -> BValue -> BValue
max :: BValue -> BValue -> BValue
$cmax :: BValue -> BValue -> BValue
>= :: BValue -> BValue -> Bool
$c>= :: BValue -> BValue -> Bool
> :: BValue -> BValue -> Bool
$c> :: BValue -> BValue -> Bool
<= :: BValue -> BValue -> Bool
$c<= :: BValue -> BValue -> Bool
< :: BValue -> BValue -> Bool
$c< :: BValue -> BValue -> Bool
compare :: BValue -> BValue -> Ordering
$ccompare :: BValue -> BValue -> Ordering
$cp1Ord :: Eq BValue
Ord, (forall x. BValue -> Rep BValue x)
-> (forall x. Rep BValue x -> BValue) -> Generic BValue
forall x. Rep BValue x -> BValue
forall x. BValue -> Rep BValue x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BValue x -> BValue
$cfrom :: forall x. BValue -> Rep BValue x
Generic)

instance NFData BValue where
    rnf :: BValue -> ()
rnf (BInteger BInteger
i) = BInteger -> ()
forall a. NFData a => a -> ()
rnf BInteger
i
    rnf (BString  BString
s) = BString -> ()
forall a. NFData a => a -> ()
rnf BString
s
    rnf (BList    [BValue]
l) = [BValue] -> ()
forall a. NFData a => a -> ()
rnf [BValue]
l
    rnf (BDict    BDict
d) = BDict -> ()
forall a. NFData a => a -> ()
rnf BDict
d

{--------------------------------------------------------------------
  Predicates
--------------------------------------------------------------------}

-- | Test if bencoded value is an integer.
isInteger :: BValue -> Bool
isInteger :: BValue -> Bool
isInteger (BInteger BInteger
_) = Bool
True
isInteger BValue
_            = Bool
False
{-# INLINE isInteger #-}

-- | Test if bencoded value is a string, both raw and utf8 encoded.
isString :: BValue -> Bool
isString :: BValue -> Bool
isString (BString BString
_) = Bool
True
isString BValue
_           = Bool
False
{-# INLINE isString #-}

-- | Test if bencoded value is a list.
isList :: BValue -> Bool
isList :: BValue -> Bool
isList (BList [BValue]
_) = Bool
True
isList BValue
_         = Bool
False
{-# INLINE isList #-}

-- | Test if bencoded value is a dictionary.
isDict :: BValue -> Bool
isDict :: BValue -> Bool
isDict (BList [BValue]
_) = Bool
True
isDict BValue
_         = Bool
False
{-# INLINE isDict #-}