{-# LANGUAGE RankNTypes #-}
module Pipes.Text.IO
(
fromHandle,
stdin,
readFile,
toHandle,
stdout,
writeFile,
MonadSafe (..),
runSafeT,
runSafeP,
Safe.withFile,
)
where
import Control.Exception (throwIO, try)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Foreign.C.Error (Errno (Errno), ePIPE)
import qualified GHC.IO.Exception as G
import Pipes
import Pipes.Safe (MonadSafe (..), runSafeP, runSafeT)
import qualified Pipes.Safe.Prelude as Safe
import qualified System.IO as IO
import Prelude hiding (readFile, writeFile)
fromHandle :: MonadIO m => IO.Handle -> Producer Text m ()
fromHandle :: Handle -> Producer Text m ()
fromHandle Handle
h = Producer Text m ()
forall x' x. Proxy x' x () Text m ()
go
where
go :: Proxy x' x () Text m ()
go = do
Text
txt <- IO Text -> Proxy x' x () Text m Text
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO Text
T.hGetChunk Handle
h)
if Text -> Bool
T.null Text
txt
then () -> Proxy x' x () Text m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
else do
Text -> Proxy x' x () Text m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield Text
txt
Proxy x' x () Text m ()
go
Proxy x' x () Text m ()
go
{-# INLINEABLE fromHandle #-}
stdin :: MonadIO m => Producer Text m ()
stdin :: Producer Text m ()
stdin = Handle -> Producer Text m ()
forall (m :: * -> *). MonadIO m => Handle -> Producer Text m ()
fromHandle Handle
IO.stdin
{-# INLINE stdin #-}
readFile :: MonadSafe m => FilePath -> Producer Text m ()
readFile :: FilePath -> Producer Text m ()
readFile FilePath
file = FilePath
-> IOMode -> (Handle -> Producer Text m ()) -> Producer Text m ()
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
Safe.withFile FilePath
file IOMode
IO.ReadMode Handle -> Producer Text m ()
forall (m :: * -> *). MonadIO m => Handle -> Producer Text m ()
fromHandle
{-# INLINE readFile #-}
stdout :: MonadIO m => Consumer' Text m ()
stdout :: Consumer' Text m ()
stdout = Proxy () Text y' y m ()
Consumer' Text m ()
go
where
go :: Proxy () Text y' y m ()
go = do
Text
txt <- Proxy () Text y' y m Text
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
Either IOException ()
x <- IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ()))
-> IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOException ())
forall e a. Exception e => IO a -> IO (Either e a)
try (Text -> IO ()
T.putStr Text
txt)
case Either IOException ()
x of
Left
G.IOError
{ ioe_type :: IOException -> IOErrorType
G.ioe_type = IOErrorType
G.ResourceVanished,
ioe_errno :: IOException -> Maybe CInt
G.ioe_errno = Just CInt
ioe
}
| CInt -> Errno
Errno CInt
ioe Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
ePIPE ->
() -> Proxy () Text y' y m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Left IOException
e -> IO () -> Proxy () Text y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO IOException
e)
Right () -> Proxy () Text y' y m ()
go
{-# INLINEABLE stdout #-}
toHandle :: MonadIO m => IO.Handle -> Consumer' Text m r
toHandle :: Handle -> Consumer' Text m r
toHandle Handle
h = Proxy () Text () Text m r
-> (Text -> Proxy () Text y' y m ()) -> Proxy () Text y' y m r
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy () Text () Text m r
forall (m :: * -> *) a r. Functor m => Pipe a a m r
cat (IO () -> Proxy () Text y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Proxy () Text y' y m ())
-> (Text -> IO ()) -> Text -> Proxy () Text y' y m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Text -> IO ()
T.hPutStr Handle
h)
{-# INLINEABLE toHandle #-}
writeFile :: (MonadSafe m) => FilePath -> Consumer' Text m ()
writeFile :: FilePath -> Consumer' Text m ()
writeFile FilePath
file = FilePath
-> IOMode
-> (Handle -> Proxy () Text y' y m ())
-> Proxy () Text y' y m ()
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
Safe.withFile FilePath
file IOMode
IO.WriteMode (\Handle
h -> Handle -> Consumer' Text m ()
forall (m :: * -> *) r. MonadIO m => Handle -> Consumer' Text m r
toHandle Handle
h)
{-# INLINE writeFile #-}