|
| 1 | +/* |
| 2 | + This source file is part of the Swift.org open source project |
| 3 | + |
| 4 | + Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 5 | + Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | + |
| 7 | + See https://swift.org/LICENSE.txt for license information |
| 8 | + See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +*/ |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +public extension SymbolGraph.Symbol.Availability { |
| 14 | + /** |
| 15 | + Availability of a symbol in a particular domain. |
| 16 | + */ |
| 17 | + struct AvailabilityItem: Codable { |
| 18 | + /** |
| 19 | + The domain in which this availability applies; if undefined, applies to all reasonable domains. |
| 20 | + */ |
| 21 | + public var domain: Domain? |
| 22 | + |
| 23 | + /** |
| 24 | + The version in which a symbol appeared. |
| 25 | + */ |
| 26 | + public var introducedVersion: SymbolGraph.SemanticVersion? |
| 27 | + |
| 28 | + /** |
| 29 | + The version in which a symbol was deprecated. |
| 30 | + |
| 31 | + > Note: If a symbol is *unconditionally deprecated*, this key should be undefined or `null` (see below). |
| 32 | + */ |
| 33 | + public var deprecatedVersion: SymbolGraph.SemanticVersion? |
| 34 | + |
| 35 | + /** |
| 36 | + The version in which a symbol was obsoleted. |
| 37 | + |
| 38 | + > Note: If a symbol is *unconditionally obsoleted*, this key should be undefined or `null` (see below). |
| 39 | + */ |
| 40 | + public var obsoletedVersion: SymbolGraph.SemanticVersion? |
| 41 | + |
| 42 | + /** |
| 43 | + A message further describing availability for documentation purposes. |
| 44 | + */ |
| 45 | + public var message: String? |
| 46 | + |
| 47 | + /** |
| 48 | + If a symbol was renamed at this point, its new name is given here. |
| 49 | + |
| 50 | + > Note: This is not necessarily an identifier but an attribute string provided by a compiler. |
| 51 | + */ |
| 52 | + public var renamed: String? |
| 53 | + |
| 54 | + /** |
| 55 | + If defined and `true`, is unconditionally deprecated regardless |
| 56 | + of version, and possibly regardless of domain. |
| 57 | + If undefined, assume `false`. |
| 58 | + */ |
| 59 | + public var isUnconditionallyDeprecated: Bool |
| 60 | + |
| 61 | + /** |
| 62 | + If defined and `true`, is unconditionally unavailable regardless |
| 63 | + of version, and possibly regardless of domain. |
| 64 | + If undefined, assume `false`. |
| 65 | + */ |
| 66 | + public var isUnconditionallyUnavailable: Bool |
| 67 | + |
| 68 | + /** |
| 69 | + A formal but lenient indication that this symbol will definitely be deprecated |
| 70 | + in future version of the availability domain, but the version hasn't |
| 71 | + been decided yet. This is also known as *soft deprecation*. |
| 72 | + |
| 73 | + Soft deprecation should not provide build errors, runtime errors, or |
| 74 | + warnings that can be upgraded to errors, but provides extra time for |
| 75 | + usage of a symbol to decrease before providing an explicit |
| 76 | + availability deadline. |
| 77 | + |
| 78 | + If a symbol is formally deprecated with an explicit version in the |
| 79 | + `deprecated` property above, the `willEventuallyBeDeprecated` key |
| 80 | + should not exist. In the event that it is still included |
| 81 | + despite this specification, `deprecated` should always take precedence |
| 82 | + over this property in clients. |
| 83 | + */ |
| 84 | + public var willEventuallyBeDeprecated: Bool |
| 85 | + |
| 86 | + enum CodingKeys: String, CodingKey { |
| 87 | + case domain |
| 88 | + case introducedVersion = "introduced" |
| 89 | + case deprecatedVersion = "deprecated" |
| 90 | + case obsoletedVersion = "obsoleted" |
| 91 | + case message |
| 92 | + case renamed |
| 93 | + case isUnconditionallyDeprecated |
| 94 | + case isUnconditionallyUnavailable |
| 95 | + case willEventuallyBeDeprecated |
| 96 | + } |
| 97 | + |
| 98 | + public init(from decoder: Decoder) throws { |
| 99 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 100 | + let domain = try container.decodeIfPresent(Domain.self, forKey: .domain) |
| 101 | + if domain?.rawValue == "*" { |
| 102 | + self.domain = nil |
| 103 | + } else { |
| 104 | + self.domain = domain |
| 105 | + } |
| 106 | + self.introducedVersion = try container.decodeIfPresent(SymbolGraph.SemanticVersion.self, forKey: .introducedVersion) |
| 107 | + self.deprecatedVersion = try container.decodeIfPresent(SymbolGraph.SemanticVersion.self, forKey: .deprecatedVersion) |
| 108 | + self.obsoletedVersion = try container.decodeIfPresent(SymbolGraph.SemanticVersion.self, forKey: .obsoletedVersion) |
| 109 | + self.message = try container.decodeIfPresent(String.self, forKey: .message) |
| 110 | + self.renamed = try container.decodeIfPresent(String.self, forKey: .renamed) |
| 111 | + self.isUnconditionallyDeprecated = try container.decodeIfPresent(Bool.self, forKey: .isUnconditionallyDeprecated) ?? false |
| 112 | + self.isUnconditionallyUnavailable = try container.decodeIfPresent(Bool.self, forKey: .isUnconditionallyUnavailable) ?? false |
| 113 | + self.willEventuallyBeDeprecated = try container.decodeIfPresent(Bool.self, forKey: .willEventuallyBeDeprecated) ?? false |
| 114 | + } |
| 115 | + |
| 116 | + public func encode(to encoder: Encoder) throws { |
| 117 | + var container = encoder.container(keyedBy: CodingKeys.self) |
| 118 | + try container.encode(domain, forKey: .domain) |
| 119 | + if let introducedVersion = introducedVersion { |
| 120 | + try container.encode(introducedVersion, forKey: .introducedVersion) |
| 121 | + } |
| 122 | + if let deprecatedVersion = deprecatedVersion { |
| 123 | + try container.encode(deprecatedVersion, forKey: .deprecatedVersion) |
| 124 | + } |
| 125 | + if let obsoletedVersion = obsoletedVersion { |
| 126 | + try container.encode(obsoletedVersion, forKey: .obsoletedVersion) |
| 127 | + } |
| 128 | + if let message = message { |
| 129 | + try container.encode(message, forKey: .message) |
| 130 | + } |
| 131 | + if let renamed = renamed { |
| 132 | + try container.encode(renamed, forKey: .renamed) |
| 133 | + } |
| 134 | + if isUnconditionallyDeprecated { |
| 135 | + try container.encode(isUnconditionallyDeprecated, forKey: .isUnconditionallyDeprecated) |
| 136 | + } |
| 137 | + if isUnconditionallyUnavailable { |
| 138 | + try container.encode(isUnconditionallyUnavailable, forKey: .isUnconditionallyUnavailable) |
| 139 | + } |
| 140 | + if willEventuallyBeDeprecated { |
| 141 | + try container.encode(willEventuallyBeDeprecated, forKey: .willEventuallyBeDeprecated) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public init(domain: SymbolGraph.Symbol.Availability.Domain?, |
| 146 | + introducedVersion: SymbolGraph.SemanticVersion?, |
| 147 | + deprecatedVersion: SymbolGraph.SemanticVersion?, |
| 148 | + obsoletedVersion: SymbolGraph.SemanticVersion?, |
| 149 | + message: String?, |
| 150 | + renamed: String?, |
| 151 | + isUnconditionallyDeprecated: Bool, |
| 152 | + isUnconditionallyUnavailable: Bool, |
| 153 | + willEventuallyBeDeprecated: Bool) { |
| 154 | + self.domain = domain |
| 155 | + self.introducedVersion = introducedVersion |
| 156 | + self.deprecatedVersion = deprecatedVersion |
| 157 | + self.obsoletedVersion = obsoletedVersion |
| 158 | + self.message = message |
| 159 | + self.renamed = renamed |
| 160 | + self.isUnconditionallyDeprecated = isUnconditionallyDeprecated |
| 161 | + self.isUnconditionallyUnavailable = isUnconditionallyUnavailable |
| 162 | + self.willEventuallyBeDeprecated = willEventuallyBeDeprecated |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments