Skip to content

Commit 4f6beaf

Browse files
committed
Use FoundationEssentials where possible
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
1 parent fd197ad commit 4f6beaf

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
internal import Foundation
1817
internal import SwiftProtobuf
1918
package import SwiftProtobufPluginLibrary
2019

@@ -25,6 +24,12 @@ package import struct GRPCCodeGen.Name
2524
package import struct GRPCCodeGen.ServiceDescriptor
2625
package import struct GRPCCodeGen.SourceGenerator
2726

27+
#if canImport(FoundationEssentials)
28+
internal import struct FoundationEssentials.IndexPath
29+
#else
30+
internal import struct Foundation.IndexPath
31+
#endif
32+
2833
/// Parses a ``FileDescriptor`` object into a ``CodeGenerationRequest`` object.
2934
package struct ProtobufCodeGenParser {
3035
let extraModuleImports: [String]

Sources/protoc-gen-grpc-swift/GenerateGRPC.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
import Foundation
1817
import GRPCCodeGen
1918
import GRPCProtobufCodeGen
2019
import SwiftProtobuf
2120
import SwiftProtobufPluginLibrary
2221

22+
#if canImport(FoundationEssentials)
23+
import FoundationEssentials
24+
#else
25+
import Foundation
26+
#endif
27+
2328
@main
2429
final class GenerateGRPC: CodeGenerator {
2530
var version: String? {
@@ -146,8 +151,7 @@ extension GenerateGRPC {
146151
case .fullPath:
147152
return pathParts.dir + pathParts.base + ext
148153
case .pathToUnderscores:
149-
let dirWithUnderscores =
150-
pathParts.dir.replacingOccurrences(of: "/", with: "_")
154+
let dirWithUnderscores = pathParts.dir.replacing("/", with: "_")
151155
return dirWithUnderscores + pathParts.base + ext
152156
case .dropPath:
153157
return pathParts.base + ext

Sources/protoc-gen-grpc-swift/Options.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,25 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import Foundation
16+
1717
import SwiftProtobufPluginLibrary
1818

19-
enum GenerationError: Error {
19+
enum GenerationError: Error, CustomStringConvertible {
2020
/// Raised when parsing the parameter string and found an unknown key
2121
case unknownParameter(name: String)
2222
/// Raised when a parameter was giving an invalid value
2323
case invalidParameterValue(name: String, value: String)
2424
/// Raised to wrap another error but provide a context message.
2525
case wrappedError(message: String, error: any Error)
2626

27-
var localizedDescription: String {
27+
var description: String {
2828
switch self {
2929
case let .unknownParameter(name):
3030
return "Unknown generation parameter '\(name)'"
3131
case let .invalidParameterValue(name, value):
3232
return "Unknown value for generation parameter '\(name)': '\(value)'"
3333
case let .wrappedError(message, error):
34-
return "\(message): \(error.localizedDescription)"
34+
return "\(message): \(error)"
3535
}
3636
}
3737
}
@@ -165,24 +165,32 @@ struct GeneratorOptions {
165165
guard let string = string, !string.isEmpty else {
166166
return []
167167
}
168-
let parts = string.components(separatedBy: ",")
168+
169+
let parts = string.split(separator: ",")
169170

170171
// Partitions the string into the section before the = and after the =
171172
let result = parts.map { string -> (key: String, value: String) in
172-
173173
// 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), "")
176176
}
177177

178178
// Creates key/value pair and trims whitespace
179179
let key = string[..<index]
180-
.trimmingCharacters(in: .whitespacesAndNewlines)
180+
.trimmingWhitespaceAndNewlines()
181181
let value = string[string.index(after: index)...]
182-
.trimmingCharacters(in: .whitespacesAndNewlines)
182+
.trimmingWhitespaceAndNewlines()
183183

184184
return (key: key, value: value)
185185
}
186186
return result
187187
}
188188
}
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

Comments
 (0)