summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-04-04 06:01:59 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-04-04 06:01:59 +0400
commit47e56d698e2c8837656600561874915cf40c0d4e (patch)
tree90fbedc04c58d6516be0d1d3214a4349360b6a12 /src/Data
parent49628a6be68a495ab846218bc6b7581c01c1bc69 (diff)
Remove aeson dependency
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Torrent.hs20
-rw-r--r--src/Data/Torrent/InfoHash.hs10
-rw-r--r--src/Data/Torrent/JSON.hs56
-rw-r--r--src/Data/Torrent/Layout.hs10
-rw-r--r--src/Data/Torrent/Piece.hs16
-rw-r--r--src/Data/Torrent/Progress.hs4
6 files changed, 0 insertions, 116 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index ccd5e666..b233937b 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -71,8 +71,6 @@ import qualified Crypto.Hash.SHA1 as C
71import Control.DeepSeq 71import Control.DeepSeq
72import Control.Exception 72import Control.Exception
73import Control.Lens 73import Control.Lens
74import Data.Aeson.Types (ToJSON(..), FromJSON(..), Value(..), withText)
75import Data.Aeson.TH
76import Data.BEncode as BE 74import Data.BEncode as BE
77import Data.BEncode.Types as BE 75import Data.BEncode.Types as BE
78import Data.ByteString as BS 76import Data.ByteString as BS
@@ -93,7 +91,6 @@ import Text.PrettyPrint.Class
93import System.FilePath 91import System.FilePath
94 92
95import Data.Torrent.InfoHash as IH 93import Data.Torrent.InfoHash as IH
96import Data.Torrent.JSON
97import Data.Torrent.Layout 94import Data.Torrent.Layout
98import Data.Torrent.Piece 95import Data.Torrent.Piece
99import Network.BitTorrent.Core.NodeInfo 96import Network.BitTorrent.Core.NodeInfo
@@ -125,8 +122,6 @@ data InfoDict = InfoDict
125 -- BEP 27: <http://www.bittorrent.org/beps/bep_0027.html> 122 -- BEP 27: <http://www.bittorrent.org/beps/bep_0027.html>
126 } deriving (Show, Read, Eq, Typeable) 123 } deriving (Show, Read, Eq, Typeable)
127 124
128$(deriveJSON omitRecordPrefix ''InfoDict)
129
130makeLensesFor 125makeLensesFor
131 [ ("idInfoHash" , "infohash" ) 126 [ ("idInfoHash" , "infohash" )
132 , ("idLayoutInfo", "layoutInfo") 127 , ("idLayoutInfo", "layoutInfo")
@@ -242,21 +237,6 @@ data Torrent = Torrent
242 -- encrypted SHA-1 hash of the info dictionary). 237 -- encrypted SHA-1 hash of the info dictionary).
243 } deriving (Show, Eq, Typeable) 238 } deriving (Show, Eq, Typeable)
244 239
245instance FromJSON URI where
246 parseJSON = withText "URI" $
247 maybe (fail "could not parse URI") pure . parseURI . T.unpack
248
249instance ToJSON URI where
250 toJSON = String . T.pack . show
251
252instance ToJSON NominalDiffTime where
253 toJSON = toJSON . posixSecondsToUTCTime
254
255instance FromJSON NominalDiffTime where
256 parseJSON v = utcTimeToPOSIXSeconds <$> parseJSON v
257
258$(deriveJSON omitRecordPrefix ''Torrent)
259
260makeLensesFor 240makeLensesFor
261 [ ("tAnnounce" , "announce" ) 241 [ ("tAnnounce" , "announce" )
262 , ("tAnnounceList", "announceList") 242 , ("tAnnounceList", "announceList")
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index 9eec631c..f322ac6f 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -24,7 +24,6 @@ module Data.Torrent.InfoHash
24 24
25import Control.Applicative 25import Control.Applicative
26import Control.Monad 26import Control.Monad
27import Data.Aeson
28import Data.BEncode 27import Data.BEncode
29import Data.ByteString as BS 28import Data.ByteString as BS
30import Data.ByteString.Char8 as BC 29import Data.ByteString.Char8 as BC
@@ -146,15 +145,6 @@ instance Convertible Text InfoHash where
146instance IsString InfoHash where 145instance IsString InfoHash where
147 fromString = either (error . prettyConvertError) id . safeConvert . T.pack 146 fromString = either (error . prettyConvertError) id . safeConvert . T.pack
148 147
149-- | Convert to base16 encoded JSON string.
150instance ToJSON InfoHash where
151 toJSON (InfoHash ih) = String $ T.decodeUtf8 $ Base16.encode ih
152
153-- | Convert from base16\/base32\/base64 encoded JSON string.
154instance FromJSON InfoHash where
155 parseJSON = withText "InfoHash" $
156 either (fail . prettyConvertError) pure . safeConvert
157
158ignoreErrorMsg :: Either a b -> Maybe b 148ignoreErrorMsg :: Either a b -> Maybe b
159ignoreErrorMsg = either (const Nothing) Just 149ignoreErrorMsg = either (const Nothing) Just
160 150
diff --git a/src/Data/Torrent/JSON.hs b/src/Data/Torrent/JSON.hs
deleted file mode 100644
index 845b288a..00000000
--- a/src/Data/Torrent/JSON.hs
+++ /dev/null
@@ -1,56 +0,0 @@
1{-# OPTIONS_GHC -fno-warn-orphans #-}
2module Data.Torrent.JSON
3 ( omitLensPrefix
4 , omitRecordPrefix
5 ) where
6
7import Control.Applicative
8import Data.Aeson.TH
9import Data.Aeson.Types
10import Data.ByteString as BS
11import Data.ByteString.Base16 as Base16
12import Data.Char
13import Data.IP
14import Data.List as L
15import Data.Text.Encoding as T
16
17
18-- | Ignore '_' prefix.
19omitLensPrefix :: Options
20omitLensPrefix = defaultOptions
21 { fieldLabelModifier = L.dropWhile (== '_')
22 , constructorTagModifier = id
23 , allNullaryToStringTag = True
24 , omitNothingFields = True
25 }
26
27mapWhile :: (a -> Bool) -> (a -> a) -> [a] -> [a]
28mapWhile p f = go
29 where
30 go [] = []
31 go (x : xs)
32 | p x = f x : go xs
33 | otherwise = x : xs
34
35omitRecordPrefix :: Options
36omitRecordPrefix = omitLensPrefix
37 { fieldLabelModifier = mapWhile isUpper toLower . L.dropWhile isLower
38 }
39
40instance ToJSON ByteString where
41 toJSON = String . T.decodeUtf8 . Base16.encode
42
43instance FromJSON ByteString where
44 parseJSON v = do
45 (ok, bad) <- (Base16.decode . T.encodeUtf8) <$> parseJSON v
46 if BS.null bad
47 then return ok
48 else fail "parseJSON: unable to decode ByteString"
49
50instance ToJSON IP where
51 toJSON = toJSON . show
52
53instance FromJSON IP where
54 parseJSON v = do
55 str <- parseJSON v
56 return $ read str
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs
index ef8d45eb..cc529840 100644
--- a/src/Data/Torrent/Layout.hs
+++ b/src/Data/Torrent/Layout.hs
@@ -64,8 +64,6 @@ module Data.Torrent.Layout
64import Control.Applicative 64import Control.Applicative
65import Control.DeepSeq 65import Control.DeepSeq
66import Control.Lens 66import Control.Lens
67import Data.Aeson.TH
68import Data.Aeson.Types (FromJSON, ToJSON)
69import Data.BEncode 67import Data.BEncode
70import Data.BEncode.Types 68import Data.BEncode.Types
71import Data.ByteString as BS 69import Data.ByteString as BS
@@ -82,8 +80,6 @@ import Text.PrettyPrint.Class
82import System.FilePath 80import System.FilePath
83import System.Posix.Types 81import System.Posix.Types
84 82
85import Data.Torrent.JSON
86
87{----------------------------------------------------------------------- 83{-----------------------------------------------------------------------
88-- File attribytes 84-- File attribytes
89-----------------------------------------------------------------------} 85-----------------------------------------------------------------------}
@@ -91,8 +87,6 @@ import Data.Torrent.JSON
91-- | Size of a file in bytes. 87-- | Size of a file in bytes.
92type FileSize = FileOffset 88type FileSize = FileOffset
93 89
94deriving instance FromJSON FileOffset
95deriving instance ToJSON FileOffset
96deriving instance BEncode FileOffset 90deriving instance BEncode FileOffset
97 91
98{----------------------------------------------------------------------- 92{-----------------------------------------------------------------------
@@ -125,8 +119,6 @@ data FileInfo a = FileInfo {
125 , Functor, Foldable 119 , Functor, Foldable
126 ) 120 )
127 121
128$(deriveJSON omitRecordPrefix ''FileInfo)
129
130makeLensesFor 122makeLensesFor
131 [ ("fiLength", "fileLength") 123 [ ("fiLength", "fileLength")
132 , ("fiMD5Sum", "fileMD5Sum") 124 , ("fiMD5Sum", "fileMD5Sum")
@@ -210,8 +202,6 @@ data LayoutInfo
210 , liDirName :: !ByteString 202 , liDirName :: !ByteString
211 } deriving (Show, Read, Eq, Typeable) 203 } deriving (Show, Read, Eq, Typeable)
212 204
213$(deriveJSON omitRecordPrefix ''LayoutInfo)
214
215makeLensesFor 205makeLensesFor
216 [ ("liFile" , "singleFile" ) 206 [ ("liFile" , "singleFile" )
217 , ("liFiles" , "multiFile" ) 207 , ("liFiles" , "multiFile" )
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs
index 03335819..d4b2c399 100644
--- a/src/Data/Torrent/Piece.hs
+++ b/src/Data/Torrent/Piece.hs
@@ -47,8 +47,6 @@ module Data.Torrent.Piece
47import Control.DeepSeq 47import Control.DeepSeq
48import Control.Lens 48import Control.Lens
49import qualified Crypto.Hash.SHA1 as SHA1 49import qualified Crypto.Hash.SHA1 as SHA1
50import Data.Aeson (ToJSON(..), FromJSON(..), Value(..), withText)
51import Data.Aeson.TH
52import Data.BEncode 50import Data.BEncode
53import Data.BEncode.Types 51import Data.BEncode.Types
54import Data.Bits 52import Data.Bits
@@ -63,8 +61,6 @@ import Data.Typeable
63import Text.PrettyPrint 61import Text.PrettyPrint
64import Text.PrettyPrint.Class 62import Text.PrettyPrint.Class
65 63
66import Data.Torrent.JSON
67
68 64
69-- TODO add torrent file validation 65-- TODO add torrent file validation
70class Lint a where 66class Lint a where
@@ -139,8 +135,6 @@ data Piece a = Piece
139 , pieceData :: !a 135 , pieceData :: !a
140 } deriving (Show, Read, Eq, Functor, Typeable) 136 } deriving (Show, Read, Eq, Functor, Typeable)
141 137
142$(deriveJSON omitRecordPrefix ''Piece)
143
144instance NFData (Piece a) 138instance NFData (Piece a)
145 139
146-- | Payload bytes are omitted. 140-- | Payload bytes are omitted.
@@ -163,14 +157,6 @@ hashPiece Piece {..} = SHA1.hashlazy pieceData
163newtype HashList = HashList { unHashList :: ByteString } 157newtype HashList = HashList { unHashList :: ByteString }
164 deriving (Show, Read, Eq, BEncode, Typeable) 158 deriving (Show, Read, Eq, BEncode, Typeable)
165 159
166-- | Represented as base64 encoded JSON string.
167instance ToJSON HashList where
168 toJSON (HashList bs) = String $ T.decodeUtf8 $ Base64.encode bs
169
170instance FromJSON HashList where
171 parseJSON = withText "HashArray" $
172 either fail (return . HashList) . Base64.decode . T.encodeUtf8
173
174-- | Empty hash list. 160-- | Empty hash list.
175instance Default HashList where 161instance Default HashList where
176 def = HashList "" 162 def = HashList ""
@@ -184,8 +170,6 @@ data PieceInfo = PieceInfo
184 -- ^ Concatenation of all 20-byte SHA1 hash values. 170 -- ^ Concatenation of all 20-byte SHA1 hash values.
185 } deriving (Show, Read, Eq, Typeable) 171 } deriving (Show, Read, Eq, Typeable)
186 172
187$(deriveJSON omitRecordPrefix ''PieceInfo)
188
189-- | Number of bytes in each piece. 173-- | Number of bytes in each piece.
190makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo 174makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo
191 175
diff --git a/src/Data/Torrent/Progress.hs b/src/Data/Torrent/Progress.hs
index ffcbf2aa..4719020a 100644
--- a/src/Data/Torrent/Progress.hs
+++ b/src/Data/Torrent/Progress.hs
@@ -36,7 +36,6 @@ module Data.Torrent.Progress
36 36
37import Control.Applicative 37import Control.Applicative
38import Control.Lens hiding ((%=)) 38import Control.Lens hiding ((%=))
39import Data.Aeson.TH
40import Data.ByteString.Lazy.Builder as BS 39import Data.ByteString.Lazy.Builder as BS
41import Data.ByteString.Lazy.Builder.ASCII as BS 40import Data.ByteString.Lazy.Builder.ASCII as BS
42import Data.Default 41import Data.Default
@@ -49,8 +48,6 @@ import Network.HTTP.Types.QueryLike
49import Text.PrettyPrint as PP 48import Text.PrettyPrint as PP
50import Text.PrettyPrint.Class 49import Text.PrettyPrint.Class
51 50
52import Data.Torrent.JSON
53
54 51
55-- | Progress data is considered as dynamic within one client 52-- | Progress data is considered as dynamic within one client
56-- session. This data also should be shared across client application 53-- session. This data also should be shared across client application
@@ -64,7 +61,6 @@ data Progress = Progress
64 } deriving (Show, Read, Eq) 61 } deriving (Show, Read, Eq)
65 62
66$(makeLenses ''Progress) 63$(makeLenses ''Progress)
67$(deriveJSON omitLensPrefix ''Progress)
68 64
69-- | UDP tracker compatible encoding. 65-- | UDP tracker compatible encoding.
70instance Serialize Progress where 66instance Serialize Progress where