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
|
/*
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
extension RenderNode {
/// A render node variant based on a collection of pre-defined traits.
///
/// A variant features a collection of traits and a path where the variant can be found.
/// When rendered as JSON, the data looks like this:
/// ```json
/// {
/// "traits" : [
/// { "interfaceLanguage": "swift" }
/// ],
/// "paths" : ["/path/to/variant"]
/// }
/// ```
public struct Variant: Codable, Equatable {
/// A trait describing an aspect of the render variant.
public enum Trait: Codable, Hashable {
/// Presentation language (e.g. Swift or Obj-C).
case interfaceLanguage(String)
enum CodingKeys: String, CodingKey, CaseIterable {
case interfaceLanguage
}
public enum Error: DescribedError {
case invalidTrait
public var errorDescription: String {
switch self {
case .invalidTrait: return "None of expected trait keys \(CodingKeys.allCases) was found."
}
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let language = try container.decodeIfPresent(String.self, forKey: .interfaceLanguage) {
self = .interfaceLanguage(language)
return
}
throw Error.invalidTrait
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .interfaceLanguage(let language):
try container.encode(language, forKey: .interfaceLanguage)
}
}
}
/// Collection of traits identifying the variant.
public var traits: [Trait]
/// The paths to the variant.
public var paths: [String]
enum CodingKeys: String, CodingKey {
case traits, paths
}
public init(traits: [Trait], paths: [String]) {
self.traits = traits
self.paths = paths
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
traits = try container.decode([Trait].self, forKey: .traits)
paths = try container.decode([String].self, forKey: .paths)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(traits, forKey: .traits)
try container.encode(paths, forKey: .paths)
}
}
}
|