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

import           Control.Monad.STM (STM, atomically)
import           Data.List.NonEmpty (NonEmpty((:|)))
import           Prelude hiding (compare)

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


-- | Perform the Compare operation synchronously. Raises 'ResponseError' on failures.
compare :: Ldap -> Dn -> Attr -> AttrValue -> IO Bool
compare :: Ldap -> Dn -> Attr -> AttrValue -> IO Bool
compare Ldap
l Dn
dn Attr
k AttrValue
v =
  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 -> Attr -> AttrValue -> IO (Either ResponseError Bool)
compareEither Ldap
l Dn
dn Attr
k AttrValue
v

-- | Perform the Compare operation synchronously. Returns @Left e@ where
-- @e@ is a 'ResponseError' on failures.
compareEither :: Ldap -> Dn -> Attr -> AttrValue -> IO (Either ResponseError Bool)
compareEither :: Ldap -> Dn -> Attr -> AttrValue -> IO (Either ResponseError Bool)
compareEither Ldap
l Dn
dn Attr
k AttrValue
v =
  forall a. Async a -> IO (Either ResponseError a)
wait forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Ldap -> Dn -> Attr -> AttrValue -> IO (Async Bool)
compareAsync Ldap
l Dn
dn Attr
k AttrValue
v

-- | Perform the Compare operation asynchronously. Call 'Ldap.Client.wait' to wait
-- for its completion.
compareAsync :: Ldap -> Dn -> Attr -> AttrValue -> IO (Async Bool)
compareAsync :: Ldap -> Dn -> Attr -> AttrValue -> IO (Async Bool)
compareAsync Ldap
l Dn
dn Attr
k AttrValue
v =
  forall a. STM a -> IO a
atomically (Ldap -> Dn -> Attr -> AttrValue -> STM (Async Bool)
compareAsyncSTM Ldap
l Dn
dn Attr
k AttrValue
v)

-- | Perform the Compare operation asynchronously.
--
-- Don't wait for its completion (with 'Ldap.Client.waitSTM') in the
-- same transaction you've performed it in.
compareAsyncSTM :: Ldap -> Dn -> Attr -> AttrValue -> STM (Async Bool)
compareAsyncSTM :: Ldap -> Dn -> Attr -> AttrValue -> STM (Async Bool)
compareAsyncSTM Ldap
l Dn
dn Attr
k AttrValue
v =
  let req :: Request
req = Dn -> Attr -> AttrValue -> Request
compareRequest Dn
dn Attr
k AttrValue
v in forall a.
Ldap
-> (Response -> Either ResponseError a) -> Request -> STM (Async a)
sendRequest Ldap
l (Request -> Response -> Either ResponseError Bool
compareResult Request
req) Request
req

compareRequest :: Dn -> Attr -> AttrValue -> Request
compareRequest :: Dn -> Attr -> AttrValue -> Request
compareRequest (Dn Text
dn) (Attr Text
k) AttrValue
v =
  LdapDn -> AttributeValueAssertion -> Request
Type.CompareRequest (LdapString -> LdapDn
Type.LdapDn (Text -> LdapString
Type.LdapString Text
dn))
                      (AttributeDescription -> AssertionValue -> AttributeValueAssertion
Type.AttributeValueAssertion
                        (LdapString -> AttributeDescription
Type.AttributeDescription (Text -> LdapString
Type.LdapString Text
k))
                        (AttrValue -> AssertionValue
Type.AssertionValue AttrValue
v))

compareResult :: Request -> Response -> Either ResponseError Bool
compareResult :: Request -> Response -> Either ResponseError Bool
compareResult Request
req (Type.CompareResponse (Type.LdapResult ResultCode
code (Type.LdapDn (Type.LdapString Text
dn))
                                                              (Type.LdapString Text
msg) Maybe ReferralUris
_) :| [])
  | ResultCode
Type.CompareTrue  <- ResultCode
code = forall a b. b -> Either a b
Right Bool
True
  | ResultCode
Type.CompareFalse <- ResultCode
code = forall a b. b -> Either a b
Right Bool
False
  | 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)
compareResult Request
req Response
res = forall a b. a -> Either a b
Left (Request -> Response -> ResponseError
ResponseInvalid Request
req Response
res)