module Data.MessagePack.Types
    ( Assoc (..)
    , Object (..)
    , MessagePack (..)
    , Config, defaultConfig
    , DecodeError, decodeError, errorMessages
    , fromObject
    ) where

import           Control.Monad.Validate             (runValidate)
import           Data.MessagePack.Types.Assoc       (Assoc (..))
import           Data.MessagePack.Types.Class       (Config, MessagePack (..),
                                                     defaultConfig)
import           Data.MessagePack.Types.DecodeError (DecodeError, decodeError,
                                                     errorMessages)
import           Data.MessagePack.Types.Generic     ()
import           Data.MessagePack.Types.Object      (Object (..))


-- | Similar to 'fromObjectWith' 'defaultConfig' but returns the result in
-- a 'MonadFail'.
--
-- Useful when running in another 'MonadFail' like 'Maybe'.
fromObject :: (MonadFail m, MessagePack a) => Object -> m a
fromObject :: Object -> m a
fromObject Object
obj =
    case Validate DecodeError a -> Either DecodeError a
forall e a. Validate e a -> Either e a
runValidate (Config -> Object -> Validate DecodeError a
forall a (m :: * -> *).
(MessagePack a, Applicative m, Monad m,
 MonadValidate DecodeError m) =>
Config -> Object -> m a
fromObjectWith Config
defaultConfig Object
obj) of
        Left DecodeError
err -> String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ DecodeError -> String
forall a. Show a => a -> String
show DecodeError
err
        Right a
ok -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
ok