module Language.LSP.Client.Encoding where

import Data.Aeson (ToJSON)
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy (LazyByteString)
import Data.ByteString.Lazy.Char8 qualified as LazyByteString
import Prelude

addHeader :: LazyByteString -> LazyByteString
addHeader :: LazyByteString -> LazyByteString
addHeader LazyByteString
content =
    [LazyByteString] -> LazyByteString
forall a. Monoid a => [a] -> a
mconcat
        [ LazyByteString
"Content-Length: "
        , [Char] -> LazyByteString
LazyByteString.pack ([Char] -> LazyByteString) -> [Char] -> LazyByteString
forall a b. (a -> b) -> a -> b
$ Int64 -> [Char]
forall a. Show a => a -> [Char]
show (Int64 -> [Char]) -> Int64 -> [Char]
forall a b. (a -> b) -> a -> b
$ LazyByteString -> Int64
LazyByteString.length LazyByteString
content
        , LazyByteString
"\r\n"
        , LazyByteString
"\r\n"
        , LazyByteString
content
        ]

encode :: (ToJSON a) => a -> LazyByteString
encode :: forall a. ToJSON a => a -> LazyByteString
encode = LazyByteString -> LazyByteString
addHeader (LazyByteString -> LazyByteString)
-> (a -> LazyByteString) -> a -> LazyByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> LazyByteString
forall a. ToJSON a => a -> LazyByteString
Aeson.encode