-- | 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 UnliftIO.IO.File qualified 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 = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
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 = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
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 = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
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 = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (ByteString -> IO ()) -> ByteString -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> ByteString -> IO ()
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 r. Eff es r -> IO r) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO (((forall r. Eff es r -> IO r) -> IO a) -> Eff es a)
-> ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFile FilePath
path IOMode
mode ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ Eff es a -> IO a
forall r. Eff es r -> IO r
unlift (Eff es a -> IO a) -> (Handle -> Eff es a) -> Handle -> IO a
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 r. Eff es r -> IO r) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO (((forall r. Eff es r -> IO r) -> IO a) -> Eff es a)
-> ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileAtomic FilePath
path IOMode
mode ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ Eff es a -> IO a
forall r. Eff es r -> IO r
unlift (Eff es a -> IO a) -> (Handle -> Eff es a) -> Handle -> IO a
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 r. Eff es r -> IO r) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO (((forall r. Eff es r -> IO r) -> IO a) -> Eff es a)
-> ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileDurable FilePath
path IOMode
mode ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ Eff es a -> IO a
forall r. Eff es r -> IO r
unlift (Eff es a -> IO a) -> (Handle -> Eff es a) -> Handle -> IO a
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 r. Eff es r -> IO r) -> IO a) -> Eff es a
forall (es :: [Effect]) a.
HasCallStack =>
((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
unsafeSeqUnliftIO (((forall r. Eff es r -> IO r) -> IO a) -> Eff es a)
-> ((forall r. Eff es r -> IO r) -> IO a) -> Eff es a
forall a b. (a -> b) -> a -> b
$ \forall r. Eff es r -> IO r
unlift -> do
  FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall (m :: Type -> Type) a.
MonadUnliftIO m =>
FilePath -> IOMode -> (Handle -> m a) -> m a
U.withBinaryFileDurableAtomic FilePath
path IOMode
mode ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ Eff es a -> IO a
forall r. Eff es r -> IO r
unlift (Eff es a -> IO a) -> (Handle -> Eff es a) -> Handle -> IO a
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 = IO () -> Eff es ()
forall a (es :: [Effect]). IO a -> Eff es a
unsafeEff_ (IO () -> Eff es ())
-> (FilePath -> IO ()) -> FilePath -> Eff es ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ()
forall (m :: Type -> Type). MonadIO m => FilePath -> m ()
U.ensureFileDurable