-- | <https://tools.ietf.org/html/rfc4511#section-4.8 Delete> operation.
--
-- This operation comes in four flavours:
--
--   * synchronous, exception throwing ('delete')
--
--   * synchronous, returning 'Either' 'ResponseError' @()@ ('deleteEither')
--
--   * asynchronous, 'IO' based ('deleteAsync')
--
--   * asynchronous, 'STM' based ('deleteAsyncSTM')
--
-- Of those, the first one ('delete') is probably the most useful for the typical usecase.
module Ldap.Client.Delete
  ( delete
  , deleteEither
  , deleteAsync
  , deleteAsyncSTM
  , Async
  , wait
  , waitSTM
  ) where

import           Control.Concurrent.STM (STM, atomically)
import           Data.List.NonEmpty (NonEmpty((:|)))

import qualified Ldap.Asn1.Type as Type
import           Ldap.Client.Internal


-- | Perform the Delete operation synchronously. Raises 'ResponseError' on failures.
delete :: Ldap -> Dn -> IO ()
delete :: Ldap -> Dn -> IO ()
delete Ldap
l Dn
dn =
  forall e a. Exception e => Either e a -> IO a
raise forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ldap -> Dn -> IO (Either ResponseError ())
deleteEither Ldap
l Dn
dn

-- | Perform the Delete operation synchronously. Returns @Left e@ where
-- @e@ is a 'ResponseError' on failures.
deleteEither :: Ldap -> Dn -> IO (Either ResponseError ())
deleteEither :: Ldap -> Dn -> IO (Either ResponseError ())
deleteEither Ldap
l Dn
dn =
  forall a. Async a -> IO (Either ResponseError a)
wait forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ldap -> Dn -> IO (Async ())
deleteAsync Ldap
l Dn
dn

-- | Perform the Delete operation asynchronously. Call 'Ldap.Client.wait' to wait
-- for its completion.
deleteAsync :: Ldap -> Dn -> IO (Async ())
deleteAsync :: Ldap -> Dn -> IO (Async ())
deleteAsync Ldap
l Dn
dn =
  forall a. STM a -> IO a
atomically (Ldap -> Dn -> STM (Async ())
deleteAsyncSTM Ldap
l Dn
dn)

-- | Perform the Delete operation asynchronously.
--
-- Don't wait for its completion (with 'Ldap.Client.waitSTM') in the
-- same transaction you've performed it in.
deleteAsyncSTM :: Ldap -> Dn -> STM (Async ())
deleteAsyncSTM :: Ldap -> Dn -> STM (Async ())
deleteAsyncSTM Ldap
l Dn
dn =
  let req :: Request
req = Dn -> Request
deleteRequest Dn
dn in forall a.
Ldap
-> (Response -> Either ResponseError a) -> Request -> STM (Async a)
sendRequest Ldap
l (Request -> Response -> Either ResponseError ()
deleteResult Request
req) Request
req

deleteRequest :: Dn -> Request
deleteRequest :: Dn -> Request
deleteRequest (Dn Text
dn) =
  LdapDn -> Request
Type.DeleteRequest (LdapString -> LdapDn
Type.LdapDn (Text -> LdapString
Type.LdapString Text
dn))

deleteResult :: Request -> Response -> Either ResponseError ()
deleteResult :: Request -> Response -> Either ResponseError ()
deleteResult Request
req (Type.DeleteResponse (Type.LdapResult ResultCode
code (Type.LdapDn (Type.LdapString Text
dn))
                                                            (Type.LdapString Text
msg) Maybe ReferralUris
_) :| [])
  | ResultCode
Type.Success <- ResultCode
code = forall a b. b -> Either a b
Right ()
  | Bool
otherwise = forall a b. a -> Either a b
Left (Request -> ResultCode -> Dn -> Text -> ResponseError
ResponseErrorCode Request
req ResultCode
code (Text -> Dn
Dn Text
dn) Text
msg)
deleteResult Request
req Response
res = forall a b. a -> Either a b
Left (Request -> Response -> ResponseError
ResponseInvalid Request
req Response
res)