ASDF.YAML.Scalar (asdf v0.1.0)

Copy Markdown View Source

YAML 1.1 plain-scalar resolution for the types ASDF allows.

Quoted scalars never reach resolve_plain/1. Timestamps stay strings: the resolver only accepts null, bool, int, float, and everything else is text. .inf and .nan are {:unsupported_float, _}: this runtime rejects non-finite floats, and the YAML walker turns that into an error.

Summary

Functions

Parses text that an explicit !!bool tag claims is a boolean.

Parses text that an explicit !!float tag claims is a float.

Parses text that an explicit !!int tag claims is an integer.

Resolves one plain scalar.

Functions

parse_bool(text)

@spec parse_bool(String.t()) :: {:ok, boolean()} | :error

Parses text that an explicit !!bool tag claims is a boolean.

Accepts true, false, yes, no, on, and off, in lower, title, or upper case. Returns {:ok, boolean} or :error. Does not raise.

Examples

iex> ASDF.YAML.Scalar.parse_bool("yes")
{:ok, true}

iex> ASDF.YAML.Scalar.parse_bool("OFF")
{:ok, false}

iex> ASDF.YAML.Scalar.parse_bool("nope")
:error

parse_float_text(text)

@spec parse_float_text(String.t()) ::
  {:ok, float()} | {:unsupported_float, String.t()} | :error

Parses text that an explicit !!float tag claims is a float.

Returns {:ok, float} for decimal and scientific text. Returns {:unsupported_float, "infinity"} or {:unsupported_float, "NaN"} for .inf and .nan. Returns :error for other text. Does not raise.

Examples

iex> ASDF.YAML.Scalar.parse_float_text("1.5")
{:ok, 1.5}

iex> ASDF.YAML.Scalar.parse_float_text(".nan")
{:unsupported_float, "NaN"}

iex> ASDF.YAML.Scalar.parse_float_text("nope")
:error

parse_int(text)

@spec parse_int(String.t()) :: {:ok, integer()} | :error

Parses text that an explicit !!int tag claims is an integer.

Accepts decimal, 0x hex, and leading-zero octal text. Returns {:ok, integer} or :error. Does not raise.

Examples

iex> ASDF.YAML.Scalar.parse_int("0x10")
{:ok, 16}

iex> ASDF.YAML.Scalar.parse_int("010")
{:ok, 8}

iex> ASDF.YAML.Scalar.parse_int("nope")
:error

resolve_plain(text)

@spec resolve_plain(String.t()) :: term()

Resolves one plain scalar.

text is unquoted and untagged. Returns nil for ~, null, Null, and NULL. Returns true or false for the YAML 1.1 spellings true, false, yes, no, on, and off, in any of the three casings. Returns an integer for decimal, 0x hex, and leading-zero octal text. Returns a float for decimal and scientific text. Returns {:unsupported_float, "infinity"} or {:unsupported_float, "NaN"} for .inf and .nan. Any other text is returned unchanged. Does not raise.

Examples

iex> ASDF.YAML.Scalar.resolve_plain("on")
true

iex> ASDF.YAML.Scalar.resolve_plain("null")
nil

iex> ASDF.YAML.Scalar.resolve_plain("0x10")
16

iex> ASDF.YAML.Scalar.resolve_plain("010")
8

iex> ASDF.YAML.Scalar.resolve_plain("hello")
"hello"

iex> ASDF.YAML.Scalar.resolve_plain(".inf")
{:unsupported_float, "infinity"}