1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// Copied from swift-foundation
//===----------------------------------------------------------------------===//
// Coding Path Node
//===----------------------------------------------------------------------===//
// This construction allows overall fewer and smaller allocations as the coding path is modified.
internal enum _CodingPathNode {
case root
indirect case node(CodingKey, _CodingPathNode)
indirect case indexNode(Int, _CodingPathNode)
var path: [any CodingKey] {
switch self {
case .root:
return []
case let .node(key, parent):
return parent.path + [key]
case let .indexNode(index, parent):
return parent.path + [_CodingKey(index: index)]
}
}
@inline(__always)
func appending(_ key: __owned (some CodingKey)?) -> _CodingPathNode {
if let key {
return .node(key, self)
} else {
return self
}
}
@inline(__always)
func path(byAppending key: __owned (some CodingKey)?) -> [CodingKey] {
if let key {
return self.path + [key]
}
return self.path
}
// Specializations for indexes, commonly used by unkeyed containers.
@inline(__always)
func appending(index: __owned Int) -> _CodingPathNode {
.indexNode(index, self)
}
func path(byAppendingIndex index: __owned Int) -> [CodingKey] {
self.path + [_CodingKey(index: index)]
}
}
//===----------------------------------------------------------------------===//
// Shared Key Type
//===----------------------------------------------------------------------===//
internal enum _CodingKey: CodingKey {
case string(String)
case int(Int)
case index(Int)
@inline(__always)
public init?(stringValue: String) {
self = .string(stringValue)
}
@inline(__always)
public init?(intValue: Int) {
self = .int(intValue)
}
@inline(__always)
internal init(index: Int) {
self = .index(index)
}
var stringValue: String {
switch self {
case let .string(str): return str
case let .int(int): return "\(int)"
case let .index(index): return "Index \(index)"
}
}
var intValue: Int? {
switch self {
case .string: return nil
case let .int(int): return int
case let .index(index): return index
}
}
}
|