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
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
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
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)
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)