From: Ramakrishnan Muthukrishnan Date: Mon, 31 Jul 2017 17:18:06 +0000 (+0530) Subject: More consistency of types between magneturi/metainfo parsing X-Git-Url: https://git.rkrishnan.org/vdrive/%22news.html/%22doc.html/frontends/about.html?a=commitdiff_plain;h=ee331360e1f6585f79218c56d73ed7406e9527bf;p=functorrent.git More consistency of types between magneturi/metainfo parsing --- diff --git a/src/FuncTorrent/MagnetURI.hs b/src/FuncTorrent/MagnetURI.hs index 7a9b20e..506a169 100644 --- a/src/FuncTorrent/MagnetURI.hs +++ b/src/FuncTorrent/MagnetURI.hs @@ -69,13 +69,17 @@ magnetBody = do pairs <- kvpair `sepBy1` (char '&') -- walk through pairs, populate Magnetinfo (fold?) return $ foldl f magnetInfo pairs - where f magnetRecord pair = let (k, v) = pair in - case k of - "xt" -> magnetRecord { infoHash = pack v } - "tr" -> let trVal = trackerlist magnetRecord in - magnetRecord { trackerlist = trVal ++ [urlDecode v] } - "dn" -> magnetRecord { name = urlDecode v } - "xl" -> magnetRecord { xlen = Just (read v :: Integer) } + where f magnetRecord pair = + let (k, v) = pair + in + case k of + "xt" -> magnetRecord { infoHash = pack v } + "tr" -> let trVal = trackerlist magnetRecord + in + magnetRecord { trackerlist = trVal ++ [urlDecode v] } + "dn" -> magnetRecord { name = urlDecode v } + "xl" -> magnetRecord { xlen = Just (read v :: Integer) } + _ -> magnetInfo magnetInfo = Magnetinfo { infoHash = mempty , trackerlist = mempty , name = mempty @@ -84,5 +88,8 @@ magnetBody = do magnetUri :: ParsecBS.Parser Magnetinfo magnetUri = magnetHdr >> magnetBody -parseMagneturi :: ByteString -> Either ParseError Magnetinfo -parseMagneturi input = parse magnetUri "magnetParse" input +parseMagneturi :: ByteString -> Either String Magnetinfo +parseMagneturi input = + case parse magnetUri "magnetParse" input of + Right minfo -> Right minfo + Left e -> Left $ "Cannot parse the magnet URI: " ++ show e diff --git a/src/FuncTorrent/Metainfo.hs b/src/FuncTorrent/Metainfo.hs index 05c9de5..9354082 100644 --- a/src/FuncTorrent/Metainfo.hs +++ b/src/FuncTorrent/Metainfo.hs @@ -129,5 +129,6 @@ getAnnounceList (Just (Bdict _)) = [] torrentToMetainfo :: ByteString -> Either String Metainfo torrentToMetainfo s = case decode s of - Right d -> mkMetaInfo d - Left e -> Left $ show e + Right d -> mkMetaInfo d + Left e -> Left (show "Cannot parse the torrent file: " ++ show e) +