-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at https://mozilla.org/MPL/2.0/.

{- |
Copyright   :  (c) 2023 Yamada Ryo
               (c) 2017 FP Complete
               (c) 2022 Fumiaki Kinoshita
License     :  MPL-2.0 (see the file LICENSE)
Maintainer  :  ymdfield@outlook.jp
Stability   :  experimental
Portability :  portable

An elaborator for the t'Control.Effect.Class.Resource.Resource' effect class.
-}
module Control.Effect.Interpreter.Heftia.Resource where

import Control.Effect.Hefty (Elab)
import Data.Effect.Resource (Resource (Bracket, BracketOnExcept))
import UnliftIO (MonadUnliftIO, bracket, bracketOnError)

-- | Elaborates the `Resource` effect under the `MonadUnliftIO` context.
resourceToIO :: MonadUnliftIO m => Elab Resource m
resourceToIO :: forall (m :: * -> *). MonadUnliftIO m => Elab Resource m
resourceToIO = \case
    Bracket m a1
acquire a1 -> m ()
release a1 -> m x
thing -> forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m a1
acquire a1 -> m ()
release a1 -> m x
thing
    BracketOnExcept m a1
acquire a1 -> m ()
onError a1 -> m x
thing -> forall (m :: * -> *) a b c.
MonadUnliftIO m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracketOnError m a1
acquire a1 -> m ()
onError a1 -> m x
thing