module GHCi.DAP where

import Data.Word
import qualified Data.Text as T
import qualified Text.Read as R
import qualified Data.Text.Encoding as TE
import qualified Data.ByteString as BS

-- |

--   Encode DAP Request Argument to [Word8] Show String.

--

encode :: Show a
       => a
       -> String
encode :: forall a. Show a => a -> String
encode = forall a. Show a => a -> String
show forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BS.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
TE.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show


-- |

--   Decode [Word8] Show String to DAP Request Argument.

--

decode :: Read a
       => String
       -> Either String a
decode :: forall a. Read a => String -> Either String a
decode String
argsStr = case forall a. Read a => String -> Either String a
R.readEither String
argsStr :: Either String [Word8] of
  Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ String
"read [Word8] failed. " forall a. [a] -> [a] -> [a]
++ String
err forall a. [a] -> [a] -> [a]
++ String
" : " forall a. [a] -> [a] -> [a]
++ String
argsStr
  Right [Word8]
bs -> case forall a. Read a => String -> Either String a
R.readEither ([Word8] -> String
toStr [Word8]
bs) of
    Left String
err -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ String
"read failed. " forall a. [a] -> [a] -> [a]
++ String
err forall a. [a] -> [a] -> [a]
++ String
" : " forall a. [a] -> [a] -> [a]
++  ([Word8] -> String
toStr [Word8]
bs)
    Right a
a  -> forall a b. b -> Either a b
Right a
a
  where
    toStr :: [Word8] -> String
toStr = Text -> String
T.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TE.decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> ByteString
BS.pack