mirror of
https://github.com/nushell/nushell.git
synced 2026-12-03 18:46:44 +08:00
## Description Oh boi, here comes the YAML rework. 😵💫 YAML has been a pretty constant source of annoyance for us. This is mainly because YAML itself is weird and its spec can be pretty confusing. YAML v1.2 made things a lot better, but we still have many issues around YAML v1.1, so we have to support both. Also, `serde_yaml` is unmaintained and somewhat buggy, which certainly did not make our lives easier. In recent issues we discussed `saphyr` and `serde-saphyr`, which are newer crates that implement YAML parsing and serialization. The `serde-saphyr` crate seems to be more eagerly updated and even responds to issues: * https://github.com/bourumir-wyngs/serde-saphyr/issues/131 * https://github.com/bourumir-wyngs/granit-parser/issues/21 So in this PR I completely reworked our YAML integration using `serde-saphyr` for serialization and its `granit-parser` for parsing. Unlike `serde_yaml`, `serde-saphyr` does not work on intermediate types, which makes our implementation a bit harder. For parsing, I use the raw events and construct `Value`s directly. For serialization, I use a custom intermediate type to have control over its `Serialize` implementation. The implementation lives in `nu-heavy-utils`, so the parser and serializer are not tied directly to the `from yaml` and `to yaml` commands and can be reused from other places later. One very big change is that YAML v1.1 and v1.2 are now treated as explicitly separate specs. Previously we had some mix of v1.1 and v1.2 compliance, which was pretty weird and sluggish. The newer YAML v1.2 behavior is now the default, which is much more reasonable. But we also have people working with older YAML files, so we still need to support v1.1, including weird values like `off` being parsed as `false`. Since some behavior conflicts between the two specs, users usually have to pass `--spec 1.1` for that. I also implemented tagging support properly now. So we have full support for the language-independent type tags, and we even parse and serialize Nushell values that are not basic YAML values using tags. This allows roundtrips for values like cell-paths or durations. We now also have proper support for multiple documents in a single stream, more specific behavior for non-string keys, anchors and aliases, merge keys, configurable indentation, compact list indentation, and quoting style. So hopefully this should make our YAML implementation a lot better with fewer issues down the road. There are still some possible follow-ups I'd like to do, like preserving style information or comments so they can be restored later, but I would rather keep that separate from this already-large rework. ## Additional notes - closes #18402 - fixes #13126 - fixes #13925 - fixes #18374 - fixes #18401 - related #14630 - related #14934 - related #14985 - related #14986 - related #14987 - related #16072 - related #18010 - related #18298 - related #3870 - related #425 - related #9148 - resolves #11574 - resolves #14339 - resolves #18047 - resolves #18117 - resolves #6467 - resolves #8693 - resolves #9536