PureYAML/Sources/Tagged/Tagged.Pair.swift
Mihaela Mihaljevic e51df57c35 feat(tagged): carry mapping keys as full nodes
Closes #12 and closes #13. Tagged.Pair stores keyNode instead of a
bare string: alias mapping keys parse on the Tagged path (composer
resolves them through the anchors table, dumper re-emits the reference
with the mandatory space before the colon since a colon is a legal
anchor character), key anchors register for later aliases and re-emit,
and a key's presentation style survives dump and reparse. key and
keyTag remain as read-through computed accessors and the string-key
convenience init keeps most call sites source compatible. A style-less
hand-built key keeps the plain-when-safe emission convention. The
corpus round-trip law compares key nodes structurally; alias-key
documents move from rejected to round-tripping.
2026-07-16 15:42:56 +02:00

45 lines
1.4 KiB
Swift

public extension PureYAML.Tagged {
struct Pair: Equatable, Sendable {
/// The key as a full node, carrying its tag, anchor, presentation
/// style, and alias-ness, so anchored, styled, and alias keys
/// round-trip instead of collapsing to a bare string (issues #12
/// and #13).
public var keyNode: Node
public var value: Node
public init(
keyNode: Node,
value: Node,
) {
self.keyNode = keyNode
self.value = value
}
/// Convenience for the overwhelmingly common plain string key.
public init(
key: String,
keyTag: Tag? = nil,
value: Node,
) {
self.init(
keyNode: .scalar(.init(rawValue: key, value: .string(key), tag: keyTag)),
value: value,
)
}
/// The key's resolved raw text: an alias key reads through to its
/// anchored scalar, matching `parse(_:)`'s alias-key semantics.
public var key: String {
guard case let .scalar(scalar) = keyNode.resolved else {
return ""
}
return scalar.rawValue
}
/// The key's tag, read through an alias to its resolution.
public var keyTag: Tag? {
keyNode.resolved.tag
}
}
}