extension PureYAML.Emitting { /// The deepest collection nesting the recursive renderers walk before a /// value is refused. It matches the parser's ``PureYAML/Parsing/Parser/maxNestingDepth`` /// exactly: a value nested deeper than the parser accepts can never round-trip /// through PureYAML, so emitting it would produce YAML the paired parser /// rejects. Parsed trees are always within this bound, so real round-trips /// are unaffected. static var maxNestingDepth: Int { PureYAML.Parsing.Parser.maxNestingDepth } /// The reparseable marker returned for a value nested past ``maxNestingDepth``. /// It is a valid YAML document that resolves to `null`, with a comment that /// names the refused limit, so a caller never receives a crash or silent /// truncation. static var nestingLimitDocument: String { "# value nesting exceeds the emitter limit of \(maxNestingDepth); not serialized\nnull\n" } /// Iteratively (no recursion, so this cannot itself overflow) reports whether /// a model value nests collections deeper than ``maxNestingDepth``. Complex /// keys count toward depth like values; scalars are leaves. static func exceedsNestingLimit(_ value: PureYAML.Model.Value) -> Bool { var stack: [(value: PureYAML.Model.Value, depth: Int)] = [(value, 1)] while let (node, depth) = stack.popLast() { switch node { case let .sequence(values): if depth > maxNestingDepth { return true } for child in values { stack.append((child, depth + 1)) } case let .mapping(mapping): if depth > maxNestingDepth { return true } for pair in mapping.pairs { stack.append((pair.value, depth + 1)) stack.append((pair.keyNode.value, depth + 1)) } case .null, .bool, .int, .double, .string: continue } } return false } /// Iteratively reports whether a tagged node nests collections deeper than /// ``maxNestingDepth``. An alias renders as a `*name` reference, not an /// expanded subtree, so it is a leaf here and its aliased node is not walked. static func exceedsNestingLimit(_ node: PureYAML.Tagged.Node) -> Bool { var stack: [(node: PureYAML.Tagged.Node, depth: Int)] = [(node, 1)] while let (current, depth) = stack.popLast() { switch current { case let .sequence(sequence): if depth > maxNestingDepth { return true } for child in sequence.values { stack.append((child, depth + 1)) } case let .mapping(mapping): if depth > maxNestingDepth { return true } for pair in mapping.pairs { stack.append((pair.value, depth + 1)) stack.append((pair.keyNode, depth + 1)) } case .scalar, .alias: continue } } return false } }