Macaw.Format.Alias (macaw_format v0.1.0)

Copy Markdown View Source

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"}

Summary

Types

Why parse/1 refuses a symbol.

Functions

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

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.

Types

reason()

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

Why parse/1 refuses a symbol.

Functions

format_error(reason)

@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(symbol)

@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"}