packages feed

pqi-native-1.0.1.18 / src / comms / Pqi / Native / Comms.hs

-- | The serialization abstraction shared by the wire-protocol messages: a type-- that can be both written (via @ptr-poker@) and decoded (via @ptr-peeker@,-- with semantic failures carried in an 'ExceptT' layer, since peekers-- themselves have no failure mechanism).module Pqi.Native.Comms  ( Comms (..),    Decoder,    DecodingError (..),    runDecoder,    -- * Decoder primitives    liftVariable,    liftFixed,    int16,    int32,    word8,    word32,    cstring,    bytes,    remaining,    lengthPrefixedBytes,    -- * Serializable primitives    CString (..),  )whereimport Control.Monad.Trans.Class (lift)import Control.Monad.Trans.Except (ExceptT (..), runExceptT)import Data.ByteString (ByteString)import Data.Int (Int16, Int32)import Data.Word (Word32, Word8)import Preludeimport qualified PtrPeeker as Peekerimport qualified PtrPoker.Write as Poker-- | A semantic decoding failure (as opposed to a \"need more bytes\" framing-- shortfall, which the transport handles separately).data DecodingError  = -- | A backend message had an unrecognized type byte.    UnexpectedMessageType Word8  | -- | An @Authentication@ message had an unrecognized sub-type.    UnexpectedAuthType Int32  | -- | A field could not be interpreted (context, offending tag).    BadField ByteString  | -- | The message body was shorter than expected.    TruncatedInput  | -- | A general protocol violation.    ProtocolViolation ByteString  deriving stock (Eq, Show)-- | A decoder of a message body: a @ptr-peeker@ 'Peeker.Variable' parser with-- semantic failures layered on top.type Decoder = ExceptT DecodingError Peeker.Variable-- | A type that can be serialized to the wire and decoded back from it.class Comms a where  toWrite :: a -> Poker.Write  decoderOf :: Decoder a-- | Run a decoder against a complete message body. A framing shortfall (the-- body being shorter than the decoder consumes) is reported as 'TruncatedInput'.runDecoder :: Decoder a -> ByteString -> Either DecodingError arunDecoder decoder body =  case Peeker.runVariableOnByteString (runExceptT decoder) body of    Left _needed -> Left TruncatedInput    Right (Left err) -> Left err    Right (Right value) -> Right valueliftVariable :: Peeker.Variable a -> Decoder aliftVariable = liftliftFixed :: Peeker.Fixed a -> Decoder aliftFixed = lift . Peeker.fixedint16 :: Decoder Int16int16 = liftFixed Peeker.beSignedInt2int32 :: Decoder Int32int32 = liftFixed Peeker.beSignedInt4word8 :: Decoder Word8word8 = liftFixed Peeker.unsignedInt1word32 :: Decoder Word32word32 = liftFixed Peeker.beUnsignedInt4-- | A null-terminated string.cstring :: Decoder ByteStringcstring = liftVariable Peeker.nullTerminatedStringAsByteString-- | Exactly @n@ bytes.bytes :: Int -> Decoder ByteStringbytes n = liftFixed (Peeker.byteArrayAsByteString n)-- | All remaining bytes of the body.remaining :: Decoder ByteStringremaining = liftVariable Peeker.remainderAsByteString-- | An @Int32@-length-prefixed byte string, with a negative length meaning-- \"absent\" (the wire protocol's encoding for SQL @NULL@ column values).---- Decoded as a single 'liftVariable' call rather than a separate 'int32'-- (length) lift followed by a 'bytes' lift: this is the per-column-value-- decoder, called for every cell of every row, so halving the lift count-- here has an outsized effect on hot-path allocation.lengthPrefixedBytes :: Decoder (Maybe ByteString)lengthPrefixedBytes = liftVariable $ do  len <- Peeker.fixed Peeker.beSignedInt4  if len < 0    then pure Nothing    else Just <$> Peeker.fixed (Peeker.byteArrayAsByteString (fromIntegral len))-- | A null-terminated string, as a 'Comms' building block.newtype CString = CString ByteString  deriving stock (Eq, Show)instance Comms CString where  toWrite (CString value) = Poker.byteString value <> Poker.word8 0  decoderOf = CString <$> cstringinstance Comms Int16 where  toWrite = Poker.bInt16  decoderOf = int16instance Comms Int32 where  toWrite = Poker.bInt32  decoderOf = int32instance Comms Word32 where  toWrite = Poker.bWord32  decoderOf = word32instance Comms Word8 where  toWrite = Poker.word8  decoderOf = word8