#######################################################
# This file was auto-generated by elixir_bindings.zig #
#              Do not manually modify.                #
#######################################################

defmodule TigerBeetlex.QueryFilterFlags do
  @moduledoc """
  See [QueryFilterFlags](https://docs.tigerbeetle.com/reference/query-filter#flags).
  """

  use TypedStruct

  typedstruct do
    field :reversed, boolean(), default: false
  end

  @doc """
  Creates a `TigerBeetlex.QueryFilterFlags` struct from its binary representation.
  """
  @spec from_binary(binary :: <<_::32>>) :: t()
  def from_binary(<<n::unsigned-little-32>> = _bin) do
    <<
      _padding::31,
      reversed::1
    >> = <<n::unsigned-big-32>>

    %__MODULE__{
      reversed: reversed == 1
    }
  end

  @doc """
  Converts a `TigerBeetlex.QueryFilterFlags` struct to its binary represenation.
  """
  @spec to_binary(flags :: t()) :: <<_::32>>
  def to_binary(flags) do
    %__MODULE__{
      reversed: reversed
    } = flags

    <<n::unsigned-big-32>> =
      <<
        # padding
        0::31,
        bool_to_u1(reversed)::1
      >>

    <<n::unsigned-little-32>>
  end

  @spec bool_to_u1(b :: boolean()) :: 0 | 1
  defp bool_to_u1(true), do: 1
  defp bool_to_u1(false), do: 0
end
