Skip to content

Commit a896cce

Browse files
authored
Rename binary/text format encoding/decoding types. (apple#282)
Rename Protobuf* -> Binary*. Rename Text* -> TextFormat*. Delete unnecessary "import Swift" everywhere.
2 parents f10c021 + 10f9511 commit a896cce

36 files changed

+443
-484
lines changed

Sources/SwiftProtobuf/ProtobufDecoder.swift renamed to Sources/SwiftProtobuf/BinaryDecoder.swift

Lines changed: 166 additions & 168 deletions
Large diffs are not rendered by default.

Sources/SwiftProtobuf/ProtobufDecodingError.swift renamed to Sources/SwiftProtobuf/BinaryDecodingError.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Sources/SwiftProtobuf/ProtobufDecodingError.swift - Protobuf binary decoding errors
1+
// Sources/SwiftProtobuf/BinaryDecodingError.swift - Protobuf binary decoding errors
22
//
33
// Copyright (c) 2014 - 2017 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -12,7 +12,7 @@
1212
///
1313
// -----------------------------------------------------------------------------
1414

15-
public enum ProtobufDecodingError: Error {
15+
public enum BinaryDecodingError: Error {
1616
/// Extraneous data remained after decoding should have been complete
1717
case trailingGarbage
1818
/// The data stopped before we expected

Sources/SwiftProtobuf/ProtobufEncoder.swift renamed to Sources/SwiftProtobuf/BinaryEncoder.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Sources/SwiftProtobuf/ProtobufEncoder.swift - Binary encoding support
1+
// Sources/SwiftProtobuf/BinaryEncoder.swift - Binary encoding support
22
//
33
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -14,17 +14,16 @@
1414
// -----------------------------------------------------------------------------
1515

1616
import Foundation
17-
import Swift
1817

1918
/*
2019
* Encoder for Binary Protocol Buffer format
2120
*
2221
* TODO: Should this be a class?
2322
*/
24-
public struct ProtobufEncoder {
23+
public struct BinaryEncoder {
2524
private var pointer: UnsafeMutablePointer<UInt8>
2625

27-
public init(pointer: UnsafeMutablePointer<UInt8>) {
26+
public init(forWritingInto pointer: UnsafeMutablePointer<UInt8>) {
2827
self.pointer = pointer
2928
}
3029

Sources/SwiftProtobuf/ProtobufEncodingSizeVisitor.swift renamed to Sources/SwiftProtobuf/BinaryEncodingSizeVisitor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Sources/SwiftProtobuf/ProtobufEncodingSizeVisitor.swift - Binary size calculation support
1+
// Sources/SwiftProtobuf/BinaryEncodingSizeVisitor.swift - Binary size calculation support
22
//
33
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -18,7 +18,7 @@ import Foundation
1818
/// Visitor that calculates the binary-encoded size of a message so that a
1919
/// properly sized `Data` or `UInt8` array can be pre-allocated before
2020
/// serialization.
21-
final class ProtobufEncodingSizeVisitor: Visitor {
21+
final class BinaryEncodingSizeVisitor: Visitor {
2222

2323
/// Accumulates the required size of the message during traversal.
2424
var serializedSize: Int = 0

Sources/SwiftProtobuf/ProtobufEncodingVisitor.swift renamed to Sources/SwiftProtobuf/BinaryEncodingVisitor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Sources/SwiftProtobuf/ProtobufEncodingVisitor.swift - Binary encoding support
1+
// Sources/SwiftProtobuf/BinaryEncodingVisitor.swift - Binary encoding support
22
//
33
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -16,9 +16,9 @@
1616
import Foundation
1717

1818
/// Visitor that encodes a message graph in the protobuf binary wire format.
19-
final class ProtobufEncodingVisitor: Visitor {
19+
final class BinaryEncodingVisitor: Visitor {
2020

21-
private var encoder: ProtobufEncoder
21+
private var encoder: BinaryEncoder
2222

2323
/// Creates a new visitor that writes the binary-coded message into the memory
2424
/// at the given pointer.
@@ -27,7 +27,7 @@ final class ProtobufEncodingVisitor: Visitor {
2727
/// is large enough to hold the entire encoded message. For performance
2828
/// reasons, the encoder does not make any attempts to verify this.
2929
init(forWritingInto pointer: UnsafeMutablePointer<UInt8>) {
30-
encoder = ProtobufEncoder(pointer: pointer)
30+
encoder = BinaryEncoder(forWritingInto: pointer)
3131
}
3232

3333
func visitUnknown(bytes: Data) {

Sources/SwiftProtobuf/ProtobufTypeAdditions.swift renamed to Sources/SwiftProtobuf/BinaryTypeAdditions.swift

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Sources/SwiftProtobuf/ProtobufTypeAdditions.swift - Per-type binary coding
1+
// Sources/SwiftProtobuf/BinaryTypeAdditions.swift - Per-type binary coding
22
//
33
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
@@ -13,7 +13,6 @@
1313
///
1414
// -----------------------------------------------------------------------------
1515

16-
import Swift
1716
import Foundation
1817

1918
// TODO: Examine how other proto2 implementations treat wire type mismatches
@@ -27,7 +26,7 @@ import Foundation
2726
///
2827
extension ProtobufFloat {
2928
public static var protobufWireFormat: WireFormat { return .fixed32 }
30-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Float) {
29+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Float) {
3130
encoder.putFloatValue(value: value)
3231
}
3332

@@ -42,7 +41,7 @@ extension ProtobufFloat {
4241
///
4342
extension ProtobufDouble {
4443
public static var protobufWireFormat: WireFormat { return .fixed64 }
45-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Double) {
44+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Double) {
4645
encoder.putDoubleValue(value: value)
4746
}
4847

@@ -57,7 +56,7 @@ extension ProtobufDouble {
5756
extension ProtobufInt32 {
5857
public static var protobufWireFormat: WireFormat { return .varint }
5958

60-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int32) {
59+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int32) {
6160
encoder.putVarInt(value: Int64(value))
6261
}
6362

@@ -72,7 +71,7 @@ extension ProtobufInt32 {
7271
extension ProtobufInt64 {
7372
public static var protobufWireFormat: WireFormat { return .varint }
7473

75-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int64) {
74+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int64) {
7675
encoder.putVarInt(value: value)
7776
}
7877

@@ -87,7 +86,7 @@ extension ProtobufInt64 {
8786
extension ProtobufUInt32 {
8887
public static var protobufWireFormat: WireFormat { return .varint }
8988

90-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: UInt32) {
89+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: UInt32) {
9190
encoder.putVarInt(value: UInt64(value))
9291
}
9392

@@ -102,7 +101,7 @@ extension ProtobufUInt32 {
102101
extension ProtobufUInt64 {
103102
public static var protobufWireFormat: WireFormat { return .varint }
104103

105-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: UInt64) {
104+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: UInt64) {
106105
encoder.putVarInt(value: value)
107106
}
108107

@@ -117,7 +116,7 @@ extension ProtobufUInt64 {
117116
extension ProtobufSInt32 {
118117
public static var protobufWireFormat: WireFormat { return .varint }
119118

120-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int32) {
119+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int32) {
121120
encoder.putZigZagVarInt(value: Int64(value))
122121
}
123122

@@ -132,7 +131,7 @@ extension ProtobufSInt32 {
132131
extension ProtobufSInt64 {
133132
public static var protobufWireFormat: WireFormat { return .varint }
134133

135-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int64) {
134+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int64) {
136135
encoder.putZigZagVarInt(value: value)
137136
}
138137

@@ -146,7 +145,7 @@ extension ProtobufSInt64 {
146145
///
147146
extension ProtobufFixed32 {
148147
public static var protobufWireFormat: WireFormat { return .fixed32 }
149-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: UInt32) {
148+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: UInt32) {
150149
encoder.putFixedUInt32(value: value)
151150
}
152151

@@ -160,7 +159,7 @@ extension ProtobufFixed32 {
160159
///
161160
extension ProtobufFixed64 {
162161
public static var protobufWireFormat: WireFormat { return .fixed64 }
163-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: UInt64) {
162+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: UInt64) {
164163
encoder.putFixedUInt64(value: value.littleEndian)
165164
}
166165

@@ -174,7 +173,7 @@ extension ProtobufFixed64 {
174173
///
175174
extension ProtobufSFixed32 {
176175
public static var protobufWireFormat: WireFormat { return .fixed32 }
177-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int32) {
176+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int32) {
178177
encoder.putFixedUInt32(value: UInt32(bitPattern: value))
179178
}
180179

@@ -188,7 +187,7 @@ extension ProtobufSFixed32 {
188187
///
189188
extension ProtobufSFixed64 {
190189
public static var protobufWireFormat: WireFormat { return .fixed64 }
191-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Int64) {
190+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Int64) {
192191
encoder.putFixedUInt64(value: UInt64(bitPattern: value.littleEndian))
193192
}
194193

@@ -203,7 +202,7 @@ extension ProtobufSFixed64 {
203202
extension ProtobufBool {
204203
public static var protobufWireFormat: WireFormat { return .varint }
205204

206-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Bool) {
205+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Bool) {
207206
encoder.putBoolValue(value: value)
208207
}
209208

@@ -218,7 +217,7 @@ extension ProtobufBool {
218217

219218
extension ProtobufString {
220219
public static var protobufWireFormat: WireFormat { return .lengthDelimited }
221-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: String) {
220+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: String) {
222221
encoder.putStringValue(value: value)
223222
}
224223

@@ -235,7 +234,7 @@ extension ProtobufString {
235234
extension ProtobufBytes {
236235
public static var protobufWireFormat: WireFormat { return .lengthDelimited }
237236

238-
public static func serializeProtobufValue(encoder: inout ProtobufEncoder, value: Data) {
237+
public static func serializeProtobufValue(encoder: inout BinaryEncoder, value: Data) {
239238
encoder.putBytesValue(value: value)
240239
}
241240

@@ -259,12 +258,12 @@ public extension Message {
259258
}
260259

261260
private func serializeBinary(into pointer: UnsafeMutablePointer<UInt8>) throws {
262-
let visitor = ProtobufEncodingVisitor(forWritingInto: pointer)
261+
let visitor = BinaryEncodingVisitor(forWritingInto: pointer)
263262
try traverse(visitor: visitor)
264263
}
265264

266265
internal func serializedDataSize() throws -> Int {
267-
let visitor = ProtobufEncodingSizeVisitor()
266+
let visitor = BinaryEncodingSizeVisitor()
268267
try traverse(visitor: visitor)
269268
return visitor.serializedSize
270269
}
@@ -283,25 +282,25 @@ public extension Message {
283282

284283
/// Proto2 messages preserve unknown fields
285284
public extension Proto2Message {
286-
public mutating func decodeBinary(from protobufBytes: UnsafePointer<UInt8>, count: Int, extensions: ExtensionSet?) throws {
287-
var protobufDecoder = ProtobufDecoder(protobufPointer: protobufBytes, count: count, extensions: extensions)
288-
try decodeMessage(decoder: &protobufDecoder)
289-
if !protobufDecoder.complete {
290-
throw ProtobufDecodingError.trailingGarbage
285+
public mutating func decodeBinary(from bytes: UnsafePointer<UInt8>, count: Int, extensions: ExtensionSet?) throws {
286+
var decoder = BinaryDecoder(forReadingFrom: bytes, count: count, extensions: extensions)
287+
try decodeMessage(decoder: &decoder)
288+
guard decoder.complete else {
289+
throw BinaryDecodingError.trailingGarbage
291290
}
292-
if let unknownData = protobufDecoder.unknownData {
291+
if let unknownData = decoder.unknownData {
293292
unknownFields.append(protobufData: unknownData)
294293
}
295294
}
296295
}
297296

298297
// Proto3 messages ignore unknown fields
299298
public extension Proto3Message {
300-
public mutating func decodeBinary(from protobufBytes: UnsafePointer<UInt8>, count: Int, extensions: ExtensionSet?) throws {
301-
var protobufDecoder = ProtobufDecoder(protobufPointer: protobufBytes, count: count, extensions: extensions)
302-
try decodeMessage(decoder: &protobufDecoder)
303-
if !protobufDecoder.complete {
304-
throw ProtobufDecodingError.trailingGarbage
299+
public mutating func decodeBinary(from bytes: UnsafePointer<UInt8>, count: Int, extensions: ExtensionSet?) throws {
300+
var decoder = BinaryDecoder(forReadingFrom: bytes, count: count, extensions: extensions)
301+
try decodeMessage(decoder: &decoder)
302+
guard decoder.complete else {
303+
throw BinaryDecodingError.trailingGarbage
305304
}
306305
}
307306
}

Sources/SwiftProtobuf/Decoder.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
///
1515
// -----------------------------------------------------------------------------
1616

17-
import Swift
1817
import Foundation
1918

2019
/// This is the abstract protocol used by the generated code

Sources/SwiftProtobuf/Enum.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
///
1616
// -----------------------------------------------------------------------------
1717

18-
import Swift
19-
2018
public protocol Enum: RawRepresentable, Hashable {
2119
init()
2220
init?(jsonName: String)

Sources/SwiftProtobuf/Errors.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// TODO: It would be nice if coding/decoding errors could include the name
1616
// of the specific field where the error was detected.
1717

18-
import Swift
19-
2018
public enum EncodingError: Error {
2119
/// An unspecified encoding failure
2220
case failure

Sources/SwiftProtobuf/ExtensibleMessage.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
///
1313
// -----------------------------------------------------------------------------
1414

15-
import Swift
16-
1715
// Messages that support extensions implement this protocol
1816
public protocol ExtensibleMessage: Message {
1917
mutating func setExtensionValue<F: AnyExtensionField>(ext: MessageExtension<F, Self>, value: F.ValueType)

0 commit comments

Comments
 (0)