# `CRAM.ReferenceProvider`
[🔗](https://github.com/baoulo/cram-elixir/blob/v0.1.0/lib/cram/reference_provider.ex#L1)

Behaviour for resolving a reference sequence by name and expected MD5.

Implementations must not log sequence bases. Errors carry the sequence name
and checksum only.

`fetch/3` is explicit. Inspecting a CRAM file does not call it.

# `fetch`

```elixir
@callback fetch(term(), String.t(), String.t() | nil) ::
  {:ok, binary()} | {:error, CRAM.Error.t()}
```

Return the bases for `name` after checking `expected_md5`.

`provider` is the struct passed to `CRAM.Reference.fetch/3`. `name` is the
`@SQ` `SN` value. `expected_md5` is the `M5` string, or `nil` when the header
has no checksum.

Return `{:ok, bases}` or `{:error, %CRAM.Error{}}`. Use
`reason: :reference_missing` when the name is absent and
`reason: :reference_checksum_mismatch` when the MD5 differs. Do not put the
bases on the error and do not log them. Do not raise for a missing sequence.

A provider keeps its sequences in the struct and answers `fetch/3`:

    defmodule MemoryProvider do
      @behaviour CRAM.ReferenceProvider

      defstruct sequences: %{}

      @impl true
      def fetch(%MemoryProvider{sequences: sequences}, name, expected_md5) do
        case Map.fetch(sequences, name) do
          {:ok, bases} ->
            if expected_md5 in [nil, CRAM.Reference.MD5.hex(bases)] do
              {:ok, bases}
            else
              {:error, CRAM.Error.new(reason: :reference_checksum_mismatch, data: %{name: name})}
            end

          :error ->
            {:error,
             CRAM.Error.new(reason: :reference_missing, data: %{name: name, expected_md5: expected_md5})}
        end
      end
    end

`CRAM.Reference.Local` is that callback backed by a FASTA file:

    iex> {:ok, local} = CRAM.Reference.Local.new("tutorials/fixtures/sq0.fa")
    iex> CRAM.Reference.fetch(local, "sq0", "be19336b7e15968f7ac7dc82493d9cd8")
    {:ok, "TTCACCCA"}

---

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