Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use FoundationEssentials where possible #23

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading