From ee331360e1f6585f79218c56d73ed7406e9527bf Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org> Date: Mon, 31 Jul 2017 22:48:06 +0530 Subject: [PATCH] More consistency of types between magneturi/metainfo parsing --- src/FuncTorrent/MagnetURI.hs | 25 ++++++++++++++++--------- src/FuncTorrent/Metainfo.hs | 5 +++-- 2 files changed, 19 insertions(+), 11 deletions(-) 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) + -- 2.45.2