diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-07 23:10:27 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-07 23:10:27 +0400 |
commit | 1ae7d4877a2a30131babbee54bc0c24651eaba90 (patch) | |
tree | 1d2b6835d70a546859569933d0a34037b029d7c5 /src/Data/Torrent | |
parent | 44fc77ee51b5aef9d43e3d384845d73646ea1d0e (diff) |
Move JSON options to separate module
Diffstat (limited to 'src/Data/Torrent')
-rw-r--r-- | src/Data/Torrent/JSON.hs | 31 | ||||
-rw-r--r-- | src/Data/Torrent/Layout.hs | 6 | ||||
-rw-r--r-- | src/Data/Torrent/Piece.hs | 9 | ||||
-rw-r--r-- | src/Data/Torrent/Progress.hs | 4 |
4 files changed, 42 insertions, 8 deletions
diff --git a/src/Data/Torrent/JSON.hs b/src/Data/Torrent/JSON.hs new file mode 100644 index 00000000..3fa8973d --- /dev/null +++ b/src/Data/Torrent/JSON.hs | |||
@@ -0,0 +1,31 @@ | |||
1 | module Data.Torrent.JSON | ||
2 | ( omitLensPrefix | ||
3 | , omitRecordPrefix | ||
4 | ) where | ||
5 | |||
6 | import Data.Aeson.TH | ||
7 | import Data.Char | ||
8 | import Data.List as L | ||
9 | |||
10 | |||
11 | -- | Ignore '_' prefix. | ||
12 | omitLensPrefix :: Options | ||
13 | omitLensPrefix = defaultOptions | ||
14 | { fieldLabelModifier = L.dropWhile (== '_') | ||
15 | , constructorTagModifier = id | ||
16 | , allNullaryToStringTag = True | ||
17 | , omitNothingFields = True | ||
18 | } | ||
19 | |||
20 | mapWhile :: (a -> Bool) -> (a -> a) -> [a] -> [a] | ||
21 | mapWhile p f = go | ||
22 | where | ||
23 | go [] = [] | ||
24 | go (x : xs) | ||
25 | | p x = f x : go xs | ||
26 | | otherwise = xs | ||
27 | |||
28 | omitRecordPrefix :: Options | ||
29 | omitRecordPrefix = omitLensPrefix | ||
30 | { fieldLabelModifier = mapWhile isUpper toLower . L.dropWhile isLower | ||
31 | } \ No newline at end of file | ||
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index a32d74fa..453c0d4f 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs | |||
@@ -70,7 +70,6 @@ import Data.BEncode.Types | |||
70 | import Data.ByteString as BS | 70 | import Data.ByteString as BS |
71 | import Data.ByteString.Base16 as Base16 | 71 | import Data.ByteString.Base16 as Base16 |
72 | import Data.ByteString.Char8 as BC | 72 | import Data.ByteString.Char8 as BC |
73 | import Data.Char as Char | ||
74 | import Data.Foldable as F | 73 | import Data.Foldable as F |
75 | import Data.List as L | 74 | import Data.List as L |
76 | import Data.Text as T | 75 | import Data.Text as T |
@@ -81,6 +80,7 @@ import Text.PrettyPrint.Class | |||
81 | import System.FilePath | 80 | import System.FilePath |
82 | import System.Posix.Types | 81 | import System.Posix.Types |
83 | 82 | ||
83 | import Data.Torrent.JSON | ||
84 | 84 | ||
85 | {----------------------------------------------------------------------- | 85 | {----------------------------------------------------------------------- |
86 | -- File attribytes | 86 | -- File attribytes |
@@ -123,7 +123,7 @@ data FileInfo a = FileInfo { | |||
123 | , Functor, Foldable | 123 | , Functor, Foldable |
124 | ) | 124 | ) |
125 | 125 | ||
126 | $(deriveJSON defaultOptions { fieldLabelModifier = (L.map Char.toLower . L.dropWhile isLower) } ''FileInfo) | 126 | $(deriveJSON omitRecordPrefix ''FileInfo) |
127 | 127 | ||
128 | makeLensesFor | 128 | makeLensesFor |
129 | [ ("fiLength", "fileLength") | 129 | [ ("fiLength", "fileLength") |
@@ -208,7 +208,7 @@ data LayoutInfo | |||
208 | , liDirName :: !ByteString | 208 | , liDirName :: !ByteString |
209 | } deriving (Show, Read, Eq, Typeable) | 209 | } deriving (Show, Read, Eq, Typeable) |
210 | 210 | ||
211 | $(deriveJSON defaultOptions { fieldLabelModifier = (L.map Char.toLower . L.dropWhile isLower) } ''LayoutInfo) | 211 | $(deriveJSON omitRecordPrefix ''LayoutInfo) |
212 | 212 | ||
213 | makeLensesFor | 213 | makeLensesFor |
214 | [ ("liFile" , "singleFile" ) | 214 | [ ("liFile" , "singleFile" ) |
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index 00d4b843..d79da2ee 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs | |||
@@ -54,14 +54,15 @@ import Data.Bits.Extras | |||
54 | import Data.ByteString as BS | 54 | import Data.ByteString as BS |
55 | import qualified Data.ByteString.Lazy as BL | 55 | import qualified Data.ByteString.Lazy as BL |
56 | import qualified Data.ByteString.Base64 as Base64 | 56 | import qualified Data.ByteString.Base64 as Base64 |
57 | import Data.Char | ||
58 | import Data.Int | 57 | import Data.Int |
59 | import Data.List as L | ||
60 | import Data.Text.Encoding as T | 58 | import Data.Text.Encoding as T |
61 | import Data.Typeable | 59 | import Data.Typeable |
62 | import Text.PrettyPrint | 60 | import Text.PrettyPrint |
63 | import Text.PrettyPrint.Class | 61 | import Text.PrettyPrint.Class |
64 | 62 | ||
63 | import Data.Torrent.JSON | ||
64 | |||
65 | |||
65 | -- TODO add torrent file validation | 66 | -- TODO add torrent file validation |
66 | class Lint a where | 67 | class Lint a where |
67 | lint :: a -> Either String a | 68 | lint :: a -> Either String a |
@@ -129,7 +130,7 @@ data Piece a = Piece | |||
129 | , pieceData :: !a | 130 | , pieceData :: !a |
130 | } deriving (Show, Read, Eq, Functor, Typeable) | 131 | } deriving (Show, Read, Eq, Functor, Typeable) |
131 | 132 | ||
132 | $(deriveJSON defaultOptions { fieldLabelModifier = (L.map toLower . L.dropWhile isLower) } ''Piece) | 133 | $(deriveJSON omitRecordPrefix ''Piece) |
133 | 134 | ||
134 | instance NFData (Piece a) | 135 | instance NFData (Piece a) |
135 | 136 | ||
@@ -166,7 +167,7 @@ data PieceInfo = PieceInfo | |||
166 | -- ^ Concatenation of all 20-byte SHA1 hash values. | 167 | -- ^ Concatenation of all 20-byte SHA1 hash values. |
167 | } deriving (Show, Read, Eq, Typeable) | 168 | } deriving (Show, Read, Eq, Typeable) |
168 | 169 | ||
169 | $(deriveJSON defaultOptions { fieldLabelModifier = (L.map toLower . L.dropWhile isLower) } ''PieceInfo) | 170 | $(deriveJSON omitRecordPrefix ''PieceInfo) |
170 | 171 | ||
171 | -- | Number of bytes in each piece. | 172 | -- | Number of bytes in each piece. |
172 | makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo | 173 | makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo |
diff --git a/src/Data/Torrent/Progress.hs b/src/Data/Torrent/Progress.hs index 34f8f299..ffcbf2aa 100644 --- a/src/Data/Torrent/Progress.hs +++ b/src/Data/Torrent/Progress.hs | |||
@@ -49,6 +49,8 @@ import Network.HTTP.Types.QueryLike | |||
49 | import Text.PrettyPrint as PP | 49 | import Text.PrettyPrint as PP |
50 | import Text.PrettyPrint.Class | 50 | import Text.PrettyPrint.Class |
51 | 51 | ||
52 | import Data.Torrent.JSON | ||
53 | |||
52 | 54 | ||
53 | -- | Progress data is considered as dynamic within one client | 55 | -- | Progress data is considered as dynamic within one client |
54 | -- session. This data also should be shared across client application | 56 | -- session. This data also should be shared across client application |
@@ -62,7 +64,7 @@ data Progress = Progress | |||
62 | } deriving (Show, Read, Eq) | 64 | } deriving (Show, Read, Eq) |
63 | 65 | ||
64 | $(makeLenses ''Progress) | 66 | $(makeLenses ''Progress) |
65 | $(deriveJSON defaultOptions { fieldLabelModifier = L.tail } ''Progress) | 67 | $(deriveJSON omitLensPrefix ''Progress) |
66 | 68 | ||
67 | -- | UDP tracker compatible encoding. | 69 | -- | UDP tracker compatible encoding. |
68 | instance Serialize Progress where | 70 | instance Serialize Progress where |