module Control.Monad.Scoped.Handle
(
ScopedHandle
, file
, IO.IOMode (..)
, hPutStrLn
, hPutStr
, hGetLine
, hGetContents
)
where
import Control.Monad.Scoped.Internal (Scoped (UnsafeMkScoped), ScopedResource (UnsafeMkScopedResource, unsafeUnwrapScopedResource), (:<))
import Data.Text (Text)
import Data.Text.IO qualified as TIO
import System.IO qualified as IO
import UnliftIO (MonadIO (liftIO), MonadUnliftIO)
import UnliftIO.IO qualified as IOU
type ScopedHandle s = ScopedResource s IO.Handle
file :: MonadUnliftIO m => FilePath -> IO.IOMode -> Scoped (s : ss) m (ScopedHandle s)
file :: forall (m :: Type -> Type) s (ss :: [Type]).
MonadUnliftIO m =>
FilePath -> IOMode -> Scoped (s : ss) m (ScopedHandle s)
file FilePath
path IOMode
mode = (forall b. (ScopedHandle s -> m b) -> m b)
-> Scoped (s : ss) m (ScopedHandle s)
forall {k} (s :: [Type]) (m :: k -> Type) a.
(forall (b :: k). (a -> m b) -> m b) -> Scoped s m a
UnsafeMkScoped \ScopedHandle s -> m b
k -> IO () -> m ()
forall a. IO a -> m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"opening file " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
path) m () -> m b -> m b
forall a b. m a -> m b -> m b
forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f b
*> FilePath -> IOMode -> (Handle -> m b) -> m b
forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
IOU.withFile FilePath
path IOMode
mode (ScopedHandle s -> m b
k (ScopedHandle s -> m b)
-> (Handle -> ScopedHandle s) -> Handle -> m b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> ScopedHandle s
forall s a. a -> ScopedResource s a
UnsafeMkScopedResource) m b -> m () -> m b
forall a b. m a -> m b -> m a
forall (f :: Type -> Type) a b. Applicative f => f a -> f b -> f a
<* IO () -> m ()
forall a. IO a -> m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ FilePath
"closing file " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
path)
hPutStrLn :: (MonadIO m, s :< ss) => ScopedHandle s -> Text -> Scoped ss m ()
hPutStrLn :: forall (m :: Type -> Type) s (ss :: [Type]).
(MonadIO m, s :< ss) =>
ScopedHandle s -> Text -> Scoped ss m ()
hPutStrLn ScopedHandle s
h Text
s = IO () -> Scoped ss m ()
forall a. IO a -> Scoped ss m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (Handle -> Text -> IO ()
TIO.hPutStrLn (ScopedHandle s -> Handle
forall s a. ScopedResource s a -> a
unsafeUnwrapScopedResource ScopedHandle s
h) Text
s)
hPutStr :: (MonadIO m, s :< ss) => ScopedHandle s -> Text -> Scoped ss m ()
hPutStr :: forall (m :: Type -> Type) s (ss :: [Type]).
(MonadIO m, s :< ss) =>
ScopedHandle s -> Text -> Scoped ss m ()
hPutStr ScopedHandle s
h Text
s = IO () -> Scoped ss m ()
forall a. IO a -> Scoped ss m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (Handle -> Text -> IO ()
TIO.hPutStr (ScopedHandle s -> Handle
forall s a. ScopedResource s a -> a
unsafeUnwrapScopedResource ScopedHandle s
h) Text
s)
hGetLine :: (MonadIO m, s :< ss) => ScopedHandle s -> Scoped ss m Text
hGetLine :: forall (m :: Type -> Type) s (ss :: [Type]).
(MonadIO m, s :< ss) =>
ScopedHandle s -> Scoped ss m Text
hGetLine ScopedHandle s
h = IO Text -> Scoped ss m Text
forall a. IO a -> Scoped ss m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO Text
TIO.hGetLine (ScopedHandle s -> Handle
forall s a. ScopedResource s a -> a
unsafeUnwrapScopedResource ScopedHandle s
h))
hGetContents :: (MonadIO m, s :< ss) => ScopedHandle s -> Scoped ss m Text
hGetContents :: forall (m :: Type -> Type) s (ss :: [Type]).
(MonadIO m, s :< ss) =>
ScopedHandle s -> Scoped ss m Text
hGetContents ScopedHandle s
h = IO Text -> Scoped ss m Text
forall a. IO a -> Scoped ss m a
forall (m :: Type -> Type) a. MonadIO m => IO a -> m a
liftIO (Handle -> IO Text
TIO.hGetContents (ScopedHandle s -> Handle
forall s a. ScopedResource s a -> a
unsafeUnwrapScopedResource ScopedHandle s
h))