defmodule Mercury.Types do @moduledoc false # Shared conversion helpers used by every resource's `from_json/1`. @doc false @spec datetime(value :: String.t() | nil) :: DateTime.t() | nil def datetime(nil), do: nil def datetime(value) when is_binary(value) do case DateTime.from_iso8601(value) do {:ok, datetime, _utc_offset} -> datetime {:error, _reason} -> nil end end def datetime(_other), do: nil @doc false @spec list_of(list :: list() | nil, from_json :: (map() -> struct())) :: [struct()] def list_of(nil, _from_json), do: [] def list_of(list, from_json) when is_list(list), do: Enum.map(list, from_json) end