Skip to content

Commit

Permalink
Use FoundationEssentials where possible (#23)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
glbrntt authored Dec 17, 2024
1 parent fd197ad commit e086591
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Sources/GRPCProtobufCodeGen/ProtobufCodeGenParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

internal import Foundation
internal import SwiftProtobuf
package import SwiftProtobufPluginLibrary

Expand All @@ -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]
Expand Down
10 changes: 7 additions & 3 deletions Sources/protoc-gen-grpc-swift/GenerateGRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down Expand Up @@ -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
Expand Down
28 changes: 18 additions & 10 deletions Sources/protoc-gen-grpc-swift/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
* 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
case invalidParameterValue(name: String, value: String)
/// 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)"
}
}
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit e086591

Please sign in to comment.