Regeneration

This commit is contained in:
Luke Hagar
2024-04-11 11:10:38 -05:00
parent 5c1a849e2e
commit d4c7b32e80
502 changed files with 25175 additions and 22 deletions

View File

@@ -0,0 +1,37 @@
// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
import Foundation
enum StringSubstitutionError: Swift.Error {
case `internal`
case missingParameter(named: String, in: String)
}
extension String {
typealias SubstitutionHandler = (_ key: String) throws -> String?
func substituteComponents(using substitutionHandler: SubstitutionHandler) throws -> String {
let regex = try NSRegularExpression(pattern: "\\{([^}]+)\\}")
let range = NSRange(startIndex..<endIndex, in: self)
let matches = regex.matches(in: self, range: range)
var substitutedPath = self
// Reverse matches so that we can replace substrings in place.
for match in matches.reversed() {
let templateRange = match.range
guard let keyRange = Range(match.range(at: 1), in: self) else {
throw StringSubstitutionError.internal
}
let key = String(self[keyRange])
guard let serializedParameter = try substitutionHandler(key) else {
throw StringSubstitutionError.missingParameter(named: key, in: self)
}
let startIndex = substitutedPath.index(substitutedPath.startIndex, offsetBy: templateRange.location)
let endIndex = substitutedPath.index(startIndex, offsetBy: templateRange.length)
substitutedPath.replaceSubrange(startIndex..<endIndex, with: serializedParameter)
}
return substitutedPath
}
}