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

How a value is written for a person (spec/01): a format is data, read once from the text
it is written in and applied to any number of values.

A number's format is a d3-format specifier, `[[fill]align][sign][symbol][0][width][,][.precision][~][type]`
(spec/01 §2), read by `parse/2` into a `%Macaw.Format{}` that holds no function, so
it can be compared, digested and carried, and written by `number/2`:

    iex> {:ok, format} = Macaw.Format.parse(",.2f")
    iex> Macaw.Format.number(format, 1234567.891)
    "1,234,567.89"

    iex> Macaw.Format.number(Macaw.Format.parse!(".3s"), 42_000_000)
    "42.0M"

    iex> Macaw.Format.number(Macaw.Format.parse!("($,.2f"), -1234.5)
    "($1,234.50)"

    iex> Macaw.Format.parse("..2f")
    {:error, [{[], {:specifier, "..2f"}}]}

The digits are d3's, byte for byte over visualize's conformance set, which carries
d3's own cases; where this differs from d3 (an integer's exact digits, a width bound),
spec/01 §2 says so. The locale is a `Macaw.Format.Locale`, English by default.

# `fault`

```elixir
@type fault() :: {[term()], term()}
```

A fault at a place (spec/00 §3).

# `t`

```elixir
@type t() :: %Macaw.Format{
  align: String.t(),
  comma: boolean(),
  fill: String.t(),
  locale: Macaw.Format.Locale.t(),
  precision: non_neg_integer(),
  prefix: String.t(),
  sign: String.t(),
  specifier: String.t(),
  suffix: String.t(),
  symbol: String.t(),
  trim: boolean(),
  type: String.t(),
  width: non_neg_integer() | nil,
  zero: boolean()
}
```

A parsed format: the text it was read from, each part of the grammar as d3 normalizes
it, the affixes its symbol and type write under its locale, and the locale.

# `for_measure`

```elixir
@spec for_measure(term(), keyword()) ::
  {:ok, Macaw.Format.Measure.t()} | {:error, [fault(), ...]}
```

A field's default format, from its measure (spec/01 §6): `{:ok, format}` or
`{:error, faults}`, each fault at its key.

The measure, `%{unit: code, quantity: quantity}` with either or both absent, is
completed by `Macaw.Units.Quantity.resolve/1`, and its quantity chooses the writing: a
duration compound, a currency by its code, a ratio a percent, a count rounded and
grouped, any other quantity rescaled within its unit's family, a number with no measure
by its notation.

## Options

- `:precision` - decimals, 0 to 20, default 2 (a currency's: its code's minor units)
- `:system` - one of `Macaw.Format.Quantity.systems/0`, default `:as_posted`
- `:style` - a duration's, `:compound` (default) or `:magnitude`
- `:notation` - a number's with no measure: `:auto` (default), `:plain`, `:grouped`,
  `:si` or `{:si, e}`
- `:domain` - numbers whose greatest magnitude chooses the `:auto` notation for every value
- `:format` - a d3 specifier that overrides the number
- `:locale` - a `Macaw.Format.Locale`, default English

## Examples

    iex> {:ok, format} = Macaw.Format.for_measure(%{unit: "s"})
    iex> Macaw.Format.measure(format, 5040)
    "1h 24m"

    iex> Macaw.Format.measure(Macaw.Format.for_measure!(%{unit: "{iso4217:JPY}"}), 1240)
    "¥1,240"

    iex> Macaw.Format.measure(Macaw.Format.for_measure!(%{unit: "kg"}, system: :us), 80)
    "176.37 lb"

    iex> Macaw.Format.measure(Macaw.Format.for_measure!(%{}, domain: [0, 9_000_000]), 4_500_000)
    "4.5M"

    iex> Macaw.Format.for_measure(%{unit: "By", quantity: :duration}, precision: -1)
    {:error, [{[:measure], {:quantity, :duration, "By"}}, {[:precision], {:type, :precision}}]}

# `for_measure!`

```elixir
@spec for_measure!(term(), keyword()) :: Macaw.Format.Measure.t()
```

As `for_measure/2`, the format alone, raising `Macaw.Format.Error` with the faults in `format_error/1`'s words.

# `format_error`

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

