module Streaming.With
(
withFile
, withBinaryFile
, writeBinaryFile
, appendBinaryFile
, withBinaryFileContents
, withSystemTempFile
, withTempFile
, withSystemTempDirectory
, withTempDirectory
, MonadMask
, bracket
) where
import Streaming.ByteString (ByteStream)
import qualified Streaming.ByteString as B
import Control.Monad.Catch (MonadMask, bracket)
import Control.Monad.IO.Class (MonadIO, liftIO)
import System.IO (Handle, IOMode (..), hClose,
openBinaryFile, openFile)
import System.IO.Temp (withSystemTempDirectory,
withTempDirectory)
import qualified System.IO.Temp as T
withFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r
withFile :: FilePath -> IOMode -> (Handle -> m r) -> m r
withFile FilePath
fp IOMode
md = m Handle -> (Handle -> m ()) -> (Handle -> m r) -> m r
forall (m :: * -> *) a c b.
MonadMask m =>
m a -> (a -> m c) -> (a -> m b) -> m b
bracket (IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IOMode -> IO Handle
openFile FilePath
fp IOMode
md)) (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Handle -> IO ()) -> Handle -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO ()
hClose)
withBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> IOMode -> (Handle -> m r) -> m r
withBinaryFile :: FilePath -> IOMode -> (Handle -> m r) -> m r
withBinaryFile FilePath
fp IOMode
md = m Handle -> (Handle -> m ()) -> (Handle -> m r) -> m r
forall (m :: * -> *) a c b.
MonadMask m =>
m a -> (a -> m c) -> (a -> m b) -> m b
bracket (IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IOMode -> IO Handle
openBinaryFile FilePath
fp IOMode
md)) (IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> (Handle -> IO ()) -> Handle -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO ()
hClose)
writeBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteStream m r -> m r
writeBinaryFile :: FilePath -> ByteStream m r -> m r
writeBinaryFile FilePath
fp = FilePath -> IOMode -> (Handle -> m r) -> m r
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withBinaryFile FilePath
fp IOMode
WriteMode ((Handle -> m r) -> m r)
-> (ByteStream m r -> Handle -> m r) -> ByteStream m r -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Handle -> ByteStream m r -> m r)
-> ByteStream m r -> Handle -> m r
forall a b c. (a -> b -> c) -> b -> a -> c
flip Handle -> ByteStream m r -> m r
forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
B.hPut
appendBinaryFile :: (MonadMask m, MonadIO m) => FilePath -> ByteStream m r -> m r
appendBinaryFile :: FilePath -> ByteStream m r -> m r
appendBinaryFile FilePath
fp = FilePath -> IOMode -> (Handle -> m r) -> m r
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withBinaryFile FilePath
fp IOMode
AppendMode ((Handle -> m r) -> m r)
-> (ByteStream m r -> Handle -> m r) -> ByteStream m r -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Handle -> ByteStream m r -> m r)
-> ByteStream m r -> Handle -> m r
forall a b c. (a -> b -> c) -> b -> a -> c
flip Handle -> ByteStream m r -> m r
forall (m :: * -> *) r.
MonadIO m =>
Handle -> ByteStream m r -> m r
B.hPut
withBinaryFileContents :: (MonadMask m, MonadIO m, MonadIO n) => FilePath
-> (ByteStream n () -> m r) -> m r
withBinaryFileContents :: FilePath -> (ByteStream n () -> m r) -> m r
withBinaryFileContents FilePath
fp ByteStream n () -> m r
f = FilePath -> IOMode -> (Handle -> m r) -> m r
forall (m :: * -> *) r.
(MonadMask m, MonadIO m) =>
FilePath -> IOMode -> (Handle -> m r) -> m r
withBinaryFile FilePath
fp IOMode
ReadMode (ByteStream n () -> m r
f (ByteStream n () -> m r)
-> (Handle -> ByteStream n ()) -> Handle -> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ByteStream n ()
forall (m :: * -> *). MonadIO m => Handle -> ByteStream m ()
B.hGetContents)
withSystemTempFile :: (MonadIO m, MonadMask m)
=> String
-> ((FilePath, Handle) -> m r)
-> m r
withSystemTempFile :: FilePath -> ((FilePath, Handle) -> m r) -> m r
withSystemTempFile FilePath
template = FilePath -> (FilePath -> Handle -> m r) -> m r
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
FilePath -> (FilePath -> Handle -> m a) -> m a
T.withSystemTempFile FilePath
template ((FilePath -> Handle -> m r) -> m r)
-> (((FilePath, Handle) -> m r) -> FilePath -> Handle -> m r)
-> ((FilePath, Handle) -> m r)
-> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FilePath, Handle) -> m r) -> FilePath -> Handle -> m r
forall a b c. ((a, b) -> c) -> a -> b -> c
curry
withTempFile :: (MonadIO m, MonadMask m)
=> FilePath
-> String
-> ((FilePath, Handle) -> m r)
-> m r
withTempFile :: FilePath -> FilePath -> ((FilePath, Handle) -> m r) -> m r
withTempFile FilePath
dir FilePath
template = FilePath -> FilePath -> (FilePath -> Handle -> m r) -> m r
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
FilePath -> FilePath -> (FilePath -> Handle -> m a) -> m a
T.withTempFile FilePath
dir FilePath
template ((FilePath -> Handle -> m r) -> m r)
-> (((FilePath, Handle) -> m r) -> FilePath -> Handle -> m r)
-> ((FilePath, Handle) -> m r)
-> m r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FilePath, Handle) -> m r) -> FilePath -> Handle -> m r
forall a b c. ((a, b) -> c) -> a -> b -> c
curry