Skip to content

Commit 5216abe

Browse files
author
Jesse Hallett
committed
Add YamlLoader::load_from_str_with_markers to provide AST with source markers
fixes chyh1990#103, replaces chyh1990#105 The new function produces an alternative representation for YAML documents where each YAML node is paired with a `Marker` to indicate the corresponding line and column in the source markup. The new representation takes the form of two new types, `Node` and `YamlMarked`. `Node` is a pair of `YamlMarked` and `Marker`. `YamlMarked` mimics the existing `Yaml` enum; the difference is that array elements and hash keys and values are `Node` values instead of `Yaml` or `YamlMarked` values. I created a new enum because I did not know of a way to switch child nodes in `Yaml` between `Yaml` and `Node` types without backward-incompatible changes to the `Yaml` enum. The the behavior of the existing `load_from_str` function and `Yaml` enum are unchanged, so pattern matching on results from `load_from_str` will work as before. To ensure consistent behavior for the `Node` and `Yaml` I moved methods from the `impl Yaml` block to a new trait called `YamlNode` which is implemented by `Yaml`, `Node`, and `YamlMarked`. This is a breaking change since it means that consumers will have to import `YamlNode` to use methods like `.as_str()` and `.is_array()`. I want to present this pull request as one proposal. I think there is also an argument for changing the existing `Yaml` type to incorporate source location markers instead of maintaining two parallel enums. While making changes I split up `yaml.rs` into three nested modules. I can put it back the way it was if that is preferable.
1 parent 1d29d21 commit 5216abe

File tree

8 files changed

+664
-265
lines changed

8 files changed

+664
-265
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ repository = "https://github.com/chyh1990/yaml-rust"
1010
readme = "README.md"
1111

1212
[dependencies]
13+
derivative = "1"
1314
linked-hash-map = ">=0.0.9, <0.6"
1415

1516
[dev-dependencies]
17+
indoc = "0.3"
1618
quickcheck = "0.7"

src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! # Examples
2525
//!
2626
//! ```
27-
//! use yaml_rust::{YamlLoader, YamlEmitter};
27+
//! use yaml_rust::{YamlLoader, YamlEmitter, YamlNode};
2828
//!
2929
//! let docs = YamlLoader::load_from_str("[1, 2, 3]").unwrap();
3030
//! let doc = &docs[0]; // select the first document
@@ -44,6 +44,7 @@
4444
allow(match_same_arms, should_implement_trait)
4545
)]
4646

47+
extern crate derivative;
4748
extern crate linked_hash_map;
4849

4950
pub mod emitter;
@@ -54,8 +55,8 @@ pub mod yaml;
5455
// reexport key APIs
5556
pub use emitter::{EmitError, YamlEmitter};
5657
pub use parser::Event;
57-
pub use scanner::ScanError;
58-
pub use yaml::{Yaml, YamlLoader};
58+
pub use scanner::{Marker, ScanError};
59+
pub use yaml::{Node, Yaml, YamlLoader, YamlMarked, YamlNode};
5960

6061
#[cfg(test)]
6162
mod tests {

src/scanner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub enum TScalarStyle {
1818
Foled,
1919
}
2020

21-
#[derive(Clone, Copy, PartialEq, Debug, Eq)]
21+
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Eq, Ord, Hash)]
2222
pub struct Marker {
2323
index: usize,
2424
line: usize,

0 commit comments

Comments
 (0)