|
| 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 | + |
| 15 | +/// A parsed representation of the `content-disposition` header described by RFC 6266 containing only |
| 16 | +/// the features relevant to OpenAPI multipart bodies. |
| 17 | +struct ContentDisposition: Hashable { |
| 18 | + |
| 19 | + /// A `disposition-type` parameter value. |
| 20 | + enum DispositionType: Hashable { |
| 21 | + |
| 22 | + /// A form data value. |
| 23 | + case formData |
| 24 | + |
| 25 | + /// Any other value. |
| 26 | + case other(String) |
| 27 | + |
| 28 | + /// Creates a new disposition type value. |
| 29 | + /// - Parameter rawValue: A string representation of the value. |
| 30 | + init(rawValue: String) { |
| 31 | + switch rawValue.lowercased() { |
| 32 | + case "form-data": self = .formData |
| 33 | + default: self = .other(rawValue) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// A string representation of the value. |
| 38 | + var rawValue: String { |
| 39 | + switch self { |
| 40 | + case .formData: return "form-data" |
| 41 | + case .other(let string): return string |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// The disposition type value. |
| 47 | + var dispositionType: DispositionType |
| 48 | + |
| 49 | + /// A content disposition parameter name. |
| 50 | + enum ParameterName: Hashable { |
| 51 | + |
| 52 | + /// The name parameter. |
| 53 | + case name |
| 54 | + |
| 55 | + /// The filename parameter. |
| 56 | + case filename |
| 57 | + |
| 58 | + /// Any other parameter. |
| 59 | + case other(String) |
| 60 | + |
| 61 | + /// Creates a new parameter name. |
| 62 | + /// - Parameter rawValue: A string representation of the name. |
| 63 | + init(rawValue: String) { |
| 64 | + switch rawValue.lowercased() { |
| 65 | + case "name": self = .name |
| 66 | + case "filename": self = .filename |
| 67 | + default: self = .other(rawValue) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// A string representation of the name. |
| 72 | + var rawValue: String { |
| 73 | + switch self { |
| 74 | + case .name: return "name" |
| 75 | + case .filename: return "filename" |
| 76 | + case .other(let string): return string |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// The parameters of the content disposition value. |
| 82 | + var parameters: [ParameterName: String] = [:] |
| 83 | + |
| 84 | + /// The name parameter value. |
| 85 | + var name: String? { |
| 86 | + get { parameters[.name] } |
| 87 | + set { parameters[.name] = newValue } |
| 88 | + } |
| 89 | + |
| 90 | + /// The filename parameter value. |
| 91 | + var filename: String? { |
| 92 | + get { parameters[.filename] } |
| 93 | + set { parameters[.filename] = newValue } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +extension ContentDisposition: RawRepresentable { |
| 98 | + |
| 99 | + /// Creates a new instance with the specified raw value. |
| 100 | + /// |
| 101 | + /// https://datatracker.ietf.org/doc/html/rfc6266#section-4.1 |
| 102 | + /// - Parameter rawValue: The raw value to use for the new instance. |
| 103 | + init?(rawValue: String) { |
| 104 | + var components = rawValue.split(separator: ";").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 105 | + guard !components.isEmpty else { return nil } |
| 106 | + self.dispositionType = DispositionType(rawValue: components.removeFirst()) |
| 107 | + let parameterTuples: [(ParameterName, String)] = components.compactMap { component in |
| 108 | + let parameterComponents = component.split(separator: "=", maxSplits: 1) |
| 109 | + .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } |
| 110 | + guard parameterComponents.count == 2 else { return nil } |
| 111 | + let valueWithoutQuotes = parameterComponents[1].trimmingCharacters(in: ["\""]) |
| 112 | + return (.init(rawValue: parameterComponents[0]), valueWithoutQuotes) |
| 113 | + } |
| 114 | + self.parameters = Dictionary(parameterTuples, uniquingKeysWith: { a, b in a }) |
| 115 | + } |
| 116 | + |
| 117 | + /// The corresponding value of the raw type. |
| 118 | + var rawValue: String { |
| 119 | + var string = "" |
| 120 | + string.append(dispositionType.rawValue) |
| 121 | + if !parameters.isEmpty { |
| 122 | + for (key, value) in parameters.sorted(by: { $0.key.rawValue < $1.key.rawValue }) { |
| 123 | + string.append("; \(key.rawValue)=\"\(value)\"") |
| 124 | + } |
| 125 | + } |
| 126 | + return string |
| 127 | + } |
| 128 | +} |
0 commit comments