lib/xdr/ledger_entries/ttl_entry.ex

defmodule StellarBase.XDR.TTLEntry do
  @moduledoc """
  Automatically generated by xdrgen
  DO NOT EDIT or your changes may be overwritten
  Target implementation: elixir_xdr at https://hex.pm/packages/elixir_xdr
  Representation of Stellar `TTLEntry` type.
  """

  @behaviour XDR.Declaration

  alias StellarBase.XDR.{
    Hash,
    UInt32
  }

  @struct_spec XDR.Struct.new(
                 key_hash: Hash,
                 live_until_ledger_seq: UInt32
               )

  @type key_hash_type :: Hash.t()
  @type live_until_ledger_seq_type :: UInt32.t()

  @type t :: %__MODULE__{
          key_hash: key_hash_type(),
          live_until_ledger_seq: live_until_ledger_seq_type()
        }

  defstruct [:key_hash, :live_until_ledger_seq]

  @spec new(key_hash :: key_hash_type(), live_until_ledger_seq :: live_until_ledger_seq_type()) ::
          t()
  def new(
        %Hash{} = key_hash,
        %UInt32{} = live_until_ledger_seq
      ),
      do: %__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}

  @impl true
  def encode_xdr(%__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}) do
    [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
    |> XDR.Struct.new()
    |> XDR.Struct.encode_xdr()
  end

  @impl true
  def encode_xdr!(%__MODULE__{key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq}) do
    [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
    |> XDR.Struct.new()
    |> XDR.Struct.encode_xdr!()
  end

  @impl true
  def decode_xdr(bytes, struct \\ @struct_spec)

  def decode_xdr(bytes, struct) do
    case XDR.Struct.decode_xdr(bytes, struct) do
      {:ok,
       {%XDR.Struct{
          components: [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]
        }, rest}} ->
        {:ok, {new(key_hash, live_until_ledger_seq), rest}}

      error ->
        error
    end
  end

  @impl true
  def decode_xdr!(bytes, struct \\ @struct_spec)

  def decode_xdr!(bytes, struct) do
    {%XDR.Struct{components: [key_hash: key_hash, live_until_ledger_seq: live_until_ledger_seq]},
     rest} = XDR.Struct.decode_xdr!(bytes, struct)

    {new(key_hash, live_until_ledger_seq), rest}
  end
end