Expand description
YAML 1.2 implementation in pure Rust.
§Usage
This crate is on github and can be used by adding
saphyr
to the dependencies in your project’s Cargo.toml
:
cargo add saphyr
§Examples
Parse a string into Vec<Yaml>
and then serialize it as a YAML string.
use saphyr::{LoadableYamlNode, Yaml, YamlEmitter};
let docs = Yaml::load_from_str("[1, 2, 3]").unwrap();
let doc = &docs[0]; // select the first YAML document
assert_eq!(doc[0].as_integer().unwrap(), 1); // access elements by index
let mut out_str = String::new();
let mut emitter = YamlEmitter::new(&mut out_str);
emitter.dump(doc).unwrap(); // dump the YAML object to a String
§YAML object types
There are multiple YAML objects in this library which share most features but differ in usecase:
Yaml
: The go-to YAML object. It contains YAML data and borrows from the input.YamlOwned
: An owned version ofYaml
. It does not borrow from the input and can be used when tieing the object to the input is undesireable or would introduce unnecessary lifetimes.MarkedYaml
: A YAML object with addedMarker
s for the beginning and end of the YAML object in the input.MarkedYamlOwned
: An owned version ofMarkedYaml
. It does not borrow from the input and can be used when tieing the object to the input is undesireable or would introduce unnecessary lifetimes.
All of these share the same inspection methods (is_boolean
, as_str
, into_vec
, …) with
some variants between owned and borrowing versions.
They also contain the same variants. Yaml
and YamlOwned
are enums
, while annotated
objects (MarkedYaml
, MarkedYamlOwned
) are structures with a .data
enum
(see YamlData
, YamlDataOwned
).
§YAML Tags
§YAML Core Schema tags (!!str
, !!int
, !!float
, …)
saphyr
is aware of the YAML Core Schema tags
and will parse scalars accordingly. This is handled in Scalar::parse_from_cow_and_metadata
.
Should a scalar be explicitly tagged with a tag whose handle is that of the Core Schema,
saphyr
will attempt to parse it as the given type. If parsing fails (e.g.: !!int foo
), a
BadValue
will be returned. If however the tag is unknown, the scalar will be parsed as a
string (e.g.: !!unk 12
).
Core Schema tags on collections are ignored, since the syntax disallows any ambiguity in parsing.
Upon parsing, the core schema tags are not preserved. If you need the tags on scalar preserved,
you may disable early_parse
. This will cause all scalars to use the Representation
variant which preserves the tags. There is currently no way to preserve Core Schema tags on
collections.
§User-defined tags
The YAML specification does not explicitly specify how user-defined tags should be parsed
(10.4). saphyr
is very conservative on this
and will leave tags as-is. They are wrapped in a Tagged
variant where you can freely
inspect the tag alongside the tagged node.
The tagged node will be parsed as an untagged node. What this means is that 13
will be
parsed as an integer, foo
as a string, … etc. See the related discussion on
Github for more context
on the decision.
Examples:
assert!(matches!(parse("!custom 3"), Yaml::Tagged(_tag, node) if node.is_integer()));
assert!(matches!(parse("!custom 'foo'"), Yaml::Tagged(_tag, node) if node.is_string()));
assert!(matches!(parse("!custom foo"), Yaml::Tagged(_tag, node) if node.is_string()));
assert!(matches!(parse("!custom ~"), Yaml::Tagged(_tag, node) if node.is_null()));
assert!(matches!(parse("!custom '3'"), Yaml::Tagged(_tag, node) if node.is_string()));
User-defined tags can be applied to any node, whether a collection or a scalar. They do not change the resolution behavior of inner nodes.
assert!(matches!(parse("!custom [foo]"), Yaml::Tagged(_tag, node) if node.is_sequence()));
assert!(matches!(parse("!custom {a:b}"), Yaml::Tagged(_tag, node) if node.is_mapping()));
let node = parse("!custom [1, foo, !!str 3, !custom 3]");
let Yaml::Tagged(_, seq) = node else { panic!() };
assert!(seq.is_sequence());
assert!(seq[0].is_integer());
assert!(seq[1].is_string());
assert!(seq[2].is_string());
assert!(matches!(&seq[3], Yaml::Tagged(_tag, node) if node.is_integer()));
§Features
Note: With all features disabled, this crate’s MSRV is 1.65.0
.
§encoding
(enabled by default)
Enables encoding-aware decoding of Yaml documents.
The MSRV for this feature is 1.70.0
.
Structs§
- Annotated
Yaml Iter - An iterator over a
YamlData
node. - Marked
Yaml - A YAML node with
Span
s pointing to the start of the node. - Marked
Yaml Owned - A YAML node with
Span
s pointing to the start of the node. - Marker
- A location in a yaml document.
- Scan
Error - An error that occurred while scanning.
- Tag
- A YAML tag.
- Yaml
Decoder - A
YamlLoader
builder that allows you to supply your own encoding error trap. - Yaml
Emitter - The YAML serializer.
- Yaml
Iter - An iterator over a
Yaml
node. - Yaml
Loader - Main structure for parsing YAML.
- Yaml
Owned Iter - An iterator over a
YamlOwned
node.
Enums§
- Emit
Error - An error when emitting YAML.
- Load
Error - An error that happened when loading a YAML document.
- Scalar
- The resolved value of a scalar YAML node.
- Scalar
Owned - The resolved value of a scalar YAML node, freed from borrowing.
- Scalar
Style - The style as which the scalar was written in the YAML document.
- YAML
Decoding Trap - The behavior
YamlDecoder
must have when an decoding error occurs. - Yaml
- A YAML node is stored as this
Yaml
enumeration, which provides an easy way to access your YAML document. - Yaml
Data - YAML data for nodes that will contain annotations.
- Yaml
Data Owned - YAML data for nodes that will contain annotations.
- Yaml
Owned - A YAML node is stored as this
Yaml
enumeration, which provides an easy way to access your YAML document.
Traits§
- Annotated
Node - A trait allowing for introspection in the hash types of the
YamlData::Mapping
variant. - Annotated
Node Owned - A trait allowing for introspection in the hash types of the
YamlData::Mapping
variant. - Loadable
Yaml Node - A trait providing methods used by the
YamlLoader
.
Functions§
- parse_
core_ schema_ fp - Parse the given string as a floating point according to the core schema.
Type Aliases§
- Annotated
Mapping - The type contained in the
YamlData::Mapping
variant. This corresponds to YAML mappings. - Annotated
Sequence - The type contained in the
YamlData::Sequence
variant. This corresponds to YAML sequences. - Mapping
- The type contained in the
Yaml::Mapping
variant. - Mapping
Owned - The type contained in the
YamlOwned::Mapping
variant. - Sequence
- The type contained in the
Yaml::Sequence
variant. - Sequence
Owned - The type contained in the
YamlOwned::Sequence
variant. - YAML
Decoding Trap Fn - The signature of the function to call when using
YAMLDecodingTrap::Call
.