From: Ramakrishnan Muthukrishnan Date: Mon, 31 Jul 2017 02:32:04 +0000 (+0530) Subject: Metainfo: fix warnings and refactor X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/%22doc.html/install.html?a=commitdiff_plain;h=d484812bb4953c6f83cbdce10fd62403947fb8f5;p=functorrent.git Metainfo: fix warnings and refactor --- 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"