defmodule Postgrex.Extensions.BitString do @moduledoc false import Postgrex.BinaryUtils, warn: false use Postgrex.BinaryExtension, send: "bit_send", send: "varbit_send" def init(opts), do: Keyword.fetch!(opts, :decode_binary) def encode(_) do quote location: :keep do val when is_binary(val) -> [<> | val] val when is_bitstring(val) -> bin_size = byte_size(val) last_pos = bin_size - 1 <> = val pad = 8 - bit_size(last) bit_count = bit_size(val) [ <>, binary | <> ] val -> raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(val, "a bitstring") end end def decode(:copy) do quote location: :keep do <> -> copy = :binary.copy(value) <> = copy bits end end def decode(:reference) do quote location: :keep do <> -> <> = value bits end end end