// MARK: - Canonical form extension PureYAML.Tagged.Dumper { /// The YAML-specification canonical form over a tagged tree: an /// explicit `---` document start, every node's tag explicit (the /// carried tag when present, the value's implicit tag otherwise), /// double-quoted scalar content, flow collections one entry per line, /// and anchors and alias references preserved. Canonical normalizes /// scalar spellings: content renders from the resolved value, not the /// source rawValue. func canonicalDocument(_ node: PureYAML.Tagged.Node) -> String { "---\n" + canonicalLines(node, indent: 0).joined(separator: "\n") + "\n" } func canonicalLines( _ node: PureYAML.Tagged.Node, indent: Int, ) -> [String] { let prefix = String(repeating: " ", count: indent) switch node { case let .alias(anchor, _): return [prefix + "*" + anchor] case let .scalar(scalar): return [prefix + canonicalProperties(node) + canonicalScalarContent(scalar)] case let .sequence(sequence): return canonicalSequenceLines(sequence, node: node, indent: indent) case let .mapping(mapping): return canonicalMappingLines(mapping, node: node, indent: indent) } } func canonicalSequenceLines( _ sequence: PureYAML.Tagged.Sequence, node: PureYAML.Tagged.Node, indent: Int, ) -> [String] { let prefix = String(repeating: " ", count: indent) let props = canonicalProperties(node, fallback: .sequence) guard !sequence.values.isEmpty else { return [prefix + props + "[]"] } var lines = [prefix + props + "["] for value in sequence.values { var entry = canonicalLines(value, indent: indent + options.indentWidth) entry[entry.count - 1] += "," lines.append(contentsOf: entry) } lines.append(prefix + "]") return lines } func canonicalMappingLines( _ mapping: PureYAML.Tagged.Mapping, node: PureYAML.Tagged.Node, indent: Int, ) -> [String] { let prefix = String(repeating: " ", count: indent) let props = canonicalProperties(node, fallback: .mapping) guard !mapping.pairs.isEmpty else { return [prefix + props + "{}"] } let inner = indent + options.indentWidth let innerPrefix = String(repeating: " ", count: inner) var lines = [prefix + props + "{"] for pair in orderedPairs(mapping) { var keyLines = canonicalLines(pair.keyNode, indent: inner) keyLines[0] = innerPrefix + "? " + String(keyLines[0].dropFirst(innerPrefix.count)) lines.append(contentsOf: keyLines) var valueLines = canonicalLines(pair.value, indent: inner) valueLines[0] = innerPrefix + ": " + String(valueLines[0].dropFirst(innerPrefix.count)) valueLines[valueLines.count - 1] += "," lines.append(contentsOf: valueLines) } lines.append(prefix + "}") return lines } /// The canonical `&anchor !tag ` prefix: anchor first, then the carried /// tag, or the value's implicit tag made explicit when none is carried. func canonicalProperties( _ node: PureYAML.Tagged.Node, fallback: PureYAML.Tagged.Tag? = nil, ) -> String { var parts: [String] = [] if let anchor = node.anchor, node.kind != .alias { parts.append("&" + anchor) } if let tag = node.tag ?? fallback ?? implicitTag(of: node) { parts.append(renderTag(tag)) } return parts.joined(separator: " ") + " " } func implicitTag(of node: PureYAML.Tagged.Node) -> PureYAML.Tagged.Tag? { guard case let .scalar(scalar) = node else { return nil } switch scalar.value { case .null: return .null case .bool: return .bool case .int: return .int case .double: return .float case .string: return .string case .sequence, .mapping: return nil } } /// Canonical scalar content: always double-quoted, rendered from the /// resolved value so `NO` normalizes to `"false"` under its bool tag. func canonicalScalarContent(_ scalar: PureYAML.Tagged.Scalar) -> String { switch scalar.value { case .null: "\"null\"" case let .bool(flag): "\"\(flag ? "true" : "false")\"" case let .int(number): "\"\(number)\"" case let .double(number): "\"\(number)\"" case let .string(string): PureYAML.Emitting.Dumper(options: options).quote(string) case .sequence, .mapping: "\"\"" } } }