module Database.MongoDB.Internal.Util where
import Prelude hiding (length)
import Control.Applicative ((<$>))
import Network (PortID(..))
import Data.UString as U (cons, append)
import Data.Bits (Bits, (.|.))
import Data.Bson
import Data.ByteString.Lazy as S (ByteString, length, append, hGet)
import System.IO (Handle)
import System.IO.Error (mkIOError, eofErrorType)
import Control.Exception (assert)
deriving instance Show PortID
deriving instance Eq PortID
deriving instance Ord PortID
bitOr :: (Bits a) => [a] -> a
bitOr = foldl (.|.) 0
(<.>) :: UString -> UString -> UString
a <.> b = U.append a (cons '.' b)
true1 :: Label -> Document -> Bool
true1 k doc = case valueAt k doc of
Bool b -> b
Float n -> n == 1
Int32 n -> n == 1
Int64 n -> n == 1
_ -> error $ "expected " ++ show k ++ " to be Num or Bool in " ++ show doc
hGetN :: Handle -> Int -> IO ByteString
hGetN h n = assert (n >= 0) $ do
bytes <- hGet h n
let x = fromEnum $ length bytes
if x >= n then return bytes
else if x == 0 then ioError (mkIOError eofErrorType "hGetN" (Just h) Nothing)
else S.append bytes <$> hGetN h (n x)