diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-29 08:03:07 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-09-29 08:03:07 +0400 |
commit | e7a93379de8af8567d052e71c6f728a4665bc102 (patch) | |
tree | e4d193336a7f6be9bc765ff39229b374a770d3f9 | |
parent | e1810a1318da49db2e25cdcc478b41dbe938928f (diff) |
Move types to separate module
-rw-r--r-- | src/Data/BEncode.hs | 26 | ||||
-rw-r--r-- | src/Data/BEncode/Types.hs | 45 |
2 files changed, 48 insertions, 23 deletions
diff --git a/src/Data/BEncode.hs b/src/Data/BEncode.hs index a7c766d..4d7b6c5 100644 --- a/src/Data/BEncode.hs +++ b/src/Data/BEncode.hs | |||
@@ -45,6 +45,8 @@ | |||
45 | {-# LANGUAGE FlexibleInstances #-} | 45 | {-# LANGUAGE FlexibleInstances #-} |
46 | {-# LANGUAGE Trustworthy #-} | 46 | {-# LANGUAGE Trustworthy #-} |
47 | {-# LANGUAGE CPP #-} | 47 | {-# LANGUAGE CPP #-} |
48 | {-# LANGUAGE BangPatterns #-} | ||
49 | {-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
48 | 50 | ||
49 | #if __GLASGOW_HASKELL__ >= 702 | 51 | #if __GLASGOW_HASKELL__ >= 702 |
50 | {-# LANGUAGE TypeOperators #-} | 52 | {-# LANGUAGE TypeOperators #-} |
@@ -137,31 +139,9 @@ import GHC.Generics | |||
137 | #endif | 139 | #endif |
138 | 140 | ||
139 | import Data.BEncode.BDict as BD | 141 | import Data.BEncode.BDict as BD |
142 | import Data.BEncode.Types | ||
140 | 143 | ||
141 | 144 | ||
142 | type BInteger = Integer | ||
143 | type BString = ByteString | ||
144 | type BList = [BValue] | ||
145 | type BDict = BDictMap BValue | ||
146 | |||
147 | -- | 'BEncode' is straightforward ADT for b-encoded values. Please | ||
148 | -- note that since dictionaries are sorted, in most cases we can | ||
149 | -- compare BEncoded values without serialization and vice versa. | ||
150 | -- Lists is not required to be sorted through. | ||
151 | -- | ||
152 | data BValue | ||
153 | = BInteger !BInteger -- ^ bencode integers; | ||
154 | | BString !BString -- ^ bencode strings; | ||
155 | | BList BList -- ^ list of bencode values; | ||
156 | | BDict BDict -- ^ bencode key-value dictionary. | ||
157 | deriving (Show, Read, Eq, Ord) | ||
158 | |||
159 | instance NFData BValue where | ||
160 | rnf (BInteger i) = rnf i | ||
161 | rnf (BString s) = rnf s | ||
162 | rnf (BList l) = rnf l | ||
163 | rnf (BDict d) = rnf d | ||
164 | |||
165 | -- | Result used in decoding operations. | 145 | -- | Result used in decoding operations. |
166 | type Result = Either String | 146 | type Result = Either String |
167 | 147 | ||
diff --git a/src/Data/BEncode/Types.hs b/src/Data/BEncode/Types.hs new file mode 100644 index 0000000..e505249 --- /dev/null +++ b/src/Data/BEncode/Types.hs | |||
@@ -0,0 +1,45 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD3 | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : stable | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | -- Types for working with bencode data. | ||
9 | -- | ||
10 | module Data.BEncode.Types | ||
11 | ( BInteger | ||
12 | , BString | ||
13 | , BList | ||
14 | , BDict | ||
15 | , BValue (..) | ||
16 | ) where | ||
17 | |||
18 | import Control.DeepSeq | ||
19 | import Data.ByteString | ||
20 | |||
21 | import Data.BEncode.BDict | ||
22 | |||
23 | |||
24 | type BInteger = Integer | ||
25 | type BString = ByteString | ||
26 | type BList = [BValue] | ||
27 | type BDict = BDictMap BValue | ||
28 | |||
29 | -- | 'BEncode' is straightforward ADT for b-encoded values. Please | ||
30 | -- note that since dictionaries are sorted, in most cases we can | ||
31 | -- compare BEncoded values without serialization and vice versa. | ||
32 | -- Lists is not required to be sorted through. | ||
33 | -- | ||
34 | data BValue | ||
35 | = BInteger !BInteger -- ^ bencode integers; | ||
36 | | BString !BString -- ^ bencode strings; | ||
37 | | BList BList -- ^ list of bencode values; | ||
38 | | BDict BDict -- ^ bencode key-value dictionary. | ||
39 | deriving (Show, Read, Eq, Ord) | ||
40 | |||
41 | instance NFData BValue where | ||
42 | rnf (BInteger i) = rnf i | ||
43 | rnf (BString s) = rnf s | ||
44 | rnf (BList l) = rnf l | ||
45 | rnf (BDict d) = rnf d | ||