|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftOpenAPIGenerator open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +import Foundation |
| 15 | + |
| 16 | +/// A container for a parsed, valid MIME type. |
| 17 | +@_spi(Generated) |
| 18 | +public struct OpenAPIMIMEType: Equatable { |
| 19 | + |
| 20 | + /// The kind of the MIME type. |
| 21 | + public enum Kind: Equatable { |
| 22 | + |
| 23 | + /// Any, spelled as `*/*`. |
| 24 | + case any |
| 25 | + |
| 26 | + /// Any subtype of a concrete type, spelled as `type/*`. |
| 27 | + case anySubtype(type: String) |
| 28 | + |
| 29 | + /// A concrete value, spelled as `type/subtype`. |
| 30 | + case concrete(type: String, subtype: String) |
| 31 | + |
| 32 | + public static func == (lhs: Kind, rhs: Kind) -> Bool { |
| 33 | + switch (lhs, rhs) { |
| 34 | + case (.any, .any): |
| 35 | + return true |
| 36 | + case let (.anySubtype(lhsType), .anySubtype(rhsType)): |
| 37 | + return lhsType.lowercased() == rhsType.lowercased() |
| 38 | + case let (.concrete(lhsType, lhsSubtype), .concrete(rhsType, rhsSubtype)): |
| 39 | + return lhsType.lowercased() == rhsType.lowercased() |
| 40 | + && lhsSubtype.lowercased() == rhsSubtype.lowercased() |
| 41 | + default: |
| 42 | + return false |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// The kind of the MIME type. |
| 48 | + public var kind: Kind |
| 49 | + |
| 50 | + /// Any optional parameters. |
| 51 | + public var parameters: [String: String] |
| 52 | + |
| 53 | + /// Creates a new MIME type. |
| 54 | + /// - Parameters: |
| 55 | + /// - kind: The kind of the MIME type. |
| 56 | + /// - parameters: Any optional parameters. |
| 57 | + public init(kind: Kind, parameters: [String: String] = [:]) { |
| 58 | + self.kind = kind |
| 59 | + self.parameters = parameters |
| 60 | + } |
| 61 | + |
| 62 | + public static func == (lhs: OpenAPIMIMEType, rhs: OpenAPIMIMEType) -> Bool { |
| 63 | + guard lhs.kind == rhs.kind else { |
| 64 | + return false |
| 65 | + } |
| 66 | + // Parameter names are case-insensitive, parameter values are |
| 67 | + // case-sensitive. |
| 68 | + guard lhs.parameters.count == rhs.parameters.count else { |
| 69 | + return false |
| 70 | + } |
| 71 | + if lhs.parameters.isEmpty { |
| 72 | + return true |
| 73 | + } |
| 74 | + func normalizeKeyValue(key: String, value: String) -> (String, String) { |
| 75 | + (key.lowercased(), value) |
| 76 | + } |
| 77 | + let normalizedLeftParams = Dictionary( |
| 78 | + uniqueKeysWithValues: lhs.parameters.map(normalizeKeyValue) |
| 79 | + ) |
| 80 | + let normalizedRightParams = Dictionary( |
| 81 | + uniqueKeysWithValues: rhs.parameters.map(normalizeKeyValue) |
| 82 | + ) |
| 83 | + return normalizedLeftParams == normalizedRightParams |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +extension OpenAPIMIMEType.Kind: LosslessStringConvertible { |
| 88 | + public init?(_ description: String) { |
| 89 | + let typeAndSubtype = |
| 90 | + description |
| 91 | + .split(separator: "/") |
| 92 | + .map(String.init) |
| 93 | + guard typeAndSubtype.count == 2 else { |
| 94 | + return nil |
| 95 | + } |
| 96 | + switch (typeAndSubtype[0], typeAndSubtype[1]) { |
| 97 | + case ("*", let subtype): |
| 98 | + guard subtype == "*" else { |
| 99 | + return nil |
| 100 | + } |
| 101 | + self = .any |
| 102 | + case (let type, "*"): |
| 103 | + self = .anySubtype(type: type) |
| 104 | + case (let type, let subtype): |
| 105 | + self = .concrete(type: type, subtype: subtype) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + public var description: String { |
| 110 | + switch self { |
| 111 | + case .any: |
| 112 | + return "*/*" |
| 113 | + case .anySubtype(let type): |
| 114 | + return "\(type)/*" |
| 115 | + case .concrete(let type, let subtype): |
| 116 | + return "\(type)/\(subtype)" |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +extension OpenAPIMIMEType: LosslessStringConvertible { |
| 122 | + public init?(_ description: String) { |
| 123 | + var components = |
| 124 | + description |
| 125 | + // Split by semicolon |
| 126 | + .split(separator: ";") |
| 127 | + .map(String.init) |
| 128 | + // Trim leading/trailing spaces |
| 129 | + .map { $0.trimmingLeadingAndTrailingSpaces } |
| 130 | + guard !components.isEmpty else { |
| 131 | + return nil |
| 132 | + } |
| 133 | + let firstComponent = components.removeFirst() |
| 134 | + guard let kind = OpenAPIMIMEType.Kind(firstComponent) else { |
| 135 | + return nil |
| 136 | + } |
| 137 | + func parseParameter(_ string: String) -> (String, String)? { |
| 138 | + let components = |
| 139 | + string |
| 140 | + .split(separator: "=") |
| 141 | + .map(String.init) |
| 142 | + guard components.count == 2 else { |
| 143 | + return nil |
| 144 | + } |
| 145 | + return (components[0], components[1]) |
| 146 | + } |
| 147 | + let parameters = |
| 148 | + components |
| 149 | + .compactMap(parseParameter) |
| 150 | + self.init( |
| 151 | + kind: kind, |
| 152 | + parameters: Dictionary( |
| 153 | + parameters, |
| 154 | + // Pick the first value when duplicate parameters are provided. |
| 155 | + uniquingKeysWith: { a, _ in a } |
| 156 | + ) |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + public var description: String { |
| 161 | + ([kind.description] |
| 162 | + + parameters |
| 163 | + .sorted(by: { a, b in a.key < b.key }) |
| 164 | + .map { "\($0)=\($1)" }) |
| 165 | + .joined(separator: "; ") |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +extension String { |
| 170 | + fileprivate var trimmingLeadingAndTrailingSpaces: Self { |
| 171 | + trimmingCharacters(in: .whitespacesAndNewlines) |
| 172 | + } |
| 173 | +} |
0 commit comments