1. tomas curbina
  2. vimclojure

Commits

Meikel Brandmeyer  committed 9bfdc19

Add fromBencode converter

  • Participants
  • Parent commits adb40fa
  • Branches default

Comments (0)

Files changed (1)

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

View file
  • Ignore whitespace
 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