{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Servant.Types.SourceT where
import Control.Monad.Except
(ExceptT (..), runExceptT, throwError)
import Control.Monad.Morph
(MFunctor (..))
import Control.Monad.Trans.Class
(MonadTrans (..))
import qualified Data.Attoparsec.ByteString as A
import qualified Data.ByteString as BS
import Data.Functor.Classes
(Show1 (..), showsBinaryWith, showsPrec1, showsUnaryWith)
import Data.Functor.Identity
(Identity (..))
import Prelude ()
import Prelude.Compat hiding
(readFile)
import System.IO
(Handle, IOMode (..), withFile)
import qualified Test.QuickCheck as QC
newtype SourceT m a = SourceT
{ forall (m :: * -> *) a.
SourceT m a -> forall b. (StepT m a -> m b) -> m b
unSourceT :: forall b. (StepT m a -> m b) -> m b
}
mapStepT :: (StepT m a -> StepT m b) -> SourceT m a -> SourceT m b
mapStepT :: forall (m :: * -> *) a b.
(StepT m a -> StepT m b) -> SourceT m a -> SourceT m b
mapStepT StepT m a -> StepT m b
f (SourceT forall b. (StepT m a -> m b) -> m b
m) = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT forall a b. (a -> b) -> a -> b
$ \StepT m b -> m b
k -> forall b. (StepT m a -> m b) -> m b
m (StepT m b -> m b
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. StepT m a -> StepT m b
f)
{-# INLINE mapStepT #-}
data StepT m a
= Stop
| Error String
| Skip (StepT m a)
| Yield a (StepT m a)
| Effect (m (StepT m a))
deriving forall a b. a -> StepT m b -> StepT m a
forall a b. (a -> b) -> StepT m a -> StepT m b
forall (m :: * -> *) a b. Functor m => a -> StepT m b -> StepT m a
forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> StepT m a -> StepT m b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> StepT m b -> StepT m a
$c<$ :: forall (m :: * -> *) a b. Functor m => a -> StepT m b -> StepT m a
fmap :: forall a b. (a -> b) -> StepT m a -> StepT m b
$cfmap :: forall (m :: * -> *) a b.
Functor m =>
(a -> b) -> StepT m a -> StepT m b
Functor
fromStepT :: StepT m a -> SourceT m a
fromStepT :: forall (m :: * -> *) a. StepT m a -> SourceT m a
fromStepT StepT m a
s = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT (forall a b. (a -> b) -> a -> b
$ StepT m a
s)
instance Functor m => Functor (SourceT m) where
fmap :: forall a b. (a -> b) -> SourceT m a -> SourceT m b
fmap a -> b
f = forall (m :: * -> *) a b.
(StepT m a -> StepT m b) -> SourceT m a -> SourceT m b
mapStepT (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
instance Identity ~ m => Foldable (SourceT m) where
foldr :: forall a b. (a -> b -> b) -> b -> SourceT m a -> b
foldr a -> b -> b
f b
z (SourceT forall b. (StepT m a -> m b) -> m b
m) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> b -> b
f b
z (forall a. Identity a -> a
runIdentity (forall b. (StepT m a -> m b) -> m b
m forall a. a -> Identity a
Identity))
instance (Applicative m, Show1 m) => Show1 (SourceT m) where
liftShowsPrec :: forall a.
(Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> SourceT m a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl Int
d (SourceT forall b. (StepT m a -> m b) -> m b
m) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith
(forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl)
String
"fromStepT" Int
d (forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect (forall b. (StepT m a -> m b) -> m b
m forall {m :: * -> *} {a}.
Applicative m =>
StepT m a -> m (StepT m a)
pure'))
where
pure' :: StepT m a -> m (StepT m a)
pure' (Effect m (StepT m a)
s) = m (StepT m a)
s
pure' StepT m a
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure StepT m a
s
instance (Applicative m, Show1 m, Show a) => Show (SourceT m a) where
showsPrec :: Int -> SourceT m a -> ShowS
showsPrec = forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
instance MFunctor SourceT where
hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> SourceT m b -> SourceT n b
hoist forall a. m a -> n a
f (SourceT forall b. (StepT m b -> m b) -> m b
m) = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT forall a b. (a -> b) -> a -> b
$ \StepT n b -> n b
k -> StepT n b -> n b
k forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect forall a b. (a -> b) -> a -> b
$ forall a. m a -> n a
f forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall {k} (t :: (* -> *) -> k -> *) (m :: * -> *) (n :: * -> *)
(b :: k).
(MFunctor t, Monad m) =>
(forall a. m a -> n a) -> t m b -> t n b
hoist forall a. m a -> n a
f) forall a b. (a -> b) -> a -> b
$ forall b. (StepT m b -> m b) -> m b
m forall (m :: * -> *) a. Monad m => a -> m a
return
instance Functor m => Semigroup (SourceT m a) where
SourceT forall b. (StepT m a -> m b) -> m b
withL <> :: SourceT m a -> SourceT m a -> SourceT m a
<> SourceT forall b. (StepT m a -> m b) -> m b
withR = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT forall a b. (a -> b) -> a -> b
$ \StepT m a -> m b
ret ->
forall b. (StepT m a -> m b) -> m b
withL forall a b. (a -> b) -> a -> b
$ \StepT m a
l ->
forall b. (StepT m a -> m b) -> m b
withR forall a b. (a -> b) -> a -> b
$ \StepT m a
r ->
StepT m a -> m b
ret forall a b. (a -> b) -> a -> b
$ StepT m a
l forall a. Semigroup a => a -> a -> a
<> StepT m a
r
instance Functor m => Monoid (SourceT m a) where
mempty :: SourceT m a
mempty = forall (m :: * -> *) a. StepT m a -> SourceT m a
fromStepT forall a. Monoid a => a
mempty
mappend :: SourceT m a -> SourceT m a -> SourceT m a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance (QC.Arbitrary a, Monad m) => QC.Arbitrary (SourceT m a) where
arbitrary :: Gen (SourceT m a)
arbitrary = forall (m :: * -> *) a. StepT m a -> SourceT m a
fromStepT forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Arbitrary a => Gen a
QC.arbitrary
instance Identity ~ m => Foldable (StepT m) where
foldr :: forall a b. (a -> b -> b) -> b -> StepT m a -> b
foldr a -> b -> b
f b
z = StepT Identity a -> b
go where
go :: StepT Identity a -> b
go StepT Identity a
Stop = b
z
go (Error String
_) = b
z
go (Skip StepT Identity a
s) = StepT Identity a -> b
go StepT Identity a
s
go (Yield a
a StepT Identity a
s) = a -> b -> b
f a
a (StepT Identity a -> b
go StepT Identity a
s)
go (Effect (Identity StepT Identity a
s)) = StepT Identity a -> b
go StepT Identity a
s
instance (Applicative m, Show1 m) => Show1 (StepT m) where
liftShowsPrec :: forall a.
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> StepT m a -> ShowS
liftShowsPrec Int -> a -> ShowS
sp [a] -> ShowS
sl = Int -> StepT m a -> ShowS
go where
go :: Int -> StepT m a -> ShowS
go Int
_ StepT m a
Stop = String -> ShowS
showString String
"Stop"
go Int
d (Skip StepT m a
s) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith
Int -> StepT m a -> ShowS
go
String
"Skip" Int
d StepT m a
s
go Int
d (Error String
err) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith
forall a. Show a => Int -> a -> ShowS
showsPrec
String
"Error" Int
d String
err
go Int
d (Effect m (StepT m a)
ms) = forall a. (Int -> a -> ShowS) -> String -> Int -> a -> ShowS
showsUnaryWith
(forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS
liftShowsPrec Int -> StepT m a -> ShowS
go [StepT m a] -> ShowS
goList)
String
"Effect" Int
d m (StepT m a)
ms
go Int
d (Yield a
x StepT m a
s) = forall a b.
(Int -> a -> ShowS)
-> (Int -> b -> ShowS) -> String -> Int -> a -> b -> ShowS
showsBinaryWith
Int -> a -> ShowS
sp Int -> StepT m a -> ShowS
go
String
"Yield" Int
d a
x StepT m a
s
goList :: [StepT m a] -> ShowS
goList = forall (f :: * -> *) a.
Show1 f =>
(Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS
liftShowList Int -> a -> ShowS
sp [a] -> ShowS
sl
instance (Applicative m, Show1 m, Show a) => Show (StepT m a) where
showsPrec :: Int -> StepT m a -> ShowS
showsPrec = forall (f :: * -> *) a. (Show1 f, Show a) => Int -> f a -> ShowS
showsPrec1
#if !MIN_VERSION_transformers(0,6,0)
instance MonadTrans StepT where
lift :: forall (m :: * -> *) a. Monad m => m a -> StepT m a
lift = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall (m :: * -> *) a. a -> StepT m a -> StepT m a
`Yield` forall (m :: * -> *) a. StepT m a
Stop)
#endif
instance MFunctor StepT where
hoist :: forall (m :: * -> *) (n :: * -> *) b.
Monad m =>
(forall a. m a -> n a) -> StepT m b -> StepT n b
hoist forall a. m a -> n a
f = StepT m b -> StepT n b
go where
go :: StepT m b -> StepT n b
go StepT m b
Stop = forall (m :: * -> *) a. StepT m a
Stop
go (Error String
err) = forall (m :: * -> *) a. String -> StepT m a
Error String
err
go (Skip StepT m b
s) = forall (m :: * -> *) a. StepT m a -> StepT m a
Skip (StepT m b -> StepT n b
go StepT m b
s)
go (Yield b
x StepT m b
s) = forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield b
x (StepT m b -> StepT n b
go StepT m b
s)
go (Effect m (StepT m b)
ms) = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect (forall a. m a -> n a
f (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StepT m b -> StepT n b
go m (StepT m b)
ms))
instance Functor m => Semigroup (StepT m a) where
StepT m a
Stop <> :: StepT m a -> StepT m a -> StepT m a
<> StepT m a
r = StepT m a
r
Error String
err <> StepT m a
_ = forall (m :: * -> *) a. String -> StepT m a
Error String
err
Skip StepT m a
s <> StepT m a
r = forall (m :: * -> *) a. StepT m a -> StepT m a
Skip (StepT m a
s forall a. Semigroup a => a -> a -> a
<> StepT m a
r)
Yield a
x StepT m a
s <> StepT m a
r = forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
x (StepT m a
s forall a. Semigroup a => a -> a -> a
<> StepT m a
r)
Effect m (StepT m a)
ms <> StepT m a
r = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect ((forall a. Semigroup a => a -> a -> a
<> StepT m a
r) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (StepT m a)
ms)
instance Functor m => Monoid (StepT m a) where
mempty :: StepT m a
mempty = forall (m :: * -> *) a. StepT m a
Stop
mappend :: StepT m a -> StepT m a -> StepT m a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance (QC.Arbitrary a, Monad m) => QC.Arbitrary (StepT m a) where
arbitrary :: Gen (StepT m a)
arbitrary = forall a. (Int -> Gen a) -> Gen a
QC.sized forall {a} {m :: * -> *} {a}.
(Num a, Ord a, Monad m, Arbitrary a) =>
a -> Gen (StepT m a)
arb where
arb :: a -> Gen (StepT m a)
arb a
n | a
n forall a. Ord a => a -> a -> Bool
<= a
0 = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) a. StepT m a
Stop
| Bool
otherwise = forall a. [(Int, Gen a)] -> Gen a
QC.frequency
[ (Int
1, forall (f :: * -> *) a. Applicative f => a -> f a
pure forall (m :: * -> *) a. StepT m a
Stop)
, (Int
1, forall (m :: * -> *) a. StepT m a -> StepT m a
Skip forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (StepT m a)
arb')
, (Int
1, forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (StepT m a)
arb')
, (Int
8, forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. Arbitrary a => Gen a
QC.arbitrary forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (StepT m a)
arb')
]
where
arb' :: Gen (StepT m a)
arb' = a -> Gen (StepT m a)
arb (a
n forall a. Num a => a -> a -> a
- a
1)
shrink :: StepT m a -> [StepT m a]
shrink StepT m a
Stop = []
shrink (Error String
_) = [forall (m :: * -> *) a. StepT m a
Stop]
shrink (Skip StepT m a
s) = [StepT m a
s]
shrink (Effect m (StepT m a)
_) = []
shrink (Yield a
x StepT m a
s) =
[ forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
x' StepT m a
s | a
x' <- forall a. Arbitrary a => a -> [a]
QC.shrink a
x ] forall a. [a] -> [a] -> [a]
++
[ forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
x StepT m a
s' | StepT m a
s' <- forall a. Arbitrary a => a -> [a]
QC.shrink StepT m a
s ]
source :: Foldable f => f a -> SourceT m a
source :: forall (f :: * -> *) a (m :: * -> *).
Foldable f =>
f a -> SourceT m a
source = forall (m :: * -> *) a. StepT m a -> SourceT m a
fromStepT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield forall (m :: * -> *) a. StepT m a
Stop
runSourceT :: Monad m => SourceT m a -> ExceptT String m [a]
runSourceT :: forall (m :: * -> *) a.
Monad m =>
SourceT m a -> ExceptT String m [a]
runSourceT (SourceT forall b. (StepT m a -> m b) -> m b
m) = forall e (m :: * -> *) a. m (Either e a) -> ExceptT e m a
ExceptT (forall b. (StepT m a -> m b) -> m b
m (forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
Monad m =>
StepT m a -> ExceptT String m [a]
runStepT))
runStepT :: Monad m => StepT m a -> ExceptT String m [a]
runStepT :: forall (m :: * -> *) a.
Monad m =>
StepT m a -> ExceptT String m [a]
runStepT StepT m a
Stop = forall (m :: * -> *) a. Monad m => a -> m a
return []
runStepT (Error String
err) = forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError String
err
runStepT (Skip StepT m a
s) = forall (m :: * -> *) a.
Monad m =>
StepT m a -> ExceptT String m [a]
runStepT StepT m a
s
runStepT (Yield a
x StepT m a
s) = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a
x forall a. a -> [a] -> [a]
:) (forall (m :: * -> *) a.
Monad m =>
StepT m a -> ExceptT String m [a]
runStepT StepT m a
s)
runStepT (Effect m (StepT m a)
ms) = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (StepT m a)
ms forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall (m :: * -> *) a.
Monad m =>
StepT m a -> ExceptT String m [a]
runStepT
mapMaybe :: Functor m => (a -> Maybe b) -> SourceT m a -> SourceT m b
mapMaybe :: forall (m :: * -> *) a b.
Functor m =>
(a -> Maybe b) -> SourceT m a -> SourceT m b
mapMaybe a -> Maybe b
p (SourceT forall b. (StepT m a -> m b) -> m b
m) = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT forall a b. (a -> b) -> a -> b
$ \StepT m b -> m b
k -> forall b. (StepT m a -> m b) -> m b
m (StepT m b -> m b
k forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a b.
Functor m =>
(a -> Maybe b) -> StepT m a -> StepT m b
mapMaybeStep a -> Maybe b
p)
mapMaybeStep :: Functor m => (a -> Maybe b) -> StepT m a -> StepT m b
mapMaybeStep :: forall (m :: * -> *) a b.
Functor m =>
(a -> Maybe b) -> StepT m a -> StepT m b
mapMaybeStep a -> Maybe b
p = StepT m a -> StepT m b
go where
go :: StepT m a -> StepT m b
go StepT m a
Stop = forall (m :: * -> *) a. StepT m a
Stop
go (Error String
err) = forall (m :: * -> *) a. String -> StepT m a
Error String
err
go (Skip StepT m a
s) = forall (m :: * -> *) a. StepT m a -> StepT m a
Skip (StepT m a -> StepT m b
go StepT m a
s)
go (Effect m (StepT m a)
ms) = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StepT m a -> StepT m b
go m (StepT m a)
ms)
go (Yield a
x StepT m a
s) = case a -> Maybe b
p a
x of
Maybe b
Nothing -> forall (m :: * -> *) a. StepT m a -> StepT m a
Skip (StepT m a -> StepT m b
go StepT m a
s)
Just b
y -> forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield b
y (StepT m a -> StepT m b
go StepT m a
s)
foreach
:: Monad m
=> (String -> m ())
-> (a -> m ())
-> SourceT m a
-> m ()
foreach :: forall (m :: * -> *) a.
Monad m =>
(String -> m ()) -> (a -> m ()) -> SourceT m a -> m ()
foreach String -> m ()
f a -> m ()
g SourceT m a
src = forall (m :: * -> *) a.
SourceT m a -> forall b. (StepT m a -> m b) -> m b
unSourceT SourceT m a
src (forall (m :: * -> *) a.
Monad m =>
(String -> m ()) -> (a -> m ()) -> StepT m a -> m ()
foreachStep String -> m ()
f a -> m ()
g)
foreachStep
:: Monad m
=> (String -> m ())
-> (a -> m ())
-> StepT m a
-> m ()
foreachStep :: forall (m :: * -> *) a.
Monad m =>
(String -> m ()) -> (a -> m ()) -> StepT m a -> m ()
foreachStep String -> m ()
f a -> m ()
g = StepT m a -> m ()
go where
go :: StepT m a -> m ()
go StepT m a
Stop = forall (m :: * -> *) a. Monad m => a -> m a
return ()
go (Skip StepT m a
s) = StepT m a -> m ()
go StepT m a
s
go (Yield a
x StepT m a
s) = a -> m ()
g a
x forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> StepT m a -> m ()
go StepT m a
s
go (Error String
err) = String -> m ()
f String
err
go (Effect m (StepT m a)
ms) = m (StepT m a)
ms forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= StepT m a -> m ()
go
fromAction :: Functor m => (a -> Bool) -> m a -> SourceT m a
fromAction :: forall (m :: * -> *) a.
Functor m =>
(a -> Bool) -> m a -> SourceT m a
fromAction a -> Bool
stop m a
action = forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT (forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
Functor m =>
(a -> Bool) -> m a -> StepT m a
fromActionStep a -> Bool
stop m a
action)
{-# INLINE fromAction #-}
fromActionStep :: Functor m => (a -> Bool) -> m a -> StepT m a
fromActionStep :: forall (m :: * -> *) a.
Functor m =>
(a -> Bool) -> m a -> StepT m a
fromActionStep a -> Bool
stop m a
action = StepT m a
loop where
loop :: StepT m a
loop = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect forall a b. (a -> b) -> a -> b
$ forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> StepT m a
step m a
action
step :: a -> StepT m a
step a
x
| a -> Bool
stop a
x = forall (m :: * -> *) a. StepT m a
Stop
| Bool
otherwise = forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
x StepT m a
loop
{-# INLINE fromActionStep #-}
readFile :: FilePath -> SourceT IO BS.ByteString
readFile :: String -> SourceT IO ByteString
readFile String
fp =
forall (m :: * -> *) a.
(forall b. (StepT m a -> m b) -> m b) -> SourceT m a
SourceT forall a b. (a -> b) -> a -> b
$ \StepT IO ByteString -> IO b
k ->
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
fp IOMode
ReadMode forall a b. (a -> b) -> a -> b
$ \Handle
hdl ->
StepT IO ByteString -> IO b
k (Handle -> StepT IO ByteString
readHandle Handle
hdl)
where
readHandle :: Handle -> StepT IO BS.ByteString
readHandle :: Handle -> StepT IO ByteString
readHandle Handle
hdl = forall (m :: * -> *) a.
Functor m =>
(a -> Bool) -> m a -> StepT m a
fromActionStep ByteString -> Bool
BS.null (Handle -> Int -> IO ByteString
BS.hGet Handle
hdl Int
4096)
transformWithAtto :: Monad m => A.Parser a -> SourceT m BS.ByteString -> SourceT m a
transformWithAtto :: forall (m :: * -> *) a.
Monad m =>
Parser a -> SourceT m ByteString -> SourceT m a
transformWithAtto Parser a
parser = forall (m :: * -> *) a b.
(StepT m a -> StepT m b) -> SourceT m a -> SourceT m b
mapStepT (forall a (m :: * -> *).
Monad m =>
Parser a -> StepT m ByteString -> StepT m a
transformStepWithAtto Parser a
parser)
transformStepWithAtto
:: forall a m. Monad m
=> A.Parser a -> StepT m BS.ByteString -> StepT m a
transformStepWithAtto :: forall a (m :: * -> *).
Monad m =>
Parser a -> StepT m ByteString -> StepT m a
transformStepWithAtto Parser a
parser = (ByteString -> Result a) -> StepT m ByteString -> StepT m a
go (forall a. Parser a -> ByteString -> Result a
A.parse Parser a
parser) where
p0 :: ByteString -> Result a
p0 = forall a. Parser a -> ByteString -> Result a
A.parse Parser a
parser
go :: (BS.ByteString -> A.Result a)
-> StepT m BS.ByteString -> StepT m a
go :: (ByteString -> Result a) -> StepT m ByteString -> StepT m a
go ByteString -> Result a
_ (Error String
err) = forall (m :: * -> *) a. String -> StepT m a
Error String
err
go ByteString -> Result a
p (Skip StepT m ByteString
s) = forall (m :: * -> *) a. StepT m a -> StepT m a
Skip ((ByteString -> Result a) -> StepT m ByteString -> StepT m a
go ByteString -> Result a
p StepT m ByteString
s)
go ByteString -> Result a
p (Effect m (StepT m ByteString)
ms) = forall (m :: * -> *) a. m (StepT m a) -> StepT m a
Effect (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((ByteString -> Result a) -> StepT m ByteString -> StepT m a
go ByteString -> Result a
p) m (StepT m ByteString)
ms)
go ByteString -> Result a
p StepT m ByteString
Stop = case ByteString -> Result a
p forall a. Monoid a => a
mempty of
A.Fail ByteString
_ [String]
_ String
err -> forall (m :: * -> *) a. String -> StepT m a
Error String
err
A.Done ByteString
_ a
a -> forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
a forall (m :: * -> *) a. StepT m a
Stop
A.Partial ByteString -> Result a
_ -> forall (m :: * -> *) a. StepT m a
Stop
go ByteString -> Result a
p (Yield ByteString
bs0 StepT m ByteString
s) = (ByteString -> Result a) -> ByteString -> StepT m a
loop ByteString -> Result a
p ByteString
bs0 where
loop :: (ByteString -> Result a) -> ByteString -> StepT m a
loop ByteString -> Result a
p' ByteString
bs
| ByteString -> Bool
BS.null ByteString
bs = forall (m :: * -> *) a. StepT m a -> StepT m a
Skip ((ByteString -> Result a) -> StepT m ByteString -> StepT m a
go ByteString -> Result a
p' StepT m ByteString
s)
| Bool
otherwise = case ByteString -> Result a
p' ByteString
bs of
A.Fail ByteString
_ [String]
_ String
err -> forall (m :: * -> *) a. String -> StepT m a
Error String
err
A.Done ByteString
bs' a
a -> forall (m :: * -> *) a. a -> StepT m a -> StepT m a
Yield a
a ((ByteString -> Result a) -> ByteString -> StepT m a
loop ByteString -> Result a
p0 ByteString
bs')
A.Partial ByteString -> Result a
p'' -> forall (m :: * -> *) a. StepT m a -> StepT m a
Skip ((ByteString -> Result a) -> StepT m ByteString -> StepT m a
go ByteString -> Result a
p'' StepT m ByteString
s)