Current section
Files
Jump to
Current section
Files
lib/hugs/gen.ex
defmodule Hugs.Gen do
def parent_key, do: {__MODULE__, :_parent_key, []}
def _parent_key(%{datapath: [_, parent | _]}), do: {:ok, parent}
def _parent_key(_), do: {:error, "no parent key found in context"}
def copy(other_norm_key), do: {__MODULE__, :_copy, [other_norm_key]}
def _copy(%{parent_data: d}, key) when is_map_key(d, key) do
{:ok, Map.fetch!(d, key)}
end
def _copy(%{parent_data: d}, key) when is_map(d) do
reason = %KeyError{
key: key,
message: "cannot copy key #{key} from data, key not found",
term: d
}
{:error, reason}
end
def _copy(%{parent_data: _}, _key) do
{:error, "Hugs.Gen.copy/1 can only be used on field definitions"}
end
end