Browse Source

Use FoundationEssentials where possible (#23)

Motivation:

FoundationEssentials only includes ... the essentials. We should use it
where available.

Modifications:

- Remove unused Foundation imports
- Replace a Foundation import with a FoundationEssentials import

Result:

Smaller dependency set
George Barnett 1 year ago
parent
commit
e086591b23

+ 6 - 1
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]

+ 7 - 3
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

+ 18 - 10
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[..<index]
-        .trimmingCharacters(in: .whitespacesAndNewlines)
+        .trimmingWhitespaceAndNewlines()
       let value = string[string.index(after: index)...]
-        .trimmingCharacters(in: .whitespacesAndNewlines)
+        .trimmingWhitespaceAndNewlines()
 
       return (key: key, value: value)
     }
     return result
   }
 }
+
+extension String.SubSequence {
+  func trimmingWhitespaceAndNewlines() -> String {
+    let trimmedSuffix = self.drop(while: { $0.isNewline || $0.isWhitespace })
+    let trimmed = trimmedSuffix.trimmingPrefix(while: { $0.isNewline || $0.isWhitespace })
+    return String(trimmed)
+  }
+}