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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
/// A Codable container for reference pages sections.
///
/// This allows decoding a ``RenderSection`` into its appropriate concrete type, based on the section's
/// ``RenderSection/kind``.
public struct CodableContentSection: Codable, Equatable {
var section: RenderSection {
get {
typeErasedSection.value
}
set {
typeErasedSection = AnyRenderSection(newValue)
}
}
private var typeErasedSection: AnyRenderSection
/// Creates a codable content section from the given section.
public init(_ section: RenderSection) {
self.typeErasedSection = AnyRenderSection(section)
self.section = section
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let kind = try container.decode(RenderSectionKind.self, forKey: .kind)
self.typeErasedSection = AnyRenderSection(ContentRenderSection(kind: .content, content: []))
switch kind {
case .discussion:
section = try ContentRenderSection(from: decoder)
case .content:
section = try ContentRenderSection(from: decoder)
case .taskGroup:
section = try TaskGroupRenderSection(from: decoder)
case .relationships:
section = try RelationshipsRenderSection(from: decoder)
case .declarations:
section = try DeclarationsRenderSection(from: decoder)
case .parameters:
section = try ParametersRenderSection(from: decoder)
case .attributes:
section = try AttributesRenderSection(from: decoder)
case .properties:
section = try PropertiesRenderSection(from: decoder)
case .restParameters:
section = try RESTParametersRenderSection(from: decoder)
case .restEndpoint:
section = try RESTEndpointRenderSection(from: decoder)
case .restBody:
section = try RESTBodyRenderSection(from: decoder)
case .restResponses:
section = try RESTResponseRenderSection(from: decoder)
case .plistDetails:
section = try PlistDetailsRenderSection(from: decoder)
case .possibleValues:
section = try PossibleValuesRenderSection(from: decoder)
case .mentions:
section = try MentionsRenderSection(from: decoder)
default: fatalError()
}
}
private enum CodingKeys: CodingKey {
case kind
}
public func encode(to encoder: Encoder) throws {
try section.encode(to: encoder)
}
}
|