yay

Types

A YAML document.
To get the root Node call document_root on it, like this:

let document = Document(root: NodeNil)
let assert NodeNil = document_root(document)
pub type Document {
  Document(root: Node)
}

Constructors

  • Document(root: Node)

Represents the expected type of a YAML node value.

pub type ExpectedType {
  ExpectedString
  ExpectedInt
  ExpectedFloat
  ExpectedBool
  ExpectedList
  ExpectedMap
  ExpectedStringList
  ExpectedStringMap
}

Constructors

  • ExpectedString
  • ExpectedInt
  • ExpectedFloat
  • ExpectedBool
  • ExpectedList
  • ExpectedMap
  • ExpectedStringList
  • ExpectedStringMap

An error that can occur when extracting a value from a node.

pub type ExtractionError {
  KeyMissing(key: String)
  KeyValueEmpty(key: String)
  KeyTypeMismatch(
    key: String,
    expected: ExpectedType,
    found: String,
  )
  DuplicateKeysDetected(key: String, keys: List(String))
}

Constructors

  • KeyMissing(key: String)
  • KeyValueEmpty(key: String)
  • KeyTypeMismatch(
      key: String,
      expected: ExpectedType,
      found: String,
    )
  • DuplicateKeysDetected(key: String, keys: List(String))

A YAML document node.

pub type Node {
  NodeNil
  NodeStr(String)
  NodeBool(Bool)
  NodeInt(Int)
  NodeFloat(Float)
  NodeSeq(List(Node))
  NodeMap(List(#(Node, Node)))
}

Constructors

  • NodeNil
  • NodeStr(String)
  • NodeBool(Bool)
  • NodeInt(Int)
  • NodeFloat(Float)
  • NodeSeq(List(Node))
  • NodeMap(List(#(Node, Node)))

A Node selection used by Selector.

pub type Selection {
  SelectMap(key: Node)
  SelectSeq(index: Int)
}

Constructors

  • SelectMap(key: Node)
  • SelectSeq(index: Int)

A document selector that contains a sequence of selections leading to a Node.

pub type Selector {
  Selector(List(Selection))
}

Constructors

An error that can occur when selecting a node.

pub type SelectorError {
  NodeNotFound(at: Int)
  SelectorParseError
}

Constructors

  • NodeNotFound(at: Int)
  • SelectorParseError

A YAML document error containing a message — msg and its location — loc.

pub type YamlError {
  UnexpectedParsingError
  ParsingError(msg: String, loc: YamlErrorLoc)
}

Constructors

  • UnexpectedParsingError
  • ParsingError(msg: String, loc: YamlErrorLoc)

The location of a YAML parsing error.

pub type YamlErrorLoc {
  YamlErrorLoc(line: Int, column: Int)
}

Constructors

  • YamlErrorLoc(line: Int, column: Int)

Values

pub fn document_root(document: Document) -> Node

Gets the root Node of a YAML document.

Examples

let document = Document(root: NodeNil)
let assert NodeNil = document_root(document)
pub fn extract_bool_from_node(
  node: Node,
  key: String,
) -> Result(Bool, ExtractionError)

Extracts a boolean from a YAML node.

pub fn extract_dict_strings_from_node(
  node: Node,
  key: String,
  fail_on_key_duplication fail_on_key_duplication: Bool,
) -> Result(dict.Dict(String, String), ExtractionError)

Extracts a dictionary of string key-value pairs from a YAML node. Returns Ok(empty dict) for an explicitly empty map ({}). Returns Error(KeyValueEmpty) for nil/null values. Returns Error(KeyMissing) if the key doesn’t exist.

pub fn extract_float_from_node(
  node: Node,
  key: String,
) -> Result(Float, ExtractionError)

Extracts a float from a YAML node. Also accepts integers and converts them to floats (since YAML/JSON parsers often represent numbers like 99.0 as integers).

pub fn extract_int_from_node(
  node: Node,
  key: String,
) -> Result(Int, ExtractionError)

Extracts an integer from a YAML node.

pub fn extract_string_from_node(
  node: Node,
  key: String,
) -> Result(String, ExtractionError)

Extracts a string from a YAML node.

pub fn extract_string_list_from_node(
  node: Node,
  key: String,
) -> Result(List(String), ExtractionError)

Extracts a list of strings from a YAML node. Returns Ok([]) for an explicitly empty list ([]). Returns Error(KeyValueEmpty) for nil/null values. Returns Error(KeyMissing) if the key doesn’t exist.

pub fn parse_file(
  path: String,
) -> Result(List(Document), YamlError)

Parse a YAML file located in path into a list of YAML documents.

pub fn parse_selector(
  selector: String,
) -> Result(Selector, SelectorError)

Parses a selector string into a Selector.

pub fn parse_string(
  string: String,
) -> Result(List(Document), YamlError)

Parse a string into a list of YAML documents.

pub fn select(
  from node: Node,
  selector selector: Selector,
) -> Result(Node, SelectorError)

Queries the given node with a Selector.

Examples

let map = NodeMap([
  #(NodeStr("lib name"), NodeStr("yay")),
  #(NodeStr("stars"), NodeInt(7)),
])

let assert Ok(NodeInt(7)) = select(from: map, selector: Selector([SelectMap(NodeStr("stars"))]))
pub fn select_sugar(
  from node: Node,
  selector selector: String,
) -> Result(Node, SelectorError)

Parses the selector and queries the given node with it.

Examples

let map = NodeMap([
  #(NodeStr("list"), NodeMap([
    #(NodeStr("elements"), NodeSeq([NodeInt(101)]))
  ])),
  #(NodeStr("linked"), NodeBool(False)),
])

let assert Ok(NodeInt(101)) = select_sugar(from: map, selector: "list.elements.#0")
Search Document