-- | Lifted "UnliftIO.IO.File".
module Effectful.FileSystem.IO.File
  ( writeBinaryFile
  , writeBinaryFileAtomic
  , writeBinaryFileDurable
  , writeBinaryFileDurableAtomic

  , withBinaryFile
  , withBinaryFileAtomic
  , withBinaryFileDurable
  , withBinaryFileDurableAtomic

  , ensureFileDurable
  ) where

import Data.ByteString (ByteString)
import System.IO (Handle, IOMode (..))
import qualified UnliftIO.IO.File as U

import Effectful
import Effectful.Dispatch.Static
import Effectful.FileSystem.Effect

-- | Lifted 'U.writeBinaryFile'.
writeBinaryFile :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
writeBinaryFile :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
writeBinaryFile FilePath
path = forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type).
MonadIO m =>
FilePath -> ByteString -> m ()
U.writeBinaryFile FilePath
path

-- | Lifted 'U.writeBinaryFileAtomic'.
writeBinaryFileAtomic :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
writeBinaryFileAtomic :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
writeBinaryFileAtomic FilePath
path = forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type).
MonadIO m =>
FilePath -> ByteString -> m ()
U.writeBinaryFileAtomic FilePath
path

-- | Lifted 'U.writeBinaryFileDurable'.
writeBinaryFileDurable :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
writeBinaryFileDurable :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
writeBinaryFileDurable FilePath
path = forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type).
MonadIO m =>
FilePath -> ByteString -> m ()
U.writeBinaryFileDurable FilePath
path

-- | Lifted 'U.writeBinaryFileDurableAtomic'.
writeBinaryFileDurableAtomic :: FileSystem :> es => FilePath -> ByteString -> Eff es ()
writeBinaryFileDurableAtomic :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> ByteString -> Eff es ()
writeBinaryFileDurableAtomic FilePath
path = forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type).
MonadIO m =>
FilePath -> ByteString -> m ()
U.writeBinaryFileDurableAtomic FilePath
path

----------------------------------------

-- | Lifted 'U.withBinaryFile'.
withBinaryFile
  :: FileSystem :> es => FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFile :: forall (es :: [Effect]) a.
(FileSystem :> es) =>
FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFile FilePath
path IOMode
mode Handle -> Eff es a
inner = forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFile FilePath
path IOMode
mode forall a b. (a -> b) -> a -> b
$ forall r. Eff es r -> IO r
unlift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Eff es a
inner

-- | Lifted 'U.withBinaryFileAtomic'.
withBinaryFileAtomic
  :: FileSystem :> es => FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileAtomic :: forall (es :: [Effect]) a.
(FileSystem :> es) =>
FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileAtomic FilePath
path IOMode
mode Handle -> Eff es a
inner = forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileAtomic FilePath
path IOMode
mode forall a b. (a -> b) -> a -> b
$ forall r. Eff es r -> IO r
unlift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Eff es a
inner

-- | Lifted 'U.withBinaryFileDurable'.
withBinaryFileDurable
  :: FileSystem :> es => FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileDurable :: forall (es :: [Effect]) a.
(FileSystem :> es) =>
FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileDurable FilePath
path IOMode
mode Handle -> Eff es a
inner = forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileDurable FilePath
path IOMode
mode forall a b. (a -> b) -> a -> b
$ forall r. Eff es r -> IO r
unlift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Eff es a
inner

-- | Lifted 'U.withBinaryFileDurableAtomic'.
withBinaryFileDurableAtomic
  :: FileSystem :> es => FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileDurableAtomic :: forall (es :: [Effect]) a.
(FileSystem :> es) =>
FilePath -> IOMode -> (Handle -> Eff es a) -> Eff es a
withBinaryFileDurableAtomic FilePath
path IOMode
mode Handle -> Eff es a
inner = forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileDurableAtomic FilePath
path IOMode
mode forall a b. (a -> b) -> a -> b
$ forall r. Eff es r -> IO r
unlift forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Eff es a
inner

----------------------------------------

-- | Lifted 'U.ensureFileDurable'.
ensureFileDurable :: FileSystem :> es => FilePath -> Eff es ()
ensureFileDurable :: forall (es :: [Effect]).
(FileSystem :> es) =>
FilePath -> Eff es ()
ensureFileDurable = forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: Type -> Type). MonadIO m => FilePath -> m ()
U.ensureFileDurable