PureYAML/Sources/Tagged/Tagged.Node.ModelValue.swift
Mihaela Mihaljevic 2a84d6616f feat(tagged): preserve anchors, aliases, and per-node styles end to end
Tagged.Scalar/Sequence/Mapping gain anchor and style fields
(Tagged.ScalarStyle, Tagged.CollectionStyle), Tagged.Node gains an
alias(anchor:node:) case carrying both the reference and its resolution,
and parseTagged populates all of it from the parse events instead of
discarding anchors and expanding aliases into copies.

PureYAML.dump(_ node: Tagged.Node, options:) re-emits anchors, alias
references, tags, and per-node styles over the document-wide policy.
Plain scalars re-emit their source spelling (NO stays NO, an empty null
stays empty); literal blocks cover keep chomping, single-line strip
chomping, leading blank lines, and whitespace-led first lines via an
explicit indentation indicator; folded scalars re-emit as > when the
folding rules invert exactly and degrade to a literal block otherwise.

The round-trip law parseTagged(dump(parseTagged(d))) == parseTagged(d)
is corpus-tested over every real-world fixture, with one documented
degradation: single-quoted content containing line breaks re-emits
double-quoted, value unchanged.

Closes #9
2026-07-12 13:20:12 +02:00

29 lines
1.0 KiB
Swift

public extension PureYAML.Tagged.Node {
/// Converts a tagged node into a model value while explicitly erasing tags.
/// An alias converts to its resolved node's value, matching `parse(_:)`'s
/// alias-expanding semantics.
var modelValueErasingTags: PureYAML.Model.Value {
switch self {
case let .mapping(mapping):
.mapping(.init(mapping.pairs.map { pair in
.init(key: pair.key, value: pair.value.modelValueErasingTags)
}))
case let .scalar(scalar):
scalar.value
case let .sequence(sequence):
.sequence(sequence.values.map(\.modelValueErasingTags))
case let .alias(_, node):
node.modelValueErasingTags
}
}
}
public extension PureYAML.Tagged.Constructor where Output == PureYAML.Model.Value {
/// Explicit fallback constructor that preserves order and duplicates while erasing tags.
static var modelValueErasingTags: Self {
Self().fallingBackTo { node, _ in
node.modelValueErasingTags
}
}
}