diff --git a/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift b/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift index e0a0667..a502b96 100644 --- a/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift +++ b/Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift @@ -14,7 +14,6 @@ * limitations under the License. */ -internal import Foundation internal import SwiftProtobuf package import SwiftProtobufPluginLibrary @@ -25,6 +24,12 @@ package import struct GRPCCodeGen.Name package import struct GRPCCodeGen.ServiceDescriptor package import struct GRPCCodeGen.SourceGenerator +#if canImport(FoundationEssentials) +internal import struct FoundationEssentials.IndexPath +#else +internal import struct Foundation.IndexPath +#endif + /// Parses a ``FileDescriptor`` object into a ``CodeGenerationRequest`` object. package struct ProtobufCodeGenParser { let extraModuleImports: [String] diff --git a/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift b/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift index f878d7e..c38ec83 100644 --- a/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift +++ b/Sources/protoc-gen-grpc-swift/GenerateGRPC.swift @@ -14,12 +14,17 @@ * limitations under the License. */ -import Foundation import GRPCCodeGen import GRPCProtobufCodeGen import SwiftProtobuf import SwiftProtobufPluginLibrary +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + @main final class GenerateGRPC: CodeGenerator { var version: String? { @@ -146,8 +151,7 @@ extension GenerateGRPC { case .fullPath: return pathParts.dir + pathParts.base + ext case .pathToUnderscores: - let dirWithUnderscores = - pathParts.dir.replacingOccurrences(of: "/", with: "_") + let dirWithUnderscores = pathParts.dir.replacing("/", with: "_") return dirWithUnderscores + pathParts.base + ext case .dropPath: return pathParts.base + ext diff --git a/Sources/protoc-gen-grpc-swift/Options.swift b/Sources/protoc-gen-grpc-swift/Options.swift index 05bf131..16079d5 100644 --- a/Sources/protoc-gen-grpc-swift/Options.swift +++ b/Sources/protoc-gen-grpc-swift/Options.swift @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import Foundation + import SwiftProtobufPluginLibrary -enum GenerationError: Error { +enum GenerationError: Error, CustomStringConvertible { /// Raised when parsing the parameter string and found an unknown key case unknownParameter(name: String) /// Raised when a parameter was giving an invalid value @@ -24,14 +24,14 @@ enum GenerationError: Error { /// Raised to wrap another error but provide a context message. case wrappedError(message: String, error: any Error) - var localizedDescription: String { + var description: String { switch self { case let .unknownParameter(name): return "Unknown generation parameter '\(name)'" case let .invalidParameterValue(name, value): return "Unknown value for generation parameter '\(name)': '\(value)'" case let .wrappedError(message, error): - return "\(message): \(error.localizedDescription)" + return "\(message): \(error)" } } } @@ -165,24 +165,32 @@ struct GeneratorOptions { guard let string = string, !string.isEmpty else { return [] } - let parts = string.components(separatedBy: ",") + + let parts = string.split(separator: ",") // Partitions the string into the section before the = and after the = let result = parts.map { string -> (key: String, value: String) in - // Finds the equal sign and exits early if none - guard let index = string.range(of: "=")?.lowerBound else { - return (string, "") + guard let index = string.firstIndex(of: "=") else { + return (String(string), "") } // Creates key/value pair and trims whitespace let key = string[.. String { + let trimmedSuffix = self.drop(while: { $0.isNewline || $0.isWhitespace }) + let trimmed = trimmedSuffix.trimmingPrefix(while: { $0.isNewline || $0.isWhitespace }) + return String(trimmed) + } +}