|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -import Foundation |
| 16 | + |
17 | 17 | import SwiftProtobufPluginLibrary |
18 | 18 |
|
19 | | -enum GenerationError: Error { |
| 19 | +enum GenerationError: Error, CustomStringConvertible { |
20 | 20 | /// Raised when parsing the parameter string and found an unknown key |
21 | 21 | case unknownParameter(name: String) |
22 | 22 | /// Raised when a parameter was giving an invalid value |
23 | 23 | case invalidParameterValue(name: String, value: String) |
24 | 24 | /// Raised to wrap another error but provide a context message. |
25 | 25 | case wrappedError(message: String, error: any Error) |
26 | 26 |
|
27 | | - var localizedDescription: String { |
| 27 | + var description: String { |
28 | 28 | switch self { |
29 | 29 | case let .unknownParameter(name): |
30 | 30 | return "Unknown generation parameter '\(name)'" |
31 | 31 | case let .invalidParameterValue(name, value): |
32 | 32 | return "Unknown value for generation parameter '\(name)': '\(value)'" |
33 | 33 | case let .wrappedError(message, error): |
34 | | - return "\(message): \(error.localizedDescription)" |
| 34 | + return "\(message): \(error)" |
35 | 35 | } |
36 | 36 | } |
37 | 37 | } |
@@ -165,24 +165,32 @@ struct GeneratorOptions { |
165 | 165 | guard let string = string, !string.isEmpty else { |
166 | 166 | return [] |
167 | 167 | } |
168 | | - let parts = string.components(separatedBy: ",") |
| 168 | + |
| 169 | + let parts = string.split(separator: ",") |
169 | 170 |
|
170 | 171 | // Partitions the string into the section before the = and after the = |
171 | 172 | let result = parts.map { string -> (key: String, value: String) in |
172 | | - |
173 | 173 | // Finds the equal sign and exits early if none |
174 | | - guard let index = string.range(of: "=")?.lowerBound else { |
175 | | - return (string, "") |
| 174 | + guard let index = string.firstIndex(of: "=") else { |
| 175 | + return (String(string), "") |
176 | 176 | } |
177 | 177 |
|
178 | 178 | // Creates key/value pair and trims whitespace |
179 | 179 | let key = string[..<index] |
180 | | - .trimmingCharacters(in: .whitespacesAndNewlines) |
| 180 | + .trimmingWhitespaceAndNewlines() |
181 | 181 | let value = string[string.index(after: index)...] |
182 | | - .trimmingCharacters(in: .whitespacesAndNewlines) |
| 182 | + .trimmingWhitespaceAndNewlines() |
183 | 183 |
|
184 | 184 | return (key: key, value: value) |
185 | 185 | } |
186 | 186 | return result |
187 | 187 | } |
188 | 188 | } |
| 189 | + |
| 190 | +extension String.SubSequence { |
| 191 | + func trimmingWhitespaceAndNewlines() -> String { |
| 192 | + let trimmedSuffix = self.drop(while: { $0.isNewline || $0.isWhitespace }) |
| 193 | + let trimmed = trimmedSuffix.trimmingPrefix(while: { $0.isNewline || $0.isWhitespace }) |
| 194 | + return String(trimmed) |
| 195 | + } |
| 196 | +} |
0 commit comments