# `Macaw.Format.Alias`
[🔗](https://github.com/dcoai/macaw_format/blob/v0.1.0/lib/macaw/format/alias.ex#L1)

The symbols people type and hosts have stored, each read as the UCUM code it means
(spec/02 §1, D-155), or refused naming the codes it could mean.

`parse/1` reads this table before UCUM, so an alias wins over a UCUM reading of the same
spelling: a person who types `B` or `MB` means bytes, not bels. A symbol is ambiguous
when two units people use are written that way — a kilobyte of 1,000 or 1,024 bytes, a
US or an imperial gallon — and then it is refused with its choices rather than guessed
at. A code of the ISO 4217 table typed alone, in capitals, is its currency. A symbol the
table does not hold is read as a code by macaw_units' `Macaw.Units.Unit`.

    iex> Macaw.Format.Alias.parse("mph")
    {:ok, "[mi_i]/h"}

# `reason`

```elixir
@type reason() ::
  {:ambiguous, String.t(), [{Macaw.Units.Unit.code(), String.t()}, ...]}
  | {:unknown_alias, String.t()}
```

Why `parse/1` refuses a symbol.

# `format_error`

```elixir
@spec format_error(reason() | term()) :: String.t()
```

A refusal of `parse/1` as a line a person reads; any other reason in
`Macaw.Units.Unit.format_error/1`'s words.

## Examples

    iex> Macaw.Format.Alias.format_error({:ambiguous, "gal", [{"[gal_us]", "US gallon"}, {"[gal_br]", "imperial gallon"}]})
    ~s|"gal" is ambiguous: [gal_us] (US gallon) or [gal_br] (imperial gallon)|

# `parse`

```elixir
@spec parse(String.t()) :: {:ok, Macaw.Units.Unit.code()} | {:error, reason()}
```

The code a person means by a symbol they typed (spec/02 §1, D-155): `°C`, `lb`,
`mph`, `kWh`, `KiB`. The alias table is read first, so a person's `B` is the byte and
not the bel; a symbol the table does not hold is read by `Macaw.Units.Unit.parse/1`, a
code being its own alias. An ambiguous symbol is refused with the codes it could mean,
never guessed. Surrounding whitespace is ignored.

## Examples

    iex> Macaw.Format.Alias.parse("°F")
    {:ok, "[degF]"}

    iex> Macaw.Format.Alias.parse("KB")
    {:error, {:ambiguous, "KB", [{"kBy", "1,000 bytes"}, {"KiBy", "1,024 bytes"}]}}

    iex> Macaw.Format.Alias.parse("kPa")
    {:ok, "kPa"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
