diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Data/Torrent.hs | 8 | ||||
-rw-r--r-- | src/Data/Torrent/Layout.hs | 5 | ||||
-rw-r--r-- | src/Data/Torrent/Piece.hs | 8 |
3 files changed, 21 insertions, 0 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 7d3917f2..7833c261 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -140,6 +140,10 @@ instance Hashable InfoDict where | |||
140 | hashWithSalt = Hashable.hashUsing idInfoHash | 140 | hashWithSalt = Hashable.hashUsing idInfoHash |
141 | {-# INLINE hashWithSalt #-} | 141 | {-# INLINE hashWithSalt #-} |
142 | 142 | ||
143 | -- | Empty info dictionary with zero-length content. | ||
144 | instance Default InfoDict where | ||
145 | def = infoDictionary def def False | ||
146 | |||
143 | -- | Smart constructor: add a info hash to info dictionary. | 147 | -- | Smart constructor: add a info hash to info dictionary. |
144 | infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict | 148 | infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict |
145 | infoDictionary li pinfo private = InfoDict ih li pinfo private | 149 | infoDictionary li pinfo private = InfoDict ih li pinfo private |
@@ -340,6 +344,10 @@ instance Pretty Torrent where | |||
340 | "Publisher URL" <:>? ((text . show) <$> tPublisherURL) $$ | 344 | "Publisher URL" <:>? ((text . show) <$> tPublisherURL) $$ |
341 | "Signature" <:>? ((text . show) <$> tSignature) | 345 | "Signature" <:>? ((text . show) <$> tSignature) |
342 | 346 | ||
347 | -- | No files, no trackers, no nodes, etc... | ||
348 | instance Default Torrent where | ||
349 | def = nullTorrent def | ||
350 | |||
343 | -- | A simple torrent contains only required fields. | 351 | -- | A simple torrent contains only required fields. |
344 | nullTorrent :: InfoDict -> Torrent | 352 | nullTorrent :: InfoDict -> Torrent |
345 | nullTorrent info = Torrent | 353 | nullTorrent info = Torrent |
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index a4b55b3d..ef8d45eb 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs | |||
@@ -71,6 +71,7 @@ import Data.BEncode.Types | |||
71 | import Data.ByteString as BS | 71 | import Data.ByteString as BS |
72 | import Data.ByteString.Base16 as Base16 | 72 | import Data.ByteString.Base16 as Base16 |
73 | import Data.ByteString.Char8 as BC | 73 | import Data.ByteString.Char8 as BC |
74 | import Data.Default | ||
74 | import Data.Foldable as F | 75 | import Data.Foldable as F |
75 | import Data.List as L | 76 | import Data.List as L |
76 | import Data.Text as T | 77 | import Data.Text as T |
@@ -222,6 +223,10 @@ instance NFData LayoutInfo where | |||
222 | rnf SingleFile {..} = () | 223 | rnf SingleFile {..} = () |
223 | rnf MultiFile {..} = rnf liFiles | 224 | rnf MultiFile {..} = rnf liFiles |
224 | 225 | ||
226 | -- | Empty multifile layout. | ||
227 | instance Default LayoutInfo where | ||
228 | def = MultiFile [] "" | ||
229 | |||
225 | getLayoutInfo :: Get LayoutInfo | 230 | getLayoutInfo :: Get LayoutInfo |
226 | getLayoutInfo = single <|> multi | 231 | getLayoutInfo = single <|> multi |
227 | where | 232 | where |
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index 74a2469a..03335819 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs | |||
@@ -56,6 +56,7 @@ import Data.Bits.Extras | |||
56 | import Data.ByteString as BS | 56 | import Data.ByteString as BS |
57 | import qualified Data.ByteString.Lazy as BL | 57 | import qualified Data.ByteString.Lazy as BL |
58 | import qualified Data.ByteString.Base64 as Base64 | 58 | import qualified Data.ByteString.Base64 as Base64 |
59 | import Data.Default | ||
59 | import Data.Int | 60 | import Data.Int |
60 | import Data.Text.Encoding as T | 61 | import Data.Text.Encoding as T |
61 | import Data.Typeable | 62 | import Data.Typeable |
@@ -170,6 +171,10 @@ instance FromJSON HashList where | |||
170 | parseJSON = withText "HashArray" $ | 171 | parseJSON = withText "HashArray" $ |
171 | either fail (return . HashList) . Base64.decode . T.encodeUtf8 | 172 | either fail (return . HashList) . Base64.decode . T.encodeUtf8 |
172 | 173 | ||
174 | -- | Empty hash list. | ||
175 | instance Default HashList where | ||
176 | def = HashList "" | ||
177 | |||
173 | -- | Part of torrent file used for torrent content validation. | 178 | -- | Part of torrent file used for torrent content validation. |
174 | data PieceInfo = PieceInfo | 179 | data PieceInfo = PieceInfo |
175 | { piPieceLength :: {-# UNPACK #-} !PieceSize | 180 | { piPieceLength :: {-# UNPACK #-} !PieceSize |
@@ -189,6 +194,9 @@ makeLensesFor [("piPieceHashes", "pieceHashes")] ''PieceInfo | |||
189 | 194 | ||
190 | instance NFData PieceInfo | 195 | instance NFData PieceInfo |
191 | 196 | ||
197 | instance Default PieceInfo where | ||
198 | def = PieceInfo 1 def | ||
199 | |||
192 | instance Lint PieceInfo where | 200 | instance Lint PieceInfo where |
193 | lint pinfo @ PieceInfo {..} | 201 | lint pinfo @ PieceInfo {..} |
194 | | BS.length (unHashList piPieceHashes) `rem` hashsize == 0 | 202 | | BS.length (unHashList piPieceHashes) `rem` hashsize == 0 |