Meikel  Brandmeyer avatar Meikel Brandmeyer committed 9bfdc19

Add fromBencode converter

Comments (0)

Files changed (1)

nrepl-client/src/main/haskell/VimClojure/Bencode.hs

 readBencode,
 writeBencode,
 toBencode,
-fromBencode
+fromBencode,
+IsBencodeReadable,
+IsBencodeWritable
 ) where
 
 import Prelude hiding (readList)
     | BList [Bencode]
     | BMap (M.Map B.ByteString Bencode)
 
+class IsBencodeReadable a where
+    fromBencode     :: Bencode -> a
+    fromBencodeList :: Bencode -> [a]
+    fromBencodeList (BList l) = map fromBencode l
+
+instance IsBencodeReadable Char where
+    fromBencode     (BString s) = head $ B.unpack s
+    fromBencodeList (BString s) = B.unpack s
+
+instance IsBencodeReadable Int where
+    fromBencode (BInt n) = n
+
+instance (IsBencodeReadable a) => IsBencodeReadable [a] where
+    fromBencode = fromBencodeList
+
+mapEntryFromBencode (k, v) = (B.unpack k, fromBencode v)
+
+instance (IsBencodeReadable a) => IsBencodeReadable (M.Map [Char] a) where
+    fromBencode (BMap m) = M.fromList $ map mapEntryFromBencode $ toList m
+
 readUntil delim readToken stream = do
     ch <- hLookAhead stream
     if ch == delim then do
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.