{-# LANGUAGE Safe #-}
module Universum.Lifted.File
( appendFile
, getLine
, readFile
, writeFile
, withFile
, openFile
, hClose
) where
import Control.Exception.Safe (MonadMask, bracket)
import Control.Monad.Trans (MonadIO, liftIO)
import Data.Function ((.))
import Data.Text (Text)
import Prelude (FilePath)
import System.IO (Handle, IOMode)
import qualified Data.Text.IO as XIO
import qualified System.IO as XIO (IO, hClose, openFile)
appendFile :: MonadIO m => FilePath -> Text -> m ()
appendFile :: FilePath -> Text -> m ()
appendFile FilePath
a Text
b = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> Text -> IO ()
XIO.appendFile FilePath
a Text
b)
{-# INLINE appendFile #-}
getLine :: MonadIO m => m Text
getLine :: m Text
getLine = IO Text -> m Text
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO Text
XIO.getLine
{-# INLINE getLine #-}
readFile :: MonadIO m => FilePath -> m Text
readFile :: FilePath -> m Text
readFile FilePath
a = IO Text -> m Text
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO Text
XIO.readFile FilePath
a)
{-# INLINE readFile #-}
writeFile :: MonadIO m => FilePath -> Text -> m ()
writeFile :: FilePath -> Text -> m ()
writeFile FilePath
a Text
b = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> Text -> IO ()
XIO.writeFile FilePath
a Text
b)
{-# INLINE writeFile #-}
openFile :: MonadIO m => FilePath -> IOMode -> m Handle
openFile :: FilePath -> IOMode -> m Handle
openFile FilePath
a IOMode
b = IO Handle -> m Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IOMode -> IO Handle
XIO.openFile FilePath
a IOMode
b)
{-# INLINE openFile #-}
hClose :: MonadIO m => Handle -> m ()
hClose :: Handle -> m ()
hClose = 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 ()
XIO.hClose
{-# INLINE hClose #-}
withFile :: (MonadIO m, MonadMask m) => FilePath -> IOMode -> (Handle -> m a) -> m a
withFile :: FilePath -> IOMode -> (Handle -> m a) -> m a
withFile FilePath
filePath IOMode
mode Handle -> m a
f = m Handle -> (Handle -> m ()) -> (Handle -> m a) -> m a
forall (m :: * -> *) a b c.
MonadMask m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket (FilePath -> IOMode -> m Handle
forall (m :: * -> *). MonadIO m => FilePath -> IOMode -> m Handle
openFile FilePath
filePath IOMode
mode) Handle -> m ()
forall (m :: * -> *). MonadIO m => Handle -> m ()
hClose Handle -> m a
f
{-# SPECIALIZE appendFile :: FilePath -> Text -> XIO.IO () #-}
{-# SPECIALIZE getLine :: XIO.IO Text #-}
{-# SPECIALIZE readFile :: FilePath -> XIO.IO Text #-}
{-# SPECIALIZE writeFile :: FilePath -> Text -> XIO.IO () #-}
{-# SPECIALIZE openFile :: FilePath -> IOMode -> XIO.IO Handle #-}
{-# SPECIALIZE hClose :: Handle -> XIO.IO () #-}
{-# SPECIALIZE withFile :: FilePath -> IOMode -> (Handle -> XIO.IO a) -> XIO.IO a #-}