module Data.Yaml.Marked.Decode
  ( decodeThrow
  , decodeAllThrow
  , decodeFileEither
  , decodeAllFileEither
  , decodeFileWithWarnings
  , decodeAllFileWithWarnings

    -- * Exceptions
  , ParseException (..)
  , YamlException (..)
  ) where

import Prelude

import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans.Resource (MonadThrow, throwM)
import Data.ByteString (ByteString)
import Data.Yaml (ParseException (..))
import Data.Yaml.Marked
import Data.Yaml.Marked.Internal
import Data.Yaml.Marked.Value
import System.IO.Unsafe (unsafePerformIO)
import Text.Libyaml (YamlException (..))
import qualified Text.Libyaml as Y

decodeThrow
  :: MonadThrow m
  => (Marked Value -> Either String a)
  -> FilePath
  -- ^ Name of input being parsed
  -> ByteString
  -> m a
decodeThrow :: forall (m :: * -> *) a.
MonadThrow m =>
(Marked Value -> Either String a) -> String -> ByteString -> m a
decodeThrow Marked Value -> Either String a
p String
fp = (ParseException -> m a)
-> (a -> m a) -> Either ParseException a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ParseException -> m a
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseException a -> m a)
-> (ByteString -> Either ParseException a) -> ByteString -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException a
forall a.
(Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException a
decodeEither Marked Value -> Either String a
p String
fp

decodeAllThrow
  :: MonadThrow m
  => (Marked Value -> Either String a)
  -> FilePath
  -- ^ Name of input being parsed
  -> ByteString
  -> m [a]
decodeAllThrow :: forall (m :: * -> *) a.
MonadThrow m =>
(Marked Value -> Either String a) -> String -> ByteString -> m [a]
decodeAllThrow Marked Value -> Either String a
p String
fp = (ParseException -> m [a])
-> ([a] -> m [a]) -> Either ParseException [a] -> m [a]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ParseException -> m [a]
forall e a. (HasCallStack, Exception e) => e -> m a
forall (m :: * -> *) e a.
(MonadThrow m, HasCallStack, Exception e) =>
e -> m a
throwM [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseException [a] -> m [a])
-> (ByteString -> Either ParseException [a]) -> ByteString -> m [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException [a]
forall a.
(Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException [a]
decodeAllEither Marked Value -> Either String a
p String
fp

decodeFileEither
  :: MonadIO m
  => (Marked Value -> Either String a)
  -> FilePath
  -> m (Either ParseException a)
decodeFileEither :: forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException a)
decodeFileEither Marked Value -> Either String a
p = (Either ParseException (a, [Warning]) -> Either ParseException a)
-> m (Either ParseException (a, [Warning]))
-> m (Either ParseException a)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (((a, [Warning]) -> a)
-> Either ParseException (a, [Warning]) -> Either ParseException a
forall a b.
(a -> b) -> Either ParseException a -> Either ParseException b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, [Warning]) -> a
forall a b. (a, b) -> a
fst) (m (Either ParseException (a, [Warning]))
 -> m (Either ParseException a))
-> (String -> m (Either ParseException (a, [Warning])))
-> String
-> m (Either ParseException a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String -> m (Either ParseException (a, [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException (a, [Warning]))
decodeFileWithWarnings Marked Value -> Either String a
p

decodeAllFileEither
  :: MonadIO m
  => (Marked Value -> Either String a)
  -> FilePath
  -> m (Either ParseException [a])
decodeAllFileEither :: forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException [a])
decodeAllFileEither Marked Value -> Either String a
p = (Either ParseException ([a], [Warning])
 -> Either ParseException [a])
-> m (Either ParseException ([a], [Warning]))
-> m (Either ParseException [a])
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((([a], [Warning]) -> [a])
-> Either ParseException ([a], [Warning])
-> Either ParseException [a]
forall a b.
(a -> b) -> Either ParseException a -> Either ParseException b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a], [Warning]) -> [a]
forall a b. (a, b) -> a
fst) (m (Either ParseException ([a], [Warning]))
 -> m (Either ParseException [a]))
-> (String -> m (Either ParseException ([a], [Warning])))
-> String
-> m (Either ParseException [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String -> m (Either ParseException ([a], [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException ([a], [Warning]))
decodeAllFileWithWarnings Marked Value -> Either String a
p

decodeFileWithWarnings
  :: MonadIO m
  => (Marked Value -> Either String a)
  -> FilePath
  -> m (Either ParseException (a, [Warning]))
decodeFileWithWarnings :: forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException (a, [Warning]))
decodeFileWithWarnings Marked Value -> Either String a
p String
fp = IO (Either ParseException (a, [Warning]))
-> m (Either ParseException (a, [Warning]))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either ParseException (a, [Warning]))
 -> m (Either ParseException (a, [Warning])))
-> IO (Either ParseException (a, [Warning]))
-> m (Either ParseException (a, [Warning]))
forall a b. (a -> b) -> a -> b
$ (Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException (a, [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> m (Either ParseException (a, [Warning]))
decodeHelper Marked Value -> Either String a
p String
fp (ConduitT () MarkedEvent Parse ()
 -> IO (Either ParseException (a, [Warning])))
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException (a, [Warning]))
forall a b. (a -> b) -> a -> b
$ String -> ConduitT () MarkedEvent Parse ()
forall (m :: * -> *) i.
MonadResource m =>
String -> ConduitM i MarkedEvent m ()
Y.decodeFileMarked String
fp

decodeAllFileWithWarnings
  :: MonadIO m
  => (Marked Value -> Either String a)
  -> FilePath
  -> m (Either ParseException ([a], [Warning]))
decodeAllFileWithWarnings :: forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String -> m (Either ParseException ([a], [Warning]))
decodeAllFileWithWarnings Marked Value -> Either String a
p String
fp =
  IO (Either ParseException ([a], [Warning]))
-> m (Either ParseException ([a], [Warning]))
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either ParseException ([a], [Warning]))
 -> m (Either ParseException ([a], [Warning])))
-> IO (Either ParseException ([a], [Warning]))
-> m (Either ParseException ([a], [Warning]))
forall a b. (a -> b) -> a -> b
$ (Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException ([a], [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> m (Either ParseException ([a], [Warning]))
decodeAllHelper Marked Value -> Either String a
p String
fp (ConduitT () MarkedEvent Parse ()
 -> IO (Either ParseException ([a], [Warning])))
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException ([a], [Warning]))
forall a b. (a -> b) -> a -> b
$ String -> ConduitT () MarkedEvent Parse ()
forall (m :: * -> *) i.
MonadResource m =>
String -> ConduitM i MarkedEvent m ()
Y.decodeFileMarked String
fp

decodeEither
  :: (Marked Value -> Either String a)
  -> FilePath
  -- ^ Name of input being parsed
  -> ByteString
  -> Either ParseException a
decodeEither :: forall a.
(Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException a
decodeEither Marked Value -> Either String a
p String
fp =
  ((a, [Warning]) -> a)
-> Either ParseException (a, [Warning]) -> Either ParseException a
forall a b.
(a -> b) -> Either ParseException a -> Either ParseException b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, [Warning]) -> a
forall a b. (a, b) -> a
fst (Either ParseException (a, [Warning]) -> Either ParseException a)
-> (ByteString -> Either ParseException (a, [Warning]))
-> ByteString
-> Either ParseException a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either ParseException (a, [Warning]))
-> Either ParseException (a, [Warning])
forall a. IO a -> a
unsafePerformIO (IO (Either ParseException (a, [Warning]))
 -> Either ParseException (a, [Warning]))
-> (ByteString -> IO (Either ParseException (a, [Warning])))
-> ByteString
-> Either ParseException (a, [Warning])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException (a, [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> m (Either ParseException (a, [Warning]))
decodeHelper Marked Value -> Either String a
p String
fp (ConduitT () MarkedEvent Parse ()
 -> IO (Either ParseException (a, [Warning])))
-> (ByteString -> ConduitT () MarkedEvent Parse ())
-> ByteString
-> IO (Either ParseException (a, [Warning]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConduitT () MarkedEvent Parse ()
forall (m :: * -> *) i.
MonadResource m =>
ByteString -> ConduitM i MarkedEvent m ()
Y.decodeMarked

decodeAllEither
  :: (Marked Value -> Either String a)
  -> FilePath
  -- ^ Name of input being parsed
  -> ByteString
  -> Either ParseException [a]
decodeAllEither :: forall a.
(Marked Value -> Either String a)
-> String -> ByteString -> Either ParseException [a]
decodeAllEither Marked Value -> Either String a
p String
fp =
  (([a], [Warning]) -> [a])
-> Either ParseException ([a], [Warning])
-> Either ParseException [a]
forall a b.
(a -> b) -> Either ParseException a -> Either ParseException b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a], [Warning]) -> [a]
forall a b. (a, b) -> a
fst (Either ParseException ([a], [Warning])
 -> Either ParseException [a])
-> (ByteString -> Either ParseException ([a], [Warning]))
-> ByteString
-> Either ParseException [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Either ParseException ([a], [Warning]))
-> Either ParseException ([a], [Warning])
forall a. IO a -> a
unsafePerformIO (IO (Either ParseException ([a], [Warning]))
 -> Either ParseException ([a], [Warning]))
-> (ByteString -> IO (Either ParseException ([a], [Warning])))
-> ByteString
-> Either ParseException ([a], [Warning])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> IO (Either ParseException ([a], [Warning]))
forall (m :: * -> *) a.
MonadIO m =>
(Marked Value -> Either String a)
-> String
-> ConduitT () MarkedEvent Parse ()
-> m (Either ParseException ([a], [Warning]))
decodeAllHelper Marked Value -> Either String a
p String
fp (ConduitT () MarkedEvent Parse ()
 -> IO (Either ParseException ([a], [Warning])))
-> (ByteString -> ConduitT () MarkedEvent Parse ())
-> ByteString
-> IO (Either ParseException ([a], [Warning]))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ConduitT () MarkedEvent Parse ()
forall (m :: * -> *) i.
MonadResource m =>
ByteString -> ConduitM i MarkedEvent m ()
Y.decodeMarked