localizator/lib/direction/direction.ex

33 lines
1.1 KiB
Elixir
Raw Normal View History

2020-06-07 22:01:46 +03:00
defmodule Localizator.Direction do
alias Localizator.Commons
alias Localizator.Locale
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: Locale.normalize(to), from: Locale.normalize(from)}
[from, to] -> %{to: Locale.normalize(to), from: Locale.normalize(from)}
{from, to} -> %{to: Locale.normalize(to), from: Locale.normalize(from)}
%{from: from, to: to} -> %{to: Locale.normalize(to), from: Locale.normalize(from)}
%{to: to} -> %{to: Locale.normalize(to), from: nil}
[to: to] -> %{to: Locale.normalize(to), from: nil}
to -> %{to: Locale.normalize(to), from: nil}
end
Commons.struct_from_map(direction_map, as: %__MODULE__{})
end
end