The words of a reason format answers, for a person (spec/00 §3): a fault or a list of
them as macaw_base's `Macaw.Base.Fault.sentence/2` words them, with `words/1` first.

    iex> Macaw.Format.format_error({:width, 1024})
    "is wider than 1024 characters, the most a format writes"
    iex> Macaw.Format.format_error([{[:typo], :unknown_key}])
    "typo: is not a key it takes"

# `measure`

```elixir
@spec measure(Macaw.Format.Measure.t(), term()) :: String.t()
```

Writes a value by a measure's format (spec/01 §6): a string, whatever the value; a
value that is no number is the locale's not-a-number.

    iex> Macaw.Format.measure(Macaw.Format.for_measure!(%{quantity: :ratio}), 0.914)
    "91.40%"

# `number`

```elixir
@spec number(t(), term()) :: String.t()
```

Writes a value by a format (spec/01 §1): a string, whatever the value.

A value that is no number is the locale's not-a-number, `NaN` in English, padded and
aligned as the format says; under the `c` type a string, a number or an atom is
written as text.

    iex> Macaw.Format.number(Macaw.Format.parse!(">6.2f"), nil)
    "   NaN"

# `parse`

```elixir
@spec parse(term(), keyword()) :: {:ok, t()} | {:error, [fault(), ...]}
```

Reads a d3-format specifier (spec/01 §1, §2): `{:ok, format}` or `{:error, faults}`.

A string that is no specifier is `{[], {:specifier, text}}`, a term that is no string
`{[], {:type, :specifier}}`, a width over 1,024 `{[], {:width, 1024}}`.

## Options

- `:locale` - a `Macaw.Format.Locale`, default `Macaw.Format.Locale.english/0`

## Examples

    iex> {:ok, format} = Macaw.Format.parse("+.1%")
    iex> {format.sign, format.precision, format.type, format.suffix}
    {"+", 1, "%", "%"}

    iex> Macaw.Format.parse("08.2f") == Macaw.Format.parse("08.2f")
    true

# `parse!`

```elixir
@spec parse!(term(), keyword()) :: t()
```

As `parse/2`, the format alone, raising `Macaw.Format.Error` with the faults in `format_error/1`'s words.

# `parse_time`

```elixir
@spec parse_time(term(), keyword()) ::
  {:ok, Macaw.Format.Time.t()} | {:error, [fault(), ...]}
```

Reads a strftime pattern into a time's format (spec/01 §5): `{:ok, format}`, or
`{:error, faults}` for a term that is no string (`{[], {:type, :pattern}}`) or options
`parse/2` refuses. Every string is a pattern: a `%` before a character that is no
directive is written as it stands.

## Options

- `:locale` - a `Macaw.Format.Locale`, default `Macaw.Format.Locale.english/0`

## Examples

    iex> {:ok, format} = Macaw.Format.parse_time("%Y-%m-%d %H:%M")
    iex> Macaw.Format.time(format, ~U[2026-11-12 08:05:00Z])
    "2026-11-12 08:05"

# `parse_time!`

```elixir
@spec parse_time!(term(), keyword()) :: Macaw.Format.Time.t()
```

As `parse_time/2`, the format alone, raising `Macaw.Format.Error` with the faults in `format_error/1`'s words.

# `time`

```elixir
@spec time(Macaw.Format.Time.t(), term()) :: String.t()
```

Writes a time by a time's format (spec/01 §5), as visualize's `Visualize.Format.time/2`
writes it: a `DateTime` by its own wall clock and zone, a `NaiveDateTime` and a `Date`
(at midnight) with no zone, an integer as Unix microseconds in UTC. Any other value,
and an integer past a `DateTime`'s range, is the locale's not-a-number.

    iex> format = Macaw.Format.parse_time!("%a %e %b %I:%M %p [%Z]")
    iex> Macaw.Format.time(format, ~N[2024-03-03 14:05:09])
    "Sun  3 Mar 02:05 PM []"
    iex> Macaw.Format.time(format, 1_709_474_709_000_000)
    "Sun  3 Mar 02:05 PM [UTC]"

# `words`

```elixir
@spec words(term()) :: String.t() | nil
```

Format's words for a reason, `nil` for one it leaves to macaw_base's general words
(`Macaw.Base.Fault.general/1`): the words `Macaw.Format.Error` and `format_error/1`
try after a caller's own.

    iex> Macaw.Format.words({:type, :pattern})
    "must be a strftime pattern, a string"
    iex> Macaw.Format.words({:enum, [:compound, :magnitude]})
    nil

---

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