ASDF.Tree (asdf v0.1.0)

Copy Markdown View Source

Finds the YAML tree between the header and the first block.

The document ends at a line that is .... Only max_tree_bytes are read. Block magic as the first non-space content means the file has no tree.

Summary

Functions

Finds the YAML document that starts at start.

Functions

locate(source, start, limits)

@spec locate(ASDF.Source.t(), non_neg_integer(), ASDF.Limits.t()) ::
  {:ok, binary() | nil, non_neg_integer()} | {:error, ASDF.Error.t()}

Finds the YAML document that starts at start.

source is an %ASDF.Source{}. start is the byte after the header. limits is an %ASDF.Limits{}. The document ends at a line that is .... At most max_tree_bytes are read.

Returns {:ok, tree, body_offset}. tree is the document binary, or nil when the next non-space content is a block. body_offset is the absolute byte where blocks begin. A start past the end of the source is reason: :offset_out_of_range. Content that is neither YAML nor a block is reason: :unexpected_content. A file that ends before the ... line is reason: :truncated_tree. A tree longer than the limit is reason: :tree_too_large. Does not raise.

Examples

iex> binary = "#ASDF 1.0.0\n%YAML 1.1\n---\nhello: world\n...\n"
...> {:ok, source} = ASDF.Source.open({:binary, binary})
...> {:ok, header} = ASDF.Header.parse(binary)
...> {:ok, tree, offset} = ASDF.Tree.locate(source, header.bytes, ASDF.Limits.default())
...> {String.contains?(tree, "hello: world"), offset == byte_size(binary)}
{true, true}

iex> {:ok, source} = ASDF.Source.open({:binary, "ab"})
...> {:error, error} = ASDF.Tree.locate(source, 3, ASDF.Limits.default())
...> error.reason
:offset_out_of_range