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
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
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)
+