Macaw.Format.Quantity (macaw_format v0.1.0)

Copy Markdown View Source

How a quantity is shown (spec/02 §2–§5): its display families by unit system, the unit systems a viewer may prefer, and the label a title gives a measure.

A quantity is what a unit measures, one of macaw_units' fixed list (Macaw.Units.Quantity.all/0), which keeps its meaning: its base unit, kind, bounds and additivity. This module keeps its presentation, keyed by the same quantity names. A unit and a quantity are facts about the data, not knobs: neither changes a value, they say what it already is, so a display can choose how to show it.

  • Families are ladders of units, smallest first, each tagged with the unit systems it serves; a unit belongs to the family that holds a member of its shape, so the data picks the family and no setting does (family/2).
  • Unit systems are :metric, :us, :uk and :as_posted; a system picks the family a value is shown in and never touches the data (family/3).
  • The label rule is the unit, else the quantity, else nothing (label/1).

Information is decimal by default: UCUM's kBy and Telemetry.Metrics' :kilobyte are 1,000 bytes, and a unit written with a binary prefix (MiBy) or in bit belongs to its own family. The data picks it; no setting does.

How a value is drawn in its family (tick formats, the ticks an axis picks) is the drawing library's.

Examples

iex> Macaw.Format.Quantity.family!(:mass, "kg", :us)
["[oz_av]", "[lb_av]"]

iex> Macaw.Format.Quantity.label!(%{unit: "{request}"})
"request"

Summary

Types

A unit and a quantity, either or both; the two facts a field description carries.

A unit system: a display preference, never applied to the data.

Functions

The display family a unit of the quantity belongs to, smallest first; [] for a unit of no family's shape (spec/02 §2). Within the unit's convention (family/3), a unit belongs to the first family holding a member of its shape — its atoms and exponents, prefixes aside and a binary prefix kept apart — so the unit picks among a quantity's families: bits, binary prefixes, hertz, watt-hours, US lengths. No unit means the base unit.

The display family a system shows a unit of the quantity in (spec/02 §2, §3, D-158): the family the unit's convention names for :metric, :us or :uk when it names one, else the unit's own (family/2), which is what :as_posted always is.

As family/2, the family alone, raising Macaw.Format.Error naming the reason.

As family/3, the family alone, raising Macaw.Format.Error naming the reason.

The label rule (spec/02 §5): the unit, else the quantity, else nothing — the unit's symbol with its annotations written when it is not empty, else the quantity's name with spaces for underscores, else nil, as {:ok, label}. The measure is completed by Macaw.Units.Quantity.resolve/1, and one it refuses is its {:error, reason} (spec/00 §3); label!/1 answers the label or raises.

As label/1, the label alone, raising Macaw.Format.Error naming the reason.

The unit systems a display may prefer (spec/02 §3), :as_posted the default.

Types

measure()

@type measure() :: Macaw.Units.Quantity.measure()

A unit and a quantity, either or both; the two facts a field description carries.

system()

@type system() :: :metric | :us | :uk | :as_posted

A unit system: a display preference, never applied to the data.

Functions

family(quantity, unit)

@spec family(Macaw.Units.Quantity.t(), Macaw.Units.Unit.code() | nil) ::
  {:ok, [Macaw.Units.Unit.code()]} | {:error, term()}

The display family a unit of the quantity belongs to, smallest first; [] for a unit of no family's shape (spec/02 §2). Within the unit's convention (family/3), a unit belongs to the first family holding a member of its shape — its atoms and exponents, prefixes aside and a binary prefix kept apart — so the unit picks among a quantity's families: bits, binary prefixes, hertz, watt-hours, US lengths. No unit means the base unit.

Examples

iex> Macaw.Format.Quantity.family(:information, "MiBy")
{:ok, ["By", "KiBy", "MiBy", "GiBy", "TiBy"]}

iex> Macaw.Format.Quantity.family!(:length, "[ft_i]")
["[in_i]", "[ft_i]", "[mi_i]"]

{:error, reason} for a quantity not in Macaw.Units.Quantity.all/0 ({:unknown_quantity, quantity}) or a unit that is no code (Macaw.Units.Unit.parse/1's reason); family!/2 raises it.

family(quantity, unit, system)

@spec family(Macaw.Units.Quantity.t(), Macaw.Units.Unit.code() | nil, system()) ::
  {:ok, [Macaw.Units.Unit.code()]} | {:error, term()}

The display family a system shows a unit of the quantity in (spec/02 §2, §3, D-158): the family the unit's convention names for :metric, :us or :uk when it names one, else the unit's own (family/2), which is what :as_posted always is.

A quantity read in several conventions — a pressure is gauge, barometric or clinical — picks the unit's by its exact code first, then by its shape, else the first, so a system chooses within a convention and never across: a barometric hPa is shown in inches of mercury under :us, and a blood pressure in mm[Hg] stays in it under every system.

Examples

iex> Macaw.Format.Quantity.family(:mass, "kg", :uk)
{:ok, ["[oz_av]", "[lb_av]", "[stone_av]"]}

iex> Macaw.Format.Quantity.family!(:duration, "ms", :us)
["ns", "us", "ms", "s", "min", "h", "d"]

iex> Macaw.Format.Quantity.family!(:pressure, "hPa", :us)
["[in_i'Hg]"]

iex> Macaw.Format.Quantity.family(:mass, "kg", :imperial)
{:error, {:unknown_system, :imperial}}

A quantity, a unit or a system none of systems/0 is {:error, reason}, as family/2 answers, {:unknown_system, system} for the system; family!/3 raises it.

family!(quantity, unit)

As family/2, the family alone, raising Macaw.Format.Error naming the reason.

family!(quantity, unit, system)

As family/3, the family alone, raising Macaw.Format.Error naming the reason.

label(measure)

@spec label(measure()) ::
  {:ok, String.t() | nil} | {:error, Macaw.Units.Quantity.reason()}

The label rule (spec/02 §5): the unit, else the quantity, else nothing — the unit's symbol with its annotations written when it is not empty, else the quantity's name with spaces for underscores, else nil, as {:ok, label}. The measure is completed by Macaw.Units.Quantity.resolve/1, and one it refuses is its {:error, reason} (spec/00 §3); label!/1 answers the label or raises.

Examples

iex> Macaw.Format.Quantity.label(%{unit: "ms"})
{:ok, "ms"}

iex> Macaw.Format.Quantity.label!(%{quantity: :count})
"count"

iex> Macaw.Format.Quantity.label!(%{})
nil

iex> Macaw.Format.Quantity.label(%{unit: "By", quantity: :duration})
{:error, {:quantity, :duration, "By"}}

label!(measure)

@spec label!(measure()) :: String.t() | nil

As label/1, the label alone, raising Macaw.Format.Error naming the reason.

systems()

@spec systems() :: [system(), ...]

The unit systems a display may prefer (spec/02 §3), :as_posted the default.