module Codec.Candid.EncodeTextual where

import qualified Data.ByteString.Lazy as BS
import qualified Data.ByteString.Builder as B
import Control.Monad

import Codec.Candid.Parse
import Codec.Candid.Encode

-- | Encodes a Candid value given in textual form.
--
-- This may fail if the textual form cannot be parsed or has inconsistent
-- types. It does not use the @reserved@ supertype (unless explicitly told to).
encodeTextual :: String -> Either String BS.ByteString
encodeTextual :: String -> Either String ByteString
encodeTextual = String -> Either String [Value]
parseValues (String -> Either String [Value])
-> ([Value] -> Either String ByteString)
-> String
-> Either String ByteString
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> [Value] -> Either String Builder
encodeDynValues ([Value] -> Either String Builder)
-> (Builder -> Either String ByteString)
-> [Value]
-> Either String ByteString
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> ByteString -> Either String ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> Either String ByteString)
-> (Builder -> ByteString) -> Builder -> Either String ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
B.toLazyByteString