2015-12-28 21:22:51 +00:00
|
|
|
# NimYAML - YAML implementation in Nim
|
2023-08-29 21:23:15 +00:00
|
|
|
# (c) Copyright 2015-2023 Felix Krause
|
2015-12-28 21:22:51 +00:00
|
|
|
#
|
|
|
|
# See the file "copying.txt", included in this
|
|
|
|
# distribution, for details about the copyright.
|
|
|
|
|
2023-11-27 22:05:05 +00:00
|
|
|
## .. importdoc::
|
|
|
|
## yaml/loading.nim, yaml/dumping.nim, yaml/annotations.nim, yaml/taglib.nim,
|
|
|
|
## yaml/style.nim, yaml/dom.nim, yaml/tojson.nim,
|
|
|
|
## yaml/parser.nim, yaml/presenter.nim, yaml/data.nim, yaml/stream.nim
|
|
|
|
##
|
|
|
|
## This is the root module of NimYAML, a package that provides facilities to
|
2016-09-21 19:20:57 +00:00
|
|
|
## generate and interpret `YAML <http://yaml.org>`_ character streams. Importing
|
2023-08-29 21:23:15 +00:00
|
|
|
## this package will import NimYAML's high level loading & dumping API.
|
|
|
|
## Additional APIs must be imported explicitly.
|
2015-12-27 20:36:47 +00:00
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## There is no code in this package, all functionality is available via the
|
2023-08-29 21:23:15 +00:00
|
|
|
## exported sub-packages. You can import parts of the API by importing
|
|
|
|
## certain sub-packages only.
|
2016-09-21 19:20:57 +00:00
|
|
|
##
|
2023-08-29 18:46:26 +00:00
|
|
|
## High Level Loading & Dumping
|
2023-08-29 21:23:15 +00:00
|
|
|
## ============================
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-08-29 21:23:15 +00:00
|
|
|
## import yaml
|
|
|
|
## # or alternatively:
|
2023-11-27 22:05:05 +00:00
|
|
|
## import yaml / [loading, dumping, annotations, taglib, dom]
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## Enables you to load YAML data directly into native Nim types and reversely
|
|
|
|
## dump native Nim types into YAML documents. This API corresponds to the full
|
|
|
|
## **Load** / **Dump** process as defined in the
|
|
|
|
## `YAML specification <https://yaml.org/spec/1.2.2/#31-processes>`_.
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## The module `module yaml/loading`_ provides the `load`_ and `loadAs`_ procs
|
|
|
|
## which load a single YAML document into a native Nim value.
|
|
|
|
##
|
|
|
|
## The `module yaml/dumping`_ provides the `Dumper`_ object together with its
|
|
|
|
## `dump`_ methods that serialize a given Nim value into YAML.
|
|
|
|
##
|
|
|
|
## The `module yaml/annotations`_ provides various pragmas that allow you to
|
|
|
|
## define how certain aspects of your types are to be serialized, e.g. whether
|
|
|
|
## ``Optional`` fields may be omitted.
|
|
|
|
##
|
|
|
|
## The `module yaml/taglib`_ provides facilities that customize the YAML tags
|
|
|
|
## that are generated for your types. The primary usage for tags in the context
|
|
|
|
## of NimYAML is to define the type of a value in a heterogeneous collection node.
|
|
|
|
##
|
2023-08-29 21:23:15 +00:00
|
|
|
## The following additional APIs extend the basic high-level API:
|
|
|
|
##
|
2023-08-29 18:46:26 +00:00
|
|
|
## DOM API
|
|
|
|
## -------
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## *Also exported by default, no import necessary*
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## The `module yaml/dom`_ enables you to load YAML into `YamlNode`_ objects and
|
|
|
|
## dump those back into YAML. This gives you a structured view of your YAML
|
|
|
|
## stream. The DOM API provides the types and their handling, which can then
|
|
|
|
## be used via the loading & dumping API.
|
2023-08-29 21:23:15 +00:00
|
|
|
##
|
|
|
|
## You can use ``YamlNode`` objects inside other objects to hold subtrees of
|
|
|
|
## the input YAML, or you can load the whole YAML into a ``YamlNode``.
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## ``YamlNode`` corresponds to the **Representation (Node Graph)** stage
|
|
|
|
## defined in the
|
|
|
|
## `YAML specification <https://yaml.org/spec/1.2.2/#31-processes>`_.
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## Style API
|
|
|
|
## ---------
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## # needs explicit import to use:
|
|
|
|
## import yaml/style
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## The `module yaml/style`_ lets you define the preferred YAML node style of
|
|
|
|
## your objects and fields, giving you a greater control over how your
|
|
|
|
## generated YAML looks.
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## JSON API
|
|
|
|
## --------
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## # needs explicit import to use:
|
|
|
|
## import yaml/tojson
|
|
|
|
##
|
|
|
|
## The `module yaml/tojson`_ enables you to load YAML input into the stdlib's
|
|
|
|
## ``JsonNode`` structure. This can be useful for other libraries that expect
|
|
|
|
## JSON input. Mind that the loading & dumping API is able to read & write
|
|
|
|
## JSON files (since YAML is a superset of JSON), you don't need the JSON
|
|
|
|
## API for that.
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
2023-08-29 21:23:15 +00:00
|
|
|
##
|
|
|
|
## Low Level Event Handling
|
|
|
|
## ========================
|
|
|
|
##
|
|
|
|
## NimYAML exposes lower-level APIs that allow you to access the different
|
|
|
|
## steps used for YAML loading & dumping. These APIs have at their core a
|
2023-11-27 22:05:05 +00:00
|
|
|
## `YamlStream`_ which is an object that supplies a stream of `Event`_.
|
|
|
|
## This corresponds to the **Serialization (Event Tree)** stage defined in the
|
2023-08-29 18:46:26 +00:00
|
|
|
## `YAML specification <https://yaml.org/spec/1.2.2/#31-processes>`_.
|
|
|
|
##
|
2023-08-29 21:23:15 +00:00
|
|
|
## Parsing & Presenting API
|
|
|
|
## ------------------------
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
2023-08-29 21:23:15 +00:00
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## # needs explicit import to use:
|
2023-08-29 21:23:15 +00:00
|
|
|
## import yaml / [parser, presenter, stream, data]
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## Provides `parse`_, a proc that feeds a ``YamlStream`` from YAML input,
|
|
|
|
## and `present`_, which consumes a ``YamlStream`` and writes out YAML.
|
|
|
|
## You can use a `BufferYamlStream`_ to supply manually generated events.
|
2023-08-29 21:23:15 +00:00
|
|
|
##
|
|
|
|
## Native API
|
|
|
|
## ----------
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## # needs explicit import to use:
|
2023-08-29 21:23:15 +00:00
|
|
|
## import yaml/native
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## This part of the API takes care of generating Nim values from a
|
2023-11-27 22:05:05 +00:00
|
|
|
## ``YamlStream`` via `construct`_, and transforming them back into a
|
|
|
|
## ``YamlStream`` via `represent`_. This complements the Event API.
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## Typically, you'd only access this API when defining custom constructors
|
|
|
|
## and representers.
|
2016-09-21 19:20:57 +00:00
|
|
|
##
|
2023-08-29 18:46:26 +00:00
|
|
|
##
|
|
|
|
## Hints API
|
|
|
|
## ---------
|
|
|
|
##
|
|
|
|
## .. code-block::
|
|
|
|
##
|
2023-11-27 22:05:05 +00:00
|
|
|
## # needs explicit import to use:
|
2023-08-29 18:46:26 +00:00
|
|
|
## import yaml/hints
|
|
|
|
##
|
|
|
|
## Provides type guessing, i.e. figuring out which type would be appropriate
|
|
|
|
## for a certain YAML scalar.
|
2015-12-27 20:36:47 +00:00
|
|
|
|
2023-08-29 18:46:26 +00:00
|
|
|
# top level API
|
2023-08-29 21:23:15 +00:00
|
|
|
import yaml / [annotations, loading, dumping, taglib]
|
|
|
|
export annotations, loading, dumping, taglib
|
2018-10-11 12:19:48 +00:00
|
|
|
|
2021-05-17 22:42:58 +00:00
|
|
|
when not defined(gcArc) or defined(gcOrc):
|
|
|
|
# YAML DOM may contain cycles and therefore will leak memory if used with
|
|
|
|
# ARC but without ORC. In that case it won't be available.
|
|
|
|
import yaml/dom
|
|
|
|
export dom
|