Current section

Files

Jump to
pbuf lib protoc templates enumeration.eex
Raw

lib/protoc/templates/enumeration.eex

<%
name = case embed? do
true -> e.name
false -> e.full_name
end
%>
defmodule <%= name %> do
@moduledoc false
@type t :: <%= e.typespec %>
@spec to_int(t | non_neg_integer) :: integer
<%= for {atom, value} <- Enum.sort(Map.to_list(e.values)) do %>
def to_int(:<%= atom %>), do: <%= value %>
def to_int(<%= value %>), do: <%= value %>
<% end %>
def to_int(invalid) do
raise Pbuf.Encoder.Error,
type: __MODULE__,
value: invalid,
tag: nil,
message: "#{inspect(invalid)} is not a valid enum value for #{__MODULE__}"
end
@spec from_int(integer) :: t
<%= for {atom, value} <- Enum.sort(Map.to_list(e.values)) do %>
def from_int(<%= value %>), do: :<%= atom %>
<% end %>
def from_int(_unknown), do: :invalid
end