glaml

Types

A Document Error dispatched by yamerl. It contains a string - msg and a tuple of 2 integers - loc.

The location is: #(line, column).

pub type DocError {
  DocError(msg: String, loc: #(Int, Int))
}

Constructors

  • DocError(msg: String, loc: #(Int, Int))
pub type DocNode {
  DocNodeNil
  DocNodeStr(string: String)
  DocNodeInt(int: Int)
  DocNodeSeq(nodes: List(DocNode))
  DocNodeMap(nodes: List(#(DocNode, DocNode)))
}

Constructors

  • DocNodeNil
  • DocNodeStr(string: String)
  • DocNodeInt(int: Int)
  • DocNodeSeq(nodes: List(DocNode))
  • DocNodeMap(nodes: List(#(DocNode, DocNode)))
pub type Document {
  Document(root: DocNode)
}

Constructors

  • Document(root: DocNode)
pub type NodeGetError {
  NodeNotFound(which: String)
  InvalidSugar
}

Constructors

  • NodeNotFound(which: String)
  • InvalidSugar
pub type PathRule {
  Seq(idx: Int)
  Map(key: String)
}

Constructors

  • Seq(idx: Int)
  • Map(key: String)

Functions

pub fn doc_node(doc: Document) -> DocNode

Returns the root node of the Document.

pub fn get(
  from node: DocNode,
  to path: List(PathRule),
) -> Result(DocNode, NodeGetError)

Traverses the DocNode and tries to find another DocNode by matching the PathRules.

Example

let assert Ok(doc) = parse_string("
employees:
  - name: Gordon
    surname: Befrey
    field: Database Infrastructure
  - name: Caroline
    surname: Gaster
    field: Networking
")
let doc = doc_node(doc)

get(doc, [Map("employees"), Seq(0), Map("field")])
// -> Ok(DocNodeStr("Database Infrastructure"))

get(doc, [Map("employees"), Seq(1), Map("passwords"), Seq(0)])
//                                  ~~~~~~~~~~~~~~~~
//                                    | reading backwards
// -> Error(NodeNotFound("reverse_idx:1,map:passwords"))
pub fn parse_file(path: String) -> Result(Document, DocError)

Parses the YAML file at path and returns either Ok(Document(...)) or Error(DocError(...)).

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

Parses the YAML string and returns either Ok(Document(...)) or Error(DocError(...)).

pub fn sugar(
  from node: DocNode,
  to path: String,
) -> Result(DocNode, NodeGetError)

Does the same thing as get but instead of a list of PathRules, you write a String that will get parsed into a list of PathRules for you.

Syntax

"some_key" == [Map("some_key")]
"#0" == [Seq(0)]
"combination.#0.of.these" == [Map("combination"), Seq(0), Map("of"), Map("these")]

// You can write as many dots as you want - an empty key points at the current node.
"...test..#0." == [Map("test"), Seq(0)]
Search Document