{-# LANGUAGE RankNTypes #-}
module Turtle.Bytes (
stdin
, input
, inhandle
, stdout
, output
, outhandle
, append
, stderr
, strict
, compress
, decompress
, WindowBits(..)
, Zlib.defaultWindowBits
, fromUTF8
, toUTF8
, proc
, shell
, procs
, shells
, inproc
, inshell
, inprocWithErr
, inshellWithErr
, procStrict
, shellStrict
, procStrictWithErr
, shellStrictWithErr
, system
, stream
, streamWithErr
, systemStrict
, systemStrictWithErr
) where
import Control.Applicative
import Control.Concurrent.Async (Async, Concurrently(..))
import Control.Monad.IO.Class (MonadIO(..))
import Control.Monad.Managed (MonadManaged(..))
import Data.ByteString (ByteString)
import Data.Monoid
import Data.Streaming.Zlib (Inflate, Popper, PopperRes(..), WindowBits(..))
import Data.Text (Text)
import Data.Text.Encoding (Decoding(..))
import System.Exit (ExitCode(..))
import System.IO (Handle)
import Turtle.Internal (ignoreSIGPIPE)
import Turtle.Prelude (ProcFailed(..), ShellFailed(..))
import Turtle.Shell (Shell(..), FoldShell(..), fold, sh)
import qualified Control.Concurrent.Async as Async
import qualified Control.Concurrent.STM as STM
import qualified Control.Concurrent.MVar as MVar
import qualified Control.Concurrent.STM.TQueue as TQueue
import qualified Control.Exception as Exception
import qualified Control.Foldl
import qualified Control.Monad
import qualified Control.Monad.Managed as Managed
import qualified Data.ByteString
import qualified Data.Streaming.Zlib as Zlib
import qualified Data.Text
import qualified Data.Text.Encoding as Encoding
import qualified Data.Text.Encoding.Error as Encoding.Error
import qualified Foreign
import qualified System.IO
import qualified System.Process as Process
import qualified Turtle.Prelude
stdin :: Shell ByteString
stdin :: Shell ByteString
stdin = Handle -> Shell ByteString
inhandle Handle
System.IO.stdin
input :: FilePath -> Shell ByteString
input :: FilePath -> Shell ByteString
input FilePath
file = do
Handle
handle <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using (forall (managed :: * -> *).
MonadManaged managed =>
FilePath -> managed Handle
Turtle.Prelude.readonly FilePath
file)
Handle -> Shell ByteString
inhandle Handle
handle
inhandle :: Handle -> Shell ByteString
inhandle :: Handle -> Shell ByteString
inhandle Handle
handle = forall a. (forall r. FoldShell a r -> IO r) -> Shell a
Shell (\(FoldShell x -> ByteString -> IO x
step x
begin x -> IO r
done) -> do
let loop :: x -> IO r
loop x
x = do
Bool
eof <- Handle -> IO Bool
System.IO.hIsEOF Handle
handle
if Bool
eof
then x -> IO r
done x
x
else do
ByteString
bytes <- Handle -> Int -> IO ByteString
Data.ByteString.hGetSome Handle
handle Int
defaultChunkSize
x
x' <- x -> ByteString -> IO x
step x
x ByteString
bytes
x -> IO r
loop forall a b. (a -> b) -> a -> b
$! x
x'
x -> IO r
loop forall a b. (a -> b) -> a -> b
$! x
begin )
where
defaultChunkSize :: Int
defaultChunkSize :: Int
defaultChunkSize = Int
32 forall a. Num a => a -> a -> a
* Int
1024 forall a. Num a => a -> a -> a
- Int
2 forall a. Num a => a -> a -> a
* forall a. Storable a => a -> Int
Foreign.sizeOf (forall a. HasCallStack => a
undefined :: Int)
stdout :: MonadIO io => Shell ByteString -> io ()
stdout :: forall (io :: * -> *). MonadIO io => Shell ByteString -> io ()
stdout Shell ByteString
s = forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
System.IO.stdout ByteString
bytes) )
output :: MonadIO io => FilePath -> Shell ByteString -> io ()
output :: forall (io :: * -> *).
MonadIO io =>
FilePath -> Shell ByteString -> io ()
output FilePath
file Shell ByteString
s = forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
Handle
handle <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using (forall (managed :: * -> *).
MonadManaged managed =>
FilePath -> managed Handle
Turtle.Prelude.writeonly FilePath
file)
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
handle ByteString
bytes) )
outhandle :: MonadIO io => Handle -> Shell ByteString -> io ()
outhandle :: forall (io :: * -> *).
MonadIO io =>
Handle -> Shell ByteString -> io ()
outhandle Handle
handle Shell ByteString
s = forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
handle ByteString
bytes) )
append :: MonadIO io => FilePath -> Shell ByteString -> io ()
append :: forall (io :: * -> *).
MonadIO io =>
FilePath -> Shell ByteString -> io ()
append FilePath
file Shell ByteString
s = forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
Handle
handle <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using (forall (managed :: * -> *).
MonadManaged managed =>
FilePath -> managed Handle
Turtle.Prelude.appendonly FilePath
file)
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
handle ByteString
bytes) )
stderr :: MonadIO io => Shell ByteString -> io ()
stderr :: forall (io :: * -> *). MonadIO io => Shell ByteString -> io ()
stderr Shell ByteString
s = forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
System.IO.stderr ByteString
bytes) )
strict :: MonadIO io => Shell ByteString -> io ByteString
strict :: forall (io :: * -> *).
MonadIO io =>
Shell ByteString -> io ByteString
strict Shell ByteString
s = do
[ByteString]
listOfByteStrings <- forall (io :: * -> *) a b.
MonadIO io =>
Shell a -> Fold a b -> io b
fold Shell ByteString
s forall a. Fold a [a]
Control.Foldl.list
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> ByteString
Data.ByteString.concat [ByteString]
listOfByteStrings)
proc
:: MonadIO io
=> Text
-> [Text]
-> Shell ByteString
-> io ExitCode
proc :: forall (io :: * -> *).
MonadIO io =>
Text -> [Text] -> Shell ByteString -> io ExitCode
proc Text
cmd [Text]
args =
forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io ExitCode
system
( (FilePath -> [FilePath] -> CreateProcess
Process.proc (Text -> FilePath
Data.Text.unpack Text
cmd) (forall a b. (a -> b) -> [a] -> [b]
map Text -> FilePath
Data.Text.unpack [Text]
args))
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.Inherit
, std_err :: StdStream
Process.std_err = StdStream
Process.Inherit
} )
shell
:: MonadIO io
=> Text
-> Shell ByteString
-> io ExitCode
shell :: forall (io :: * -> *).
MonadIO io =>
Text -> Shell ByteString -> io ExitCode
shell Text
cmdline =
forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io ExitCode
system
( (FilePath -> CreateProcess
Process.shell (Text -> FilePath
Data.Text.unpack Text
cmdline))
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.Inherit
, std_err :: StdStream
Process.std_err = StdStream
Process.Inherit
} )
procs
:: MonadIO io
=> Text
-> [Text]
-> Shell ByteString
-> io ()
procs :: forall (io :: * -> *).
MonadIO io =>
Text -> [Text] -> Shell ByteString -> io ()
procs Text
cmd [Text]
args Shell ByteString
s = do
ExitCode
exitCode <- forall (io :: * -> *).
MonadIO io =>
Text -> [Text] -> Shell ByteString -> io ExitCode
proc Text
cmd [Text]
args Shell ByteString
s
case ExitCode
exitCode of
ExitCode
ExitSuccess -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
ExitCode
_ -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => e -> IO a
Exception.throwIO (Text -> [Text] -> ExitCode -> ProcFailed
ProcFailed Text
cmd [Text]
args ExitCode
exitCode))
shells
:: MonadIO io
=> Text
-> Shell ByteString
-> io ()
shells :: forall (io :: * -> *).
MonadIO io =>
Text -> Shell ByteString -> io ()
shells Text
cmdline Shell ByteString
s = do
ExitCode
exitCode <- forall (io :: * -> *).
MonadIO io =>
Text -> Shell ByteString -> io ExitCode
shell Text
cmdline Shell ByteString
s
case ExitCode
exitCode of
ExitCode
ExitSuccess -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
ExitCode
_ -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => e -> IO a
Exception.throwIO (Text -> ExitCode -> ShellFailed
ShellFailed Text
cmdline ExitCode
exitCode))
procStrict
:: MonadIO io
=> Text
-> [Text]
-> Shell ByteString
-> io (ExitCode, ByteString)
procStrict :: forall (io :: * -> *).
MonadIO io =>
Text -> [Text] -> Shell ByteString -> io (ExitCode, ByteString)
procStrict Text
cmd [Text]
args =
forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io (ExitCode, ByteString)
systemStrict (FilePath -> [FilePath] -> CreateProcess
Process.proc (Text -> FilePath
Data.Text.unpack Text
cmd) (forall a b. (a -> b) -> [a] -> [b]
map Text -> FilePath
Data.Text.unpack [Text]
args))
shellStrict
:: MonadIO io
=> Text
-> Shell ByteString
-> io (ExitCode, ByteString)
shellStrict :: forall (io :: * -> *).
MonadIO io =>
Text -> Shell ByteString -> io (ExitCode, ByteString)
shellStrict Text
cmdline = forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io (ExitCode, ByteString)
systemStrict (FilePath -> CreateProcess
Process.shell (Text -> FilePath
Data.Text.unpack Text
cmdline))
procStrictWithErr
:: MonadIO io
=> Text
-> [Text]
-> Shell ByteString
-> io (ExitCode, ByteString, ByteString)
procStrictWithErr :: forall (io :: * -> *).
MonadIO io =>
Text
-> [Text]
-> Shell ByteString
-> io (ExitCode, ByteString, ByteString)
procStrictWithErr Text
cmd [Text]
args =
forall (io :: * -> *).
MonadIO io =>
CreateProcess
-> Shell ByteString -> io (ExitCode, ByteString, ByteString)
systemStrictWithErr (FilePath -> [FilePath] -> CreateProcess
Process.proc (Text -> FilePath
Data.Text.unpack Text
cmd) (forall a b. (a -> b) -> [a] -> [b]
map Text -> FilePath
Data.Text.unpack [Text]
args))
shellStrictWithErr
:: MonadIO io
=> Text
-> Shell ByteString
-> io (ExitCode, ByteString, ByteString)
shellStrictWithErr :: forall (io :: * -> *).
MonadIO io =>
Text -> Shell ByteString -> io (ExitCode, ByteString, ByteString)
shellStrictWithErr Text
cmdline =
forall (io :: * -> *).
MonadIO io =>
CreateProcess
-> Shell ByteString -> io (ExitCode, ByteString, ByteString)
systemStrictWithErr (FilePath -> CreateProcess
Process.shell (Text -> FilePath
Data.Text.unpack Text
cmdline))
halt :: Async a -> IO ()
halt :: forall a. Async a -> IO ()
halt Async a
a = do
Maybe (Either SomeException a)
m <- forall a. Async a -> IO (Maybe (Either SomeException a))
Async.poll Async a
a
case Maybe (Either SomeException a)
m of
Maybe (Either SomeException a)
Nothing -> forall a. Async a -> IO ()
Async.cancel Async a
a
Just (Left SomeException
e) -> forall e a. Exception e => e -> IO a
Exception.throwIO SomeException
e
Just (Right a
_) -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
system
:: MonadIO io
=> Process.CreateProcess
-> Shell ByteString
-> io ExitCode
system :: forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io ExitCode
system CreateProcess
p Shell ByteString
s = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (do
let open :: IO (Maybe Handle, ProcessHandle)
open = do
(Maybe Handle
m, Maybe Handle
Nothing, Maybe Handle
Nothing, ProcessHandle
ph) <- CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess CreateProcess
p
case Maybe Handle
m of
Just Handle
hIn -> Handle -> BufferMode -> IO ()
System.IO.hSetBuffering Handle
hIn (Maybe Int -> BufferMode
System.IO.BlockBuffering forall a. Maybe a
Nothing)
Maybe Handle
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Handle
m, ProcessHandle
ph)
MVar Bool
mvar <- forall a. a -> IO (MVar a)
MVar.newMVar Bool
False
let close :: Handle -> IO ()
close Handle
handle = do
forall a. MVar a -> (a -> IO a) -> IO ()
MVar.modifyMVar_ MVar Bool
mvar (\Bool
finalized -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Control.Monad.unless Bool
finalized
(IO () -> IO ()
ignoreSIGPIPE (Handle -> IO ()
System.IO.hClose Handle
handle))
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True )
let close' :: (Maybe Handle, ProcessHandle) -> IO ()
close' (Just Handle
hIn, ProcessHandle
ph) = do
Handle -> IO ()
close Handle
hIn
ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph
close' (Maybe Handle
Nothing , ProcessHandle
ph) = do
ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph
let handle :: (Maybe Handle, ProcessHandle) -> IO ExitCode
handle (Just Handle
hIn, ProcessHandle
ph) = do
let feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (IO () -> IO ()
ignoreSIGPIPE (forall (io :: * -> *).
MonadIO io =>
Handle -> Shell ByteString -> io ()
outhandle Handle
hIn Shell ByteString
s))
forall a b. IO a -> IO b -> IO a
`Exception.finally` Handle -> IO ()
close Handle
hIn
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore) (\Async ()
a ->
forall a. IO a -> IO a
restore (ProcessHandle -> IO ExitCode
Process.waitForProcess ProcessHandle
ph) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall a. Async a -> IO ()
halt Async ()
a ) )
handle (Maybe Handle
Nothing , ProcessHandle
ph) = do
ProcessHandle -> IO ExitCode
Process.waitForProcess ProcessHandle
ph
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Exception.bracket IO (Maybe Handle, ProcessHandle)
open (Maybe Handle, ProcessHandle) -> IO ()
close' (Maybe Handle, ProcessHandle) -> IO ExitCode
handle )
systemStrict
:: MonadIO io
=> Process.CreateProcess
-> Shell ByteString
-> io (ExitCode, ByteString)
systemStrict :: forall (io :: * -> *).
MonadIO io =>
CreateProcess -> Shell ByteString -> io (ExitCode, ByteString)
systemStrict CreateProcess
p Shell ByteString
s = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (do
let p' :: CreateProcess
p' = CreateProcess
p
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.CreatePipe
, std_err :: StdStream
Process.std_err = StdStream
Process.Inherit
}
let open :: IO (Handle, Handle, ProcessHandle)
open = do
(Just Handle
hIn, Just Handle
hOut, Maybe Handle
Nothing, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess CreateProcess
p')
Handle -> BufferMode -> IO ()
System.IO.hSetBuffering Handle
hIn (Maybe Int -> BufferMode
System.IO.BlockBuffering forall a. Maybe a
Nothing)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle
hIn, Handle
hOut, ProcessHandle
ph)
MVar Bool
mvar <- forall a. a -> IO (MVar a)
MVar.newMVar Bool
False
let close :: Handle -> IO ()
close Handle
handle = do
forall a. MVar a -> (a -> IO a) -> IO ()
MVar.modifyMVar_ MVar Bool
mvar (\Bool
finalized -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Control.Monad.unless Bool
finalized
(IO () -> IO ()
ignoreSIGPIPE (Handle -> IO ()
System.IO.hClose Handle
handle))
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True )
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Exception.bracket IO (Handle, Handle, ProcessHandle)
open (\(Handle
hIn, Handle
_, ProcessHandle
ph) -> Handle -> IO ()
close Handle
hIn forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph) (\(Handle
hIn, Handle
hOut, ProcessHandle
ph) -> do
let feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (IO () -> IO ()
ignoreSIGPIPE (forall (io :: * -> *).
MonadIO io =>
Handle -> Shell ByteString -> io ()
outhandle Handle
hIn Shell ByteString
s))
forall a b. IO a -> IO b -> IO a
`Exception.finally` Handle -> IO ()
close Handle
hIn
forall a b. IO a -> IO b -> IO (a, b)
Async.concurrently
(forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore) (\Async ()
a ->
forall a. IO a -> IO a
restore (ProcessHandle -> IO ExitCode
Process.waitForProcess ProcessHandle
ph) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall a. Async a -> IO ()
halt Async ()
a ) ))
(Handle -> IO ByteString
Data.ByteString.hGetContents Handle
hOut) ) )
systemStrictWithErr
:: MonadIO io
=> Process.CreateProcess
-> Shell ByteString
-> io (ExitCode, ByteString, ByteString)
systemStrictWithErr :: forall (io :: * -> *).
MonadIO io =>
CreateProcess
-> Shell ByteString -> io (ExitCode, ByteString, ByteString)
systemStrictWithErr CreateProcess
p Shell ByteString
s = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (do
let p' :: CreateProcess
p' = CreateProcess
p
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.CreatePipe
, std_err :: StdStream
Process.std_err = StdStream
Process.CreatePipe
}
let open :: IO (Handle, Handle, Handle, ProcessHandle)
open = do
(Just Handle
hIn, Just Handle
hOut, Just Handle
hErr, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess CreateProcess
p')
Handle -> BufferMode -> IO ()
System.IO.hSetBuffering Handle
hIn (Maybe Int -> BufferMode
System.IO.BlockBuffering forall a. Maybe a
Nothing)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle
hIn, Handle
hOut, Handle
hErr, ProcessHandle
ph)
MVar Bool
mvar <- forall a. a -> IO (MVar a)
MVar.newMVar Bool
False
let close :: Handle -> IO ()
close Handle
handle = do
forall a. MVar a -> (a -> IO a) -> IO ()
MVar.modifyMVar_ MVar Bool
mvar (\Bool
finalized -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Control.Monad.unless Bool
finalized
(IO () -> IO ()
ignoreSIGPIPE (Handle -> IO ()
System.IO.hClose Handle
handle))
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True )
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Exception.bracket IO (Handle, Handle, Handle, ProcessHandle)
open (\(Handle
hIn, Handle
_, Handle
_, ProcessHandle
ph) -> Handle -> IO ()
close Handle
hIn forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph) (\(Handle
hIn, Handle
hOut, Handle
hErr, ProcessHandle
ph) -> do
let feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (IO () -> IO ()
ignoreSIGPIPE (forall (io :: * -> *).
MonadIO io =>
Handle -> Shell ByteString -> io ()
outhandle Handle
hIn Shell ByteString
s))
forall a b. IO a -> IO b -> IO a
`Exception.finally` Handle -> IO ()
close Handle
hIn
forall a. Concurrently a -> IO a
runConcurrently forall a b. (a -> b) -> a -> b
$ (,,)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. IO a -> Concurrently a
Concurrently (forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore) (\Async ()
a ->
forall a. IO a -> IO a
restore (ProcessHandle -> IO ExitCode
Process.waitForProcess ProcessHandle
ph) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall a. Async a -> IO ()
halt Async ()
a ) ))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. IO a -> Concurrently a
Concurrently (Handle -> IO ByteString
Data.ByteString.hGetContents Handle
hOut)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall a. IO a -> Concurrently a
Concurrently (Handle -> IO ByteString
Data.ByteString.hGetContents Handle
hErr) ) )
inproc
:: Text
-> [Text]
-> Shell ByteString
-> Shell ByteString
inproc :: Text -> [Text] -> Shell ByteString -> Shell ByteString
inproc Text
cmd [Text]
args =
CreateProcess -> Shell ByteString -> Shell ByteString
stream (FilePath -> [FilePath] -> CreateProcess
Process.proc (Text -> FilePath
Data.Text.unpack Text
cmd) (forall a b. (a -> b) -> [a] -> [b]
map Text -> FilePath
Data.Text.unpack [Text]
args))
inshell
:: Text
-> Shell ByteString
-> Shell ByteString
inshell :: Text -> Shell ByteString -> Shell ByteString
inshell Text
cmd = CreateProcess -> Shell ByteString -> Shell ByteString
stream (FilePath -> CreateProcess
Process.shell (Text -> FilePath
Data.Text.unpack Text
cmd))
waitForProcessThrows :: Process.ProcessHandle -> IO ()
waitForProcessThrows :: ProcessHandle -> IO ()
waitForProcessThrows ProcessHandle
ph = do
ExitCode
exitCode <- ProcessHandle -> IO ExitCode
Process.waitForProcess ProcessHandle
ph
case ExitCode
exitCode of
ExitCode
ExitSuccess -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
ExitFailure Int
_ -> forall e a. Exception e => e -> IO a
Exception.throwIO ExitCode
exitCode
stream
:: Process.CreateProcess
-> Shell ByteString
-> Shell ByteString
stream :: CreateProcess -> Shell ByteString -> Shell ByteString
stream CreateProcess
p Shell ByteString
s = do
let p' :: CreateProcess
p' = CreateProcess
p
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.CreatePipe
, std_err :: StdStream
Process.std_err = StdStream
Process.Inherit
}
let open :: IO (Handle, Handle, ProcessHandle)
open = do
(Just Handle
hIn, Just Handle
hOut, Maybe Handle
Nothing, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess CreateProcess
p')
Handle -> BufferMode -> IO ()
System.IO.hSetBuffering Handle
hIn (Maybe Int -> BufferMode
System.IO.BlockBuffering forall a. Maybe a
Nothing)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle
hIn, Handle
hOut, ProcessHandle
ph)
MVar Bool
mvar <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. a -> IO (MVar a)
MVar.newMVar Bool
False)
let close :: Handle -> IO ()
close Handle
handle = do
forall a. MVar a -> (a -> IO a) -> IO ()
MVar.modifyMVar_ MVar Bool
mvar (\Bool
finalized -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Control.Monad.unless Bool
finalized (IO () -> IO ()
ignoreSIGPIPE (Handle -> IO ()
System.IO.hClose Handle
handle))
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True )
(Handle
hIn, Handle
hOut, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using (forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Exception.bracket IO (Handle, Handle, ProcessHandle)
open (\(Handle
hIn, Handle
_, ProcessHandle
ph) -> Handle -> IO ()
close Handle
hIn forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph)))
let feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (IO () -> IO ()
ignoreSIGPIPE (forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
hIn ByteString
bytes) ) ) )
forall a b. IO a -> IO b -> IO a
`Exception.finally` Handle -> IO ()
close Handle
hIn
Async ()
a <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using
(forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (\Async () -> IO r
k ->
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore) Async () -> IO r
k ) ))
Handle -> Shell ByteString
inhandle Handle
hOut forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ProcessHandle -> IO ()
waitForProcessThrows ProcessHandle
ph forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall a. Async a -> IO ()
halt Async ()
a) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Alternative f => f a
empty)
streamWithErr
:: Process.CreateProcess
-> Shell ByteString
-> Shell (Either ByteString ByteString)
streamWithErr :: CreateProcess
-> Shell ByteString -> Shell (Either ByteString ByteString)
streamWithErr CreateProcess
p Shell ByteString
s = do
let p' :: CreateProcess
p' = CreateProcess
p
{ std_in :: StdStream
Process.std_in = StdStream
Process.CreatePipe
, std_out :: StdStream
Process.std_out = StdStream
Process.CreatePipe
, std_err :: StdStream
Process.std_err = StdStream
Process.CreatePipe
}
let open :: IO (Handle, Handle, Handle, ProcessHandle)
open = do
(Just Handle
hIn, Just Handle
hOut, Just Handle
hErr, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (CreateProcess
-> IO (Maybe Handle, Maybe Handle, Maybe Handle, ProcessHandle)
Process.createProcess CreateProcess
p')
Handle -> BufferMode -> IO ()
System.IO.hSetBuffering Handle
hIn (Maybe Int -> BufferMode
System.IO.BlockBuffering forall a. Maybe a
Nothing)
forall (m :: * -> *) a. Monad m => a -> m a
return (Handle
hIn, Handle
hOut, Handle
hErr, ProcessHandle
ph)
MVar Bool
mvar <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. a -> IO (MVar a)
MVar.newMVar Bool
False)
let close :: Handle -> IO ()
close Handle
handle = do
forall a. MVar a -> (a -> IO a) -> IO ()
MVar.modifyMVar_ MVar Bool
mvar (\Bool
finalized -> do
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
Control.Monad.unless Bool
finalized (IO () -> IO ()
ignoreSIGPIPE (Handle -> IO ()
System.IO.hClose Handle
handle))
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True )
(Handle
hIn, Handle
hOut, Handle
hErr, ProcessHandle
ph) <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using (forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
Exception.bracket IO (Handle, Handle, Handle, ProcessHandle)
open (\(Handle
hIn, Handle
_, Handle
_, ProcessHandle
ph) -> Handle -> IO ()
close Handle
hIn forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> ProcessHandle -> IO ()
Process.terminateProcess ProcessHandle
ph)))
let feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn :: (forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (IO () -> IO ()
ignoreSIGPIPE (forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Shell ByteString
s
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> ByteString -> IO ()
Data.ByteString.hPut Handle
hIn ByteString
bytes) ) ) )
forall a b. IO a -> IO b -> IO a
`Exception.finally` Handle -> IO ()
close Handle
hIn
TQueue (Maybe (Either ByteString ByteString))
queue <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a. IO (TQueue a)
TQueue.newTQueueIO
let forwardOut :: (forall a. IO a -> IO a) -> IO ()
forwardOut :: (forall a. IO a -> IO a) -> IO ()
forwardOut forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Handle -> Shell ByteString
inhandle Handle
hOut
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. STM a -> IO a
STM.atomically (forall a. TQueue a -> a -> STM ()
TQueue.writeTQueue TQueue (Maybe (Either ByteString ByteString))
queue (forall a. a -> Maybe a
Just (forall a b. b -> Either a b
Right ByteString
bytes)))) ))
forall a b. IO a -> IO b -> IO a
`Exception.finally` forall a. STM a -> IO a
STM.atomically (forall a. TQueue a -> a -> STM ()
TQueue.writeTQueue TQueue (Maybe (Either ByteString ByteString))
queue forall a. Maybe a
Nothing)
let forwardErr :: (forall a. IO a -> IO a) -> IO ()
forwardErr :: (forall a. IO a -> IO a) -> IO ()
forwardErr forall a. IO a -> IO a
restore =
forall a. IO a -> IO a
restore (forall (io :: * -> *) a. MonadIO io => Shell a -> io ()
sh (do
ByteString
bytes <- Handle -> Shell ByteString
inhandle Handle
hErr
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. STM a -> IO a
STM.atomically (forall a. TQueue a -> a -> STM ()
TQueue.writeTQueue TQueue (Maybe (Either ByteString ByteString))
queue (forall a. a -> Maybe a
Just (forall a b. a -> Either a b
Left ByteString
bytes)))) ))
forall a b. IO a -> IO b -> IO a
`Exception.finally` forall a. STM a -> IO a
STM.atomically (forall a. TQueue a -> a -> STM ()
TQueue.writeTQueue TQueue (Maybe (Either ByteString ByteString))
queue forall a. Maybe a
Nothing)
let drain :: Shell (Either ByteString ByteString)
drain = forall a. (forall r. FoldShell a r -> IO r) -> Shell a
Shell (\(FoldShell x -> Either ByteString ByteString -> IO x
step x
begin x -> IO r
done) -> do
let loop :: x -> a -> IO x
loop x
x a
numNothing
| a
numNothing forall a. Ord a => a -> a -> Bool
< a
2 = do
Maybe (Either ByteString ByteString)
m <- forall a. STM a -> IO a
STM.atomically (forall a. TQueue a -> STM a
TQueue.readTQueue TQueue (Maybe (Either ByteString ByteString))
queue)
case Maybe (Either ByteString ByteString)
m of
Maybe (Either ByteString ByteString)
Nothing -> x -> a -> IO x
loop x
x forall a b. (a -> b) -> a -> b
$! a
numNothing forall a. Num a => a -> a -> a
+ a
1
Just Either ByteString ByteString
e -> do
x
x' <- x -> Either ByteString ByteString -> IO x
step x
x Either ByteString ByteString
e
x -> a -> IO x
loop x
x' a
numNothing
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return x
x
x
x1 <- forall {a}. (Ord a, Num a) => x -> a -> IO x
loop x
begin (Int
0 :: Int)
x -> IO r
done x
x1 )
Async ()
a <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using
(forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (\Async () -> IO r
k ->
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
feedIn forall a. IO a -> IO a
restore) Async () -> IO r
k ) ))
Async ()
b <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using
(forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (\Async () -> IO r
k ->
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
forwardOut forall a. IO a -> IO a
restore) Async () -> IO r
k ) ))
Async ()
c <- forall (m :: * -> *) a. MonadManaged m => Managed a -> m a
using
(forall (m :: * -> *) a.
MonadManaged m =>
(forall r. (a -> IO r) -> IO r) -> m a
Managed.managed (\Async () -> IO r
k ->
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
Exception.mask (\forall a. IO a -> IO a
restore ->
forall a b. IO a -> (Async a -> IO b) -> IO b
Async.withAsync ((forall a. IO a -> IO a) -> IO ()
forwardErr forall a. IO a -> IO a
restore) Async () -> IO r
k ) ))
let STM a
l also :: STM a -> STM a -> STM ()
`also` STM a
r = do
a
_ <- STM a
l forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (STM a
r forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall a. STM a
STM.retry)
a
_ <- STM a
r
forall (m :: * -> *) a. Monad m => a -> m a
return ()
let waitAll :: IO ()
waitAll = forall a. STM a -> IO a
STM.atomically (forall a. Async a -> STM a
Async.waitSTM Async ()
a forall {a} {a}. STM a -> STM a -> STM ()
`also` (forall a. Async a -> STM a
Async.waitSTM Async ()
b forall {a} {a}. STM a -> STM a -> STM ()
`also` forall a. Async a -> STM a
Async.waitSTM Async ()
c))
Shell (Either ByteString ByteString)
drain forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (ProcessHandle -> IO ()
waitForProcessThrows ProcessHandle
ph forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> IO ()
waitAll) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Alternative f => f a
empty)
inprocWithErr
:: Text
-> [Text]
-> Shell ByteString
-> Shell (Either ByteString ByteString)
inprocWithErr :: Text
-> [Text]
-> Shell ByteString
-> Shell (Either ByteString ByteString)
inprocWithErr Text
cmd [Text]
args =
CreateProcess
-> Shell ByteString -> Shell (Either ByteString ByteString)
streamWithErr (FilePath -> [FilePath] -> CreateProcess
Process.proc (Text -> FilePath
Data.Text.unpack Text
cmd) (forall a b. (a -> b) -> [a] -> [b]
map Text -> FilePath
Data.Text.unpack [Text]
args))
inshellWithErr
:: Text
-> Shell ByteString
-> Shell (Either ByteString ByteString)
inshellWithErr :: Text -> Shell ByteString -> Shell (Either ByteString ByteString)
inshellWithErr Text
cmd = CreateProcess
-> Shell ByteString -> Shell (Either ByteString ByteString)
streamWithErr (FilePath -> CreateProcess
Process.shell (Text -> FilePath
Data.Text.unpack Text
cmd))
fromPopper :: Popper -> Shell ByteString
fromPopper :: Popper -> Shell ByteString
fromPopper Popper
popper = Shell ByteString
loop
where
loop :: Shell ByteString
loop = do
PopperRes
result <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO Popper
popper
case PopperRes
result of
PopperRes
PRDone ->
forall (f :: * -> *) a. Alternative f => f a
empty
PRNext ByteString
compressedByteString ->
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
compressedByteString forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Shell ByteString
loop
PRError ZlibException
exception ->
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall e a. Exception e => e -> IO a
Exception.throwIO ZlibException
exception)
compress
:: Int
-> WindowBits
-> Shell ByteString
-> Shell ByteString
compress :: Int -> WindowBits -> Shell ByteString -> Shell ByteString
compress Int
compressionLevel WindowBits
windowBits Shell ByteString
bytestrings = do
Deflate
deflate <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Int -> WindowBits -> IO Deflate
Zlib.initDeflate Int
compressionLevel WindowBits
windowBits)
let loop :: Shell ByteString
loop = do
ByteString
bytestring <- Shell ByteString
bytestrings
Popper
popper <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Deflate -> ByteString -> IO Popper
Zlib.feedDeflate Deflate
deflate ByteString
bytestring)
Popper -> Shell ByteString
fromPopper Popper
popper
let wrapUp :: Shell ByteString
wrapUp = do
let popper :: Popper
popper = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Deflate -> Popper
Zlib.finishDeflate Deflate
deflate)
Popper -> Shell ByteString
fromPopper Popper
popper
Shell ByteString
loop forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Shell ByteString
wrapUp
data DecompressionState = Uninitialized | Decompressing Inflate
decompress :: WindowBits -> Shell ByteString -> Shell ByteString
decompress :: WindowBits -> Shell ByteString -> Shell ByteString
decompress WindowBits
windowBits (Shell forall r. FoldShell ByteString r -> IO r
k) = forall a. (forall r. FoldShell a r -> IO r) -> Shell a
Shell forall r. FoldShell ByteString r -> IO r
k'
where
k' :: FoldShell ByteString b -> IO b
k' (FoldShell x -> ByteString -> IO x
step x
begin x -> IO b
done) = forall r. FoldShell ByteString r -> IO r
k (forall a b x. (x -> a -> IO x) -> x -> (x -> IO b) -> FoldShell a b
FoldShell (x, DecompressionState) -> ByteString -> IO (x, DecompressionState)
step' (x, DecompressionState)
begin' (x, DecompressionState) -> IO b
done')
where
begin' :: (x, DecompressionState)
begin' = (x
begin, DecompressionState
Uninitialized)
step' :: (x, DecompressionState) -> ByteString -> IO (x, DecompressionState)
step' (x
x0, DecompressionState
Uninitialized) ByteString
compressedByteString = do
Inflate
inflate <- WindowBits -> IO Inflate
Zlib.initInflate WindowBits
windowBits
(x, DecompressionState) -> ByteString -> IO (x, DecompressionState)
step' (x
x0, Inflate -> DecompressionState
Decompressing Inflate
inflate) ByteString
compressedByteString
step' (x
x0, Decompressing Inflate
inflate) ByteString
compressedByteString = do
Popper
popper <- Inflate -> ByteString -> IO Popper
Zlib.feedInflate Inflate
inflate ByteString
compressedByteString
let loop :: x -> IO (x, DecompressionState)
loop x
x = do
PopperRes
result <- Popper
popper
case PopperRes
result of
PopperRes
PRDone -> do
ByteString
compressedByteString' <- Inflate -> IO ByteString
Zlib.getUnusedInflate Inflate
inflate
if ByteString -> Bool
Data.ByteString.null ByteString
compressedByteString'
then forall (m :: * -> *) a. Monad m => a -> m a
return (x
x, Inflate -> DecompressionState
Decompressing Inflate
inflate)
else do
ByteString
decompressedByteString <- Inflate -> IO ByteString
Zlib.finishInflate Inflate
inflate
x
x' <- x -> ByteString -> IO x
step x
x ByteString
decompressedByteString
(x, DecompressionState) -> ByteString -> IO (x, DecompressionState)
step' (x
x', DecompressionState
Uninitialized) ByteString
compressedByteString'
PRNext ByteString
decompressedByteString -> do
x
x' <- x -> ByteString -> IO x
step x
x ByteString
decompressedByteString
x -> IO (x, DecompressionState)
loop x
x'
PRError ZlibException
exception -> do
forall e a. Exception e => e -> IO a
Exception.throwIO ZlibException
exception
x -> IO (x, DecompressionState)
loop x
x0
done' :: (x, DecompressionState) -> IO b
done' (x
x0, DecompressionState
Uninitialized) = do
x -> IO b
done x
x0
done' (x
x0, Decompressing Inflate
inflate) = do
ByteString
decompressedByteString <- Inflate -> IO ByteString
Zlib.finishInflate Inflate
inflate
x
x0' <- x -> ByteString -> IO x
step x
x0 ByteString
decompressedByteString
(x, DecompressionState) -> IO b
done' (x
x0', DecompressionState
Uninitialized)
toUTF8 :: Shell ByteString -> Shell Text
toUTF8 :: Shell ByteString -> Shell Text
toUTF8 (Shell forall r. FoldShell ByteString r -> IO r
k) = forall a. (forall r. FoldShell a r -> IO r) -> Shell a
Shell forall {b}. FoldShell Text b -> IO b
k'
where
k' :: FoldShell Text b -> IO b
k' (FoldShell x -> Text -> IO x
step x
begin x -> IO b
done) =
forall r. FoldShell ByteString r -> IO r
k (forall a b x. (x -> a -> IO x) -> x -> (x -> IO b) -> FoldShell a b
FoldShell forall {a}.
Semigroup a =>
(a, a -> Decoding, x)
-> a -> IO (ByteString, ByteString -> Decoding, x)
step' (ByteString, ByteString -> Decoding, x)
begin' forall {a} {b}. (a, b, x) -> IO b
done')
where
begin' :: (ByteString, ByteString -> Decoding, x)
begin' =
(forall a. Monoid a => a
mempty, OnDecodeError -> ByteString -> Decoding
Encoding.streamDecodeUtf8With OnDecodeError
Encoding.Error.strictDecode, x
begin)
step' :: (a, a -> Decoding, x)
-> a -> IO (ByteString, ByteString -> Decoding, x)
step' (a
prefix, a -> Decoding
decoder, x
x) a
suffix = do
let bytes :: a
bytes = a
prefix forall a. Semigroup a => a -> a -> a
<> a
suffix
let Some Text
text ByteString
prefix' ByteString -> Decoding
decoder' = a -> Decoding
decoder a
bytes
x
x' <- x -> Text -> IO x
step x
x Text
text
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString
prefix', ByteString -> Decoding
decoder', x
x')
done' :: (a, b, x) -> IO b
done' (a
_, b
_, x
x) = do
x -> IO b
done x
x
fromUTF8 :: Shell Text -> Shell ByteString
fromUTF8 :: Shell Text -> Shell ByteString
fromUTF8 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> ByteString
Encoding.encodeUtf8