summaryrefslogtreecommitdiff
path: root/src/Data/BEncode/Types.hs
blob: e505249b124f0609e444e0e6f2724f743b8d9c26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- |
--   Copyright   :  (c) Sam Truzjan 2013
--   License     :  BSD3
--   Maintainer  :  pxqr.sta@gmail.com
--   Stability   :  stable
--   Portability :  portable
--
--   Types for working with bencode data.
--
module Data.BEncode.Types
       ( BInteger
       , BString
       , BList
       , BDict
       , BValue (..)
       ) where

import Control.DeepSeq
import Data.ByteString

import Data.BEncode.BDict


type BInteger = Integer
type BString  = ByteString
type BList    = [BValue]
type BDict    = BDictMap BValue

-- | 'BEncode' 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 (Show, Read, Eq, Ord)

instance NFData BValue where
    rnf (BInteger i) = rnf i
    rnf (BString  s) = rnf s
    rnf (BList    l) = rnf l
    rnf (BDict    d) = rnf d