Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

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

Closed
wants to merge 2 commits into from

Conversation

lucasvr
Copy link

@lucasvr lucasvr commented Sep 9, 2023

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"],
}])

When deserializing a YAML document, the location and names of anchors
and aliases is not preserved. That information is useful, though, as
users of the library 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"],
}])
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I am ordinarily not interested in growing the API of this library to support more of YAML. I would instead be interested in someone else making a more fully featured Rust YAML library that can be used for the use case you described, among others.

@lucasvr
Copy link
Author

lucasvr commented Sep 11, 2023

No worries, thanks for letting me know.

@dtolnay dtolnay closed this Sep 11, 2023
@lucasvr lucasvr deleted the doc_anchors branch September 11, 2023 14:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants