1. What this library is {#scope}
ucum implements the Unified Code for Units of Measure (UCUM, https://ucum.org/ucum),
maintained by the Regenstrief Institute: a code system for units, such as kg/m2,
mm[Hg], 10*3/uL and {request}/s, built for exchange between machines. Its codes are
unambiguous, and HL7, FHIR and OpenTelemetry's semantic conventions use them.
The library reads a code, says whether it is valid, and answers what UCUM defines for it (spec/02): its dimension, its exact factor to the base units, its kind, how to convert a value between two codes, its form in base units, the product and quotient of two quantities, and how to write it for display and by name. It reads UCUM's case-sensitive codes, and answers the case-sensitive code for a case-insensitive one (spec/01 §5).
It follows the standard. Where UCUM defines an answer, the library gives that answer and no other. A program that wants a stricter rule (refusing to mix two units UCUM calls commensurable, say) adds it on top, in its own code.
Coverage. UCUM defines its grammar (its specification's §2.2, Syntax Rules) and a
table of units and prefixes, the essence table (ucum-essence.xml). The library reads
all of both (spec/01), and is compiled from the table itself. A string that is not a code
is refused with a named reason, never guessed at. Conformance is measured by the UCUM
functional tests (spec/04).
2. What it is not {#not}
- Not a symbol guesser. It reads UCUM codes only.
sec,mphandKBare not codes; reading what people type is a different job, with judgments UCUM doesn't make (isKB1,000 or 1,024 bytes?). - Not a currency table. UCUM has no currencies. An annotation such as
{iso4217:USD}is an annotation like any other: it names the thing counted and has no unit meaning. - Not a formatter. It writes a code's display symbol and its name; writing a number in a locale is not its job.
- Not a judge of which units belong together. It converts what UCUM calls commensurable: a byte to a percent, both being numbers. A stricter rule is the program's.
3. Errors are values {#errors}
Every public function answers {:ok, value} or {:error, reason}, reason a term that
names what is wrong, and format_error/1 turns a reason into a sentence. A function ending
in ! is the twin of its value function: it answers the value alone, and raises
Ucum.Error, carrying the reason and the sentence, exactly where its twin answers
{:error, reason}. Nothing else raises, and nothing catches: there is no try, catch or
rescue in the library. CI holds this (ci/raises.exs, with every allowed raise listed
in ci/raises_allowlist.exs).
This holds for arguments of every shape, not only of the types a function takes: a term
that is no code, no quantity, no conversion or no options is refused with a reason
(spec/03 §4), and format_error/1 answers a sentence for any term. ci/raises.exs cannot
see a clause that fails to match, so a property test holds it: every public function,
given arbitrary terms, answers its declared shape and never raises.
That check reads the source for a raise that is written. The other kind is the
machine's: on the BEAM a float past its range is no infinity but an ArithmeticError.
The library's arithmetic has no range to leave (spec/02 §9), and a property test holds
it, over every unit of the table and numbers of every size. The machine's other fault
is an integer too large to hold, which a code can write in a few bytes (km999999): a
code's numbers have a stated bound (spec/01 §6), and a property test holds that too.
4. No state, no dependencies {#state}
The library is pure functions over a compiled-in table. It starts no processes, reads no
application environment and no file, keeps no cache, and has no runtime dependency. The
table is compiled from UCUM's essence file (spec/01 §3), which Erlang/OTP's xmerl reads
while the library compiles and at no other time. It needs Elixir 1.18 or later.