summaryrefslogtreecommitdiff
path: root/src/Data/Torrent
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-07 23:10:27 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-07 23:10:27 +0400
commit1ae7d4877a2a30131babbee54bc0c24651eaba90 (patch)
tree1d2b6835d70a546859569933d0a34037b029d7c5 /src/Data/Torrent
parent44fc77ee51b5aef9d43e3d384845d73646ea1d0e (diff)
Move JSON options to separate module
Diffstat (limited to 'src/Data/Torrent')
-rw-r--r--src/Data/Torrent/JSON.hs31
-rw-r--r--src/Data/Torrent/Layout.hs6
-rw-r--r--src/Data/Torrent/Piece.hs9
-rw-r--r--src/Data/Torrent/Progress.hs4
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 @@
1module Data.Torrent.JSON
2 ( omitLensPrefix
3 , omitRecordPrefix
4 ) where
5
6import Data.Aeson.TH
7import Data.Char
8import Data.List as L
9
10
11-- | Ignore '_' prefix.
12omitLensPrefix :: Options
13omitLensPrefix = defaultOptions
14 { fieldLabelModifier = L.dropWhile (== '_')
15 , constructorTagModifier = id
16 , allNullaryToStringTag = True
17 , omitNothingFields = True
18 }
19
20mapWhile :: (a -> Bool) -> (a -> a) -> [a] -> [a]
21mapWhile p f = go
22 where
23 go [] = []
24 go (x : xs)
25 | p x = f x : go xs
26 | otherwise = xs
27
28omitRecordPrefix :: Options
29omitRecordPrefix = 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
70import Data.ByteString as BS 70import Data.ByteString as BS
71import Data.ByteString.Base16 as Base16 71import Data.ByteString.Base16 as Base16
72import Data.ByteString.Char8 as BC 72import Data.ByteString.Char8 as BC
73import Data.Char as Char
74import Data.Foldable as F 73import Data.Foldable as F
75import Data.List as L 74import Data.List as L
76import Data.Text as T 75import Data.Text as T
@@ -81,6 +80,7 @@ import Text.PrettyPrint.Class
81import System.FilePath 80import System.FilePath
82import System.Posix.Types 81import System.Posix.Types
83 82
83import 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
128makeLensesFor 128makeLensesFor
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
213makeLensesFor 213makeLensesFor
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
54import Data.ByteString as BS 54import Data.ByteString as BS
55import qualified Data.ByteString.Lazy as BL 55import qualified Data.ByteString.Lazy as BL
56import qualified Data.ByteString.Base64 as Base64 56import qualified Data.ByteString.Base64 as Base64
57import Data.Char
58import Data.Int 57import Data.Int
59import Data.List as L
60import Data.Text.Encoding as T 58import Data.Text.Encoding as T
61import Data.Typeable 59import Data.Typeable
62import Text.PrettyPrint 60import Text.PrettyPrint
63import Text.PrettyPrint.Class 61import Text.PrettyPrint.Class
64 62
63import Data.Torrent.JSON
64
65
65-- TODO add torrent file validation 66-- TODO add torrent file validation
66class Lint a where 67class 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
134instance NFData (Piece a) 135instance 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.
172makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo 173makeLensesFor [("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
49import Text.PrettyPrint as PP 49import Text.PrettyPrint as PP
50import Text.PrettyPrint.Class 50import Text.PrettyPrint.Class
51 51
52import 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.
68instance Serialize Progress where 70instance Serialize Progress where