From d484812bb4953c6f83cbdce10fd62403947fb8f5 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Mon, 31 Jul 2017 08:02:04 +0530
Subject: [PATCH] Metainfo: fix warnings and refactor

---
 src/FuncTorrent/Metainfo.hs | 69 +++++++++++++++++++------------------
 1 file changed, 35 insertions(+), 34 deletions(-)

diff --git a/src/FuncTorrent/Metainfo.hs b/src/FuncTorrent/Metainfo.hs
index 3f860ec..05c9de5 100644
--- a/src/FuncTorrent/Metainfo.hs
+++ b/src/FuncTorrent/Metainfo.hs
@@ -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"
 
-- 
2.45.2