module Effectful.Temporary
  ( -- * Effect
    Temporary

    -- ** Handlers
  , runTemporary

    -- ** Operations
  , withSystemTempFile
  , withSystemTempDirectory
  , withTempFile
  , withTempDirectory
  ) where

import System.IO
import qualified UnliftIO.Temporary as T

import Effectful
import Effectful.Dispatch.Static

-- | An effect for interacting with temporary files.
data Temporary :: Effect

type instance DispatchOf Temporary = Static WithSideEffects
data instance StaticRep Temporary = Temporary

-- | Run the 'Temporary' effect.
runTemporary :: IOE :> es => Eff (Temporary : es) a -> Eff es a
runTemporary :: forall (es :: [Effect]) a.
(IOE :> es) =>
Eff (Temporary : es) a -> Eff es a
runTemporary = forall (e :: Effect) (sideEffects :: SideEffects) (es :: [Effect])
       a.
(DispatchOf e ~ 'Static sideEffects, MaybeIOE sideEffects es) =>
StaticRep e -> Eff (e : es) a -> Eff es a
evalStaticRep StaticRep Temporary
Temporary

-- | Lifted 'T.withSystemTempFile'.
withSystemTempFile
  :: Temporary :> es
  => String
  -- ^ File name template. See 'openTempFile'.
  -> (FilePath -> Handle -> Eff es a)
  -- ^ Callback that can use the file.
  -> Eff es a
withSystemTempFile :: forall (es :: [Effect]) a.
(Temporary :> es) =>
String -> (String -> Handle -> Eff es a) -> Eff es a
withSystemTempFile String
template String -> Handle -> Eff es a
action = 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 =>
String -> (String -> Handle -> m a) -> m a
T.withSystemTempFile String
template forall a b. (a -> b) -> a -> b
$ \String
fp Handle
handle -> forall r. Eff es r -> IO r
unlift forall a b. (a -> b) -> a -> b
$ String -> Handle -> Eff es a
action String
fp Handle
handle

-- | Lifted 'T.withSystemTempDirectory'.
withSystemTempDirectory
  :: Temporary :> es
  => String
  -- ^ Directory name template. See 'openTempFile'.
  -> (FilePath -> Eff es a)
  -- ^ Callback that can use the directory.
  -> Eff es a
withSystemTempDirectory :: forall (es :: [Effect]) a.
(Temporary :> es) =>
String -> (String -> Eff es a) -> Eff es a
withSystemTempDirectory String
template String -> Eff es a
action = 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 =>
String -> (String -> m a) -> m a
T.withSystemTempDirectory String
template forall a b. (a -> b) -> a -> b
$ \String
fp -> forall r. Eff es r -> IO r
unlift forall a b. (a -> b) -> a -> b
$ String -> Eff es a
action String
fp

-- | Lifted 'T.withTempFile'.
withTempFile
  :: Temporary :> es
  => FilePath
  -- ^ Temp dir to create the file in.
  -> String
  -- ^ File name template. See 'openTempFile'.
  -> (FilePath -> Handle -> Eff es a)
  -- ^ Callback that can use the file.
  -> Eff es a
withTempFile :: forall (es :: [Effect]) a.
(Temporary :> es) =>
String -> String -> (String -> Handle -> Eff es a) -> Eff es a
withTempFile String
tmpDir String
template String -> Handle -> Eff es a
action = 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 =>
String -> String -> (String -> Handle -> m a) -> m a
T.withTempFile String
tmpDir String
template forall a b. (a -> b) -> a -> b
$ \String
fp Handle
handle -> forall r. Eff es r -> IO r
unlift forall a b. (a -> b) -> a -> b
$ String -> Handle -> Eff es a
action String
fp Handle
handle

-- | Lifted 'T.withTempDirectory'.
withTempDirectory
  :: Temporary :> es
  => FilePath
  -- ^ Temp directory to create the directory in.
  -> String
  -- ^ Directory name template. See 'openTempFile'.
  -> (FilePath -> Eff es a)
  -- ^ Callback that can use the directory.
  -> Eff es a
withTempDirectory :: forall (es :: [Effect]) a.
(Temporary :> es) =>
String -> String -> (String -> Eff es a) -> Eff es a
withTempDirectory String
tmpDir String
template String -> Eff es a
action = 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 =>
String -> String -> (String -> m a) -> m a
T.withTempDirectory String
tmpDir String
template forall a b. (a -> b) -> a -> b
$ \String
fp -> forall r. Eff es r -> IO r
unlift forall a b. (a -> b) -> a -> b
$ String -> Eff es a
action String
fp