dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
965 stars 164 forks source link

New API to get anchors and aliases from a document #389

Closed lucasvr closed 1 year ago

lucasvr commented 1 year ago

When deserializing a YAML document, the location and names of anchors and aliases is not preserved. That information is useful, though, as users may want to deduplicate references to the same mapping.

This commit introduces an API that exposes such mappings by parsing the metadata in struct Document. The new API has been added to struct Deserializer and provides the following signature:

pub fn anchors(&self) -> Option<Vec<DocumentAnchor>>

As an example, given the following YAML document:

a:
    enum: &io
        INPUT: 0
        OUTPUT: 1
b:
    enum: *io
c:
    enum: *io

this API returns:


Some([DocumentAnchor {
    anchor_name: "io",
    anchor_path: "/a/enum",
    aliases: ["/b/enum", "/c/enum"],
}])
lucasvr commented 1 year ago

No worries, thanks for letting me know.