# `Poney`
[🔗](https://github.com/hattoriai/poney-elixir/blob/v0.1.0/lib/poney.ex#L1)

The Poney format in Elixir: read and check Poney documents, as the
[specification](https://github.com/hattoriai/poney/blob/main/spec/SPEC.md)
says.

A Poney document (`.poney`, JSON) describes software, its views and its
walkthroughs: stories that explain one change, or tours of code as it is.
This library is a conforming reader and writer (§2). It draws nothing;
the drawings are Poney's JavaScript packages.

    {:ok, doc} = Poney.read(File.read!("change.poney"))

    for walkthrough <- doc.walkthroughs, scene <- walkthrough.scenes do
      IO.puts("#{scene.title}: #{scene.text}")
    end

Reading a document takes these steps, each from the specification:

  1. A document written for an earlier version of the format is upgraded
     (§11): version 0.1.0 opens as it is.
  2. Its shape must match the format's published JSON Schema (§9.1). A
     member that the format does not know is an error.
  3. Code is a diff or an excerpt, not both.
  4. The canonical form: every default filled in, and views written inside
     walkthrough scenes moved into `views` (§5.8).
  5. The rules that JSON Schema cannot say (§9.2): unique identifiers,
     references that exist, well-formed behaviors and walkthroughs. A
     required extension that this reader does not know is an error; an
     optional one is kept, with a warning (§8.4).

Every problem is a `Poney.Issue`, with the path of the value, such as
`"model.relationships.4.target"`, and often a hint.

# `input`

```elixir
@type input() :: String.t() | map()
```

A document: JSON text, or JSON already decoded (maps with string keys).

# `canonical`

```elixir
@spec canonical(input()) :: {:ok, map()} | {:error, [Poney.Issue.t()]}
```

The canonical form of a document, as JSON (maps with string keys): what
`read/1` turns into structs. Use it to store or send a document that
other readers will read.

# `check`

```elixir
@spec check(input()) :: [Poney.Issue.t()]
```

Every issue of a document, errors and warnings, as an author or an agent
needs them to fix it. An empty list means a valid document with nothing
to report.

# `format_version`

```elixir
@spec format_version() :: String.t()
```

The version of the format this library reads and writes, such as `"0.2.0"`.

# `read`

```elixir
@spec read(input()) :: {:ok, Poney.Document.t()} | {:error, [Poney.Issue.t()]}
```

Reads a document.

Returns the document in its canonical form, as structs, or every issue
when there is at least one error. Warnings alone do not stop a document;
`check/1` lists them.

# `read!`

```elixir
@spec read!(input()) :: Poney.Document.t()
```

Reads a document, or raises `Poney.InvalidDocumentError`.

# `to_json`

```elixir
@spec to_json(Poney.Document.t()) :: map()
```

A document as JSON (maps with string keys), without checking it.

# `write`

```elixir
@spec write(Poney.Document.t()) :: {:ok, String.t()} | {:error, [Poney.Issue.t()]}
```

Writes a document as `.poney` text: JSON indented by two spaces, members
in the format's order, and a final newline.

The document is checked first, as `read/1` checks one: a writer gives
only documents a reader accepts (§2). Reading a document and writing it
keeps everything in it, data of unknown extensions too.

    {:ok, doc} = Poney.read(File.read!("change.poney"))
    {:ok, text} = Poney.write(%{doc | project: %{doc.project | name: "Kestrel"}})
    File.write!("change.poney", text)

# `write!`

```elixir
@spec write!(Poney.Document.t()) :: String.t()
```

Writes a document, or raises `Poney.InvalidDocumentError`.

---

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