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)

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

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

Constructors

  • KeyMissing(key: String, failed_at_segment: Int)
  • KeyValueEmpty(key: String)
  • KeyTypeMismatch(key: String, expected: String, 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(
  node: Node,
  key: String,
) -> Result(Bool, ExtractionError)

Extracts a boolean from a YAML node.

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

Extracts a list of booleans from a YAML node.

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

Extracts a map of boolean values from a YAML node.

pub fn extract_bool_or(
  node: Node,
  key: String,
  default: Bool,
) -> Result(Bool, ExtractionError)

Extracts a boolean from a YAML node, returning a default if the key is missing or nil. Returns Error only for type mismatches.

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

Extracts a float from a YAML node. Also accepts integers and converts them to floats.

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

Extracts a list of floats from a YAML node. Also accepts integers and converts them to floats.

pub fn extract_float_map(
  node: Node,
  key: String,
) -> Result(dict.Dict(String, Float), ExtractionError)

Extracts a map of float values from a YAML node. Also accepts integers and converts them to floats.

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

Extracts a float from a YAML node, returning a default if the key is missing or nil. Also accepts integers and converts them to floats. Returns Error only for type mismatches.

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

Extracts an integer from a YAML node.

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

Extracts a list of integers from a YAML node.

pub fn extract_int_map(
  node: Node,
  key: String,
) -> Result(dict.Dict(String, Int), ExtractionError)

Extracts a map of integer values from a YAML node.

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

Extracts an integer from a YAML node, returning a default if the key is missing or nil. Returns Error only for type mismatches.

pub fn extract_list_with(
  node: Node,
  key: String,
  item_extractor: fn(Node) -> Result(a, ExtractionError),
) -> Result(List(a), ExtractionError)

Extracts a list using a custom item extractor function. Useful for extracting nested containers like List(Dict(String, String)).

Example

// Extract a list of string maps
extract_list_with(node, "servers", fn(item) {
  extract_string_map(item, "")
})
pub fn extract_map_with(
  node: Node,
  key: String,
  value_extractor: fn(Node) -> Result(a, ExtractionError),
) -> Result(dict.Dict(String, a), ExtractionError)

Extracts a map using a custom value extractor function. Useful for extracting nested containers like Dict(String, List(Int)).

Example

// Extract a map of integer lists
extract_map_with(node, "groups", fn(item) {
  extract_int_list(item, "")
})
pub fn extract_optional_bool(
  node: Node,
  key: String,
) -> Result(option.Option(Bool), ExtractionError)

Extracts an optional boolean from a YAML node. Returns Ok(None) if the key is missing, Ok(Some(value)) if present. Returns Error for type mismatches.

pub fn extract_optional_float(
  node: Node,
  key: String,
) -> Result(option.Option(Float), ExtractionError)

Extracts an optional float from a YAML node. Returns Ok(None) if the key is missing, Ok(Some(value)) if present. Also accepts integers and converts them to floats. Returns Error for type mismatches.

pub fn extract_optional_int(
  node: Node,
  key: String,
) -> Result(option.Option(Int), ExtractionError)

Extracts an optional integer from a YAML node. Returns Ok(None) if the key is missing, Ok(Some(value)) if present. Returns Error for type mismatches.

pub fn extract_optional_string(
  node: Node,
  key: String,
) -> Result(option.Option(String), ExtractionError)

Extracts an optional string from a YAML node. Returns Ok(None) if the key is missing, Ok(Some(value)) if present. Returns Error for type mismatches.

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

Extracts a string from a YAML node.

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

Extracts a list of strings from a YAML node.

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

Extracts a map of string values from a YAML node.

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

Extracts a string map with duplicate key detection.

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

Extracts a string from a YAML node, returning a default if the key is missing or nil. Returns Error only for type mismatches.

pub fn extraction_error_to_string(
  error: ExtractionError,
) -> String

Converts an ExtractionError to a human-readable string.

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