2020-06-07 17:00:56 +03:00

32 lines
952 B
Elixir

defmodule Localizator.Translator.Direction do
alias Localizator.Commons
defstruct [:from, :to]
@typedoc """
Locale
"""
@type locale :: String.t() | Atom.t()
@type from :: locale
@type from_may_be_nil :: from | nil
@type to :: locale
@type direction :: {from, to} | to | %{from: from, to: to} | %{to: to}
@type direction_struct :: %{to: to, from: from_may_be_nil}
@spec get(direction) :: direction_struct
def get(direction) do
direction_map =
case direction do
[from: from, to: to] -> %{to: "#{to}", from: "#{from}"}
[from, to] -> %{to: "#{to}", from: "#{from}"}
{from, to} -> %{to: "#{to}", from: "#{from}"}
%{from: from, to: to} -> %{to: "#{to}", from: "#{from}"}
%{to: to} -> %{to: "#{to}", from: nil}
[to: to] -> %{to: "#{to}", from: nil}
to -> %{to: "#{to}", from: nil}
end
Commons.struct_from_map(direction_map, as: %__MODULE__{})
end
end