]> git.rkrishnan.org Git - functorrent.git/commitdiff
Metainfo: fix warnings and refactor
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Mon, 31 Jul 2017 02:32:04 +0000 (08:02 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Mon, 31 Jul 2017 02:32:04 +0000 (08:02 +0530)
src/FuncTorrent/Metainfo.hs

index 3f860ec1d062d3fe46b24e4e112e8eea393db959..05c9de571097142b861f88f1fd50e071ca075ca2 100644 (file)
@@ -55,52 +55,53 @@ data Metainfo = Metainfo { info :: !(Maybe Info)
                          } deriving (Eq, Show)
 
 bvalToInfo :: BVal -> Maybe Info
-bvalToInfo (Bdict m) = let (Bint pieceLength') = m ! "piece length"
-                           (Bstr pieces') = m ! "pieces"
-                           private' = Nothing
-                           (Bstr name') = m ! "name"
-                           -- is the key "files" present? If so, it is a multi-file torrent
-                           -- if not, it is a single file torrent.
-                           filesIfMulti = lookup "files" m
-                           partialInfo = Info { pieceLength = pieceLength'
-                                               , pieces = pieces'
-                                               , private = private'
-                                               , name = unpack name'
-                                               , filemeta = []
-                                               }
-                       in
-                         case filesIfMulti of
-                           Nothing -> let (Bint length') = m ! "length"
-                                          filemeta' = FileMeta { lengthInBytes = length'
-                                                               , md5sum = Nothing
-                                                               , path = unpack name' }
-                                      in Just (partialInfo { filemeta = [filemeta'] })
-                           Just (Blist files) -> mapM toFileMeta files >>=
-                                                 \filemeta' ->
-                                                   Just partialInfo { filemeta = filemeta' }
+bvalToInfo (Bdict minfo) = let (Bint pieceLength') = minfo ! "piece length"
+                               (Bstr pieces') = minfo ! "pieces"
+                               private' = Nothing
+                               (Bstr name') = minfo ! "name"
+                               -- is the key "files" present? If so, it is a multi-file torrent
+                               -- if not, it is a single file torrent.
+                               filesIfMulti = lookup "files" minfo
+                               partialInfo = Info { pieceLength = pieceLength'
+                                                  , pieces = pieces'
+                                                  , private = private'
+                                                  , name = unpack name'
+                                                  , filemeta = []
+                                                  }
+                           in
+                             case filesIfMulti of
+                               Nothing -> let (Bint length') = minfo ! "length"
+                                              filemeta' = FileMeta { lengthInBytes = length'
+                                                                   , md5sum = Nothing
+                                                                   , path = unpack name' }
+                                          in Just (partialInfo { filemeta = [filemeta'] })
+                               Just (Blist files) -> mapM toFileMeta files >>=
+                                                     \filemeta' ->
+                                                       Just partialInfo { filemeta = filemeta' }
+                               Just _ -> Nothing
 bvalToInfo _ = Nothing
 
 toFileMeta :: BVal -> Maybe FileMeta
-toFileMeta (Bdict fm) = let (Bint length) = fm ! "length"
+toFileMeta (Bdict fm) = let (Bint length') = fm ! "length"
                             (Blist pathElems) = fm ! "path"
                             pathStrings = fmap bstrToString pathElems
                         in
                           sequence pathStrings >>=
                           \pathList -> let path' = concat $ intersperse "/" pathList
-                                       in Just (FileMeta { lengthInBytes = length
+                                       in Just (FileMeta { lengthInBytes = length'
                                                          , md5sum = Nothing
                                                          , path = path' })
 toFileMeta _ = Nothing
 
 mkMetaInfo :: BVal   -> Either String Metainfo
-mkMetaInfo (Bdict m)  =
-    let info'         = bvalToInfo $ m ! "info"
-        announce'     = lookup "announce" m
-        announceList' = lookup "announce-list" m
-        creationDate' = lookup "creation date" m
-        comment'      = lookup "comment" m
-        createdBy'    = lookup "created by" m
-        encoding'     = lookup "encoding" m
+mkMetaInfo (Bdict minfo)  =
+    let info'         = bvalToInfo $ minfo ! "info"
+        announce'     = lookup "announce" minfo
+        announceList' = lookup "announce-list" minfo
+        creationDate' = lookup "creation date" minfo
+        comment'      = lookup "comment" minfo
+        createdBy'    = lookup "created by" minfo
+        encoding'     = lookup "encoding" minfo
     in Right Metainfo {
                info         = info'
              , announceList = maybeToList (announce' >>= bstrToString)
@@ -109,7 +110,7 @@ mkMetaInfo (Bdict m)  =
              , comment      = bstrToString  =<< comment'
              , createdBy    = bstrToString  =<< createdBy'
              , encoding     = bstrToString  =<< encoding'
-             , infoHash     = hash . encode $ (m ! "info")
+             , infoHash     = hash . encode $ (minfo ! "info")
              }
 mkMetaInfo _ = Left "mkMetaInfo: expect an input dict"