Skip to content

Commit 19da4fe

Browse files
authored
Add documentation to ProtobufMessage (#148)
1 parent c9a742f commit 19da4fe

File tree

3 files changed

+527
-6
lines changed

3 files changed

+527
-6
lines changed

Sources/OpenSwiftUICore/Data/Protobuf/ProtobufDecoder.swift

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,41 @@
88

99
import Foundation
1010

11+
/// An object that decodes instances of a data type from protobuf objects.
1112
package struct ProtobufDecoder {
13+
/// An error that can occur during `ProtobufDecoder` decoding.
1214
package enum DecodingError: Error {
1315
case failed
1416
}
1517

18+
/// A type representing a field in a protobuf encoding.
1619
package typealias Field = ProtobufFormat.Field
20+
21+
/// A type representing the wire type of a protobuf encoding.
1722
package typealias WireType = ProtobufFormat.WireType
1823

24+
/// The data being decoded.
1925
var data: NSData
26+
27+
/// A pointer to the current position in the data.
2028
var ptr: UnsafeRawPointer
29+
30+
/// A pointer to the end of the data.
2131
var end: UnsafeRawPointer
32+
33+
/// The current packed field.
2234
var packedField: Field = Field(rawValue: 0)
35+
36+
/// A pointer to the end of the packed field.
2337
var packedEnd: UnsafeRawPointer
38+
39+
/// A stack of pointers for nested messages.
2440
var stack: [UnsafeRawPointer] = []
41+
42+
/// User-defined information.
2543
package var userInfo: [CodingUserInfoKey : Any] = [:]
2644

45+
/// Creates an instance with a data buffer.
2746
package init(_ data: Data) {
2847
let nsData = data as NSData
2948
self.data = nsData
@@ -35,6 +54,7 @@ package struct ProtobufDecoder {
3554
}
3655

3756
extension ProtobufDecoder {
57+
/// Decodes the next field in the data.
3858
package mutating func nextField() throws -> ProtobufDecoder.Field? {
3959
guard ptr < end else {
4060
packedField = Field(rawValue: 0)
@@ -55,6 +75,7 @@ extension ProtobufDecoder {
5575
return field
5676
}
5777

78+
/// Skips the next field in the data.
5879
package mutating func skipField(_ field: ProtobufDecoder.Field) throws {
5980
switch field.wireType {
6081
case .varint:
@@ -78,6 +99,10 @@ extension ProtobufDecoder {
7899
}
79100
}
80101

102+
/// Decodes a boolean(Bool) value field from the data.
103+
///
104+
/// - Parameter field: The field to decode.
105+
/// - Returns: A boolean(Bool) value.
81106
package mutating func boolField(_ field: ProtobufDecoder.Field) throws -> Bool {
82107
switch field.wireType {
83108
case .varint:
@@ -96,6 +121,10 @@ extension ProtobufDecoder {
96121
return try decodeVariant() != 0
97122
}
98123

124+
/// Decodes an unsigned integer(UInt) value field from the data.
125+
///
126+
/// - Parameter field: The field to decode.
127+
/// - Returns: An unsigned integer(UInt) value.
99128
package mutating func uintField(_ field: ProtobufDecoder.Field) throws -> UInt {
100129
switch field.wireType {
101130
case .varint:
@@ -114,31 +143,59 @@ extension ProtobufDecoder {
114143
return try decodeVariant()
115144
}
116145

146+
/// Decodes an enum field from the data.
147+
///
148+
/// - Parameter field: The field to decode.
149+
/// - Returns: A ProtobufEnum value.
117150
package mutating func enumField<T>(_ field: ProtobufDecoder.Field) throws -> T? where T: ProtobufEnum {
118151
try T(protobufValue: uintField(field))
119152
}
120153

154+
/// Decodes an unsigned 8-bit integer(UInt8) value field from the data.
155+
///
156+
/// - Parameter field: The field to decode.
157+
/// - Returns: An unsigned 8-bit integer(UInt8) value.
121158
package mutating func uint8Field(_ field: ProtobufDecoder.Field) throws -> UInt8 {
122159
try UInt8(uintField(field))
123160
}
124161

162+
/// Decodes an unsigned 16-bit integer(UInt16) value field from the data.
163+
///
164+
/// - Parameter field: The field to decode.
165+
/// - Returns: An unsigned 16-bit integer(UInt16) value.
125166
package mutating func uint16Field(_ field: ProtobufDecoder.Field) throws -> UInt16 {
126167
try UInt16(uintField(field))
127168
}
128169

170+
/// Decodes an unsigned 32-bit integer(UInt32) value field from the data.
171+
///
172+
/// - Parameter field: The field to decode.
173+
/// - Returns: An unsigned 32-bit integer(UInt32) value.
129174
package mutating func uint32Field(_ field: ProtobufDecoder.Field) throws -> UInt32 {
130175
try UInt32(uintField(field))
131176
}
132177

178+
/// Decodes an unsigned 64-bit integer(UInt64) value field from the data.
179+
///
180+
/// - Parameter field: The field to decode.
181+
/// - Returns: An unsigned 64-bit integer(UInt64) value.
133182
package mutating func uint64Field(_ field: ProtobufDecoder.Field) throws -> UInt64 {
134183
try UInt64(uintField(field))
135184
}
136185

186+
/// Decodes a signed integer(Int) value field from the data.
187+
///
188+
/// - Parameter field: The field to decode.
189+
/// - Returns: A signed integer(Int) value.
137190
package mutating func intField(_ field: ProtobufDecoder.Field) throws -> Int {
138191
let value = Int(bitPattern: try uintField(field))
139192
return Int(bitPattern: UInt(bitPattern: (value >> 1)) ^ UInt(bitPattern: -(value & 1)))
140193
}
141194

195+
/// Decodes a fixed 32-bit integer(UInt32) value field from the data.
196+
///
197+
/// - Parameter field: The field to decode.
198+
/// - Returns: A fixed 32-bit integer(UInt32) value.
142199
package mutating func fixed32Field(_ field: ProtobufDecoder.Field) throws -> UInt32 {
143200
switch field.wireType {
144201
case .lengthDelimited:
@@ -163,6 +220,10 @@ extension ProtobufDecoder {
163220
return value
164221
}
165222

223+
/// Decodes a fixed 64-bit integer(UInt64) value field from the data.
224+
///
225+
/// - Parameter field: The field to decode.
226+
/// - Returns: A fixed 64-bit integer(UInt64) value.
166227
package mutating func fixed64Field(_ field: ProtobufDecoder.Field) throws -> UInt64 {
167228
switch field.wireType {
168229
case .lengthDelimited:
@@ -187,6 +248,10 @@ extension ProtobufDecoder {
187248
return value
188249
}
189250

251+
/// Decodes a float(Float) value field from the data.
252+
///
253+
/// - Parameter field: The field to decode.
254+
/// - Returns: A float(Float) value.
190255
package mutating func floatField(_ field: ProtobufDecoder.Field) throws -> Float {
191256
switch field.wireType {
192257
case .lengthDelimited:
@@ -211,6 +276,10 @@ extension ProtobufDecoder {
211276
return Float(bitPattern: value)
212277
}
213278

279+
/// Decodes a double(Double) value field from the data.
280+
///
281+
/// - Parameter field: The field to decode.
282+
/// - Returns: A double(Double) value.
214283
package mutating func doubleField(_ field: ProtobufDecoder.Field) throws -> Double {
215284
switch field.wireType {
216285
case .fixed64:
@@ -243,11 +312,19 @@ extension ProtobufDecoder {
243312
return Double(bitPattern: value)
244313
}
245314

315+
/// Decodes a CGFloat value field from the data.
316+
///
317+
/// - Parameter field: The field to decode.
318+
/// - Returns: A CGFloat value.
246319
@inline(__always)
247320
package mutating func cgFloatField(_ field: ProtobufDecoder.Field) throws -> CGFloat {
248321
try doubleField(field)
249322
}
250323

324+
/// Decodes a data buffer value field from the data.
325+
///
326+
/// - Parameter field: The field to decode.
327+
/// - Returns: A data buffer value.
251328
package mutating func dataBufferField(_ field: ProtobufDecoder.Field) throws -> UnsafeRawBufferPointer {
252329
switch field.wireType {
253330
case .lengthDelimited:
@@ -257,6 +334,10 @@ extension ProtobufDecoder {
257334
}
258335
}
259336

337+
/// Decodes a data value field from the data.
338+
///
339+
/// - Parameter field: The field to decode.
340+
/// - Returns: A data value.
260341
package mutating func dataField(_ field: ProtobufDecoder.Field) throws -> Data {
261342
switch field.wireType {
262343
case .lengthDelimited:
@@ -272,20 +353,34 @@ extension ProtobufDecoder {
272353
}
273354
}
274355

356+
/// Decodes a message value field from the data.
357+
///
358+
/// - Parameter field: The field to decode.
359+
/// - Returns: A ProtobufDecodableMessage value.
275360
package mutating func messageField<T>(_ field: ProtobufDecoder.Field) throws -> T where T: ProtobufDecodableMessage {
276361
guard field.wireType == .lengthDelimited else {
277362
throw DecodingError.failed
278363
}
279364
return try decodeMessage()
280365
}
281366

367+
/// Decodes a message value field from the data.
368+
///
369+
/// - Parameters:
370+
/// - field: The field to decode.
371+
/// - body: A closure that decodes the message.
372+
/// - Returns: A value decoded from the message.
282373
package mutating func messageField<T>(_ field: ProtobufDecoder.Field, _ body: (inout ProtobufDecoder) throws -> T) throws -> T {
283374
guard field.wireType == .lengthDelimited else {
284375
throw DecodingError.failed
285376
}
286377
return try decodeMessage(body)
287378
}
288379

380+
/// Decodes a string value field from the data.
381+
///
382+
/// - Parameter field: The field to decode.
383+
/// - Returns: A string value.
289384
package mutating func stringField(_ field: ProtobufDecoder.Field) throws -> String {
290385
let data = try dataField(field)
291386
guard let result = String(data: data, encoding: .utf8) else {
@@ -294,13 +389,18 @@ extension ProtobufDecoder {
294389
return result
295390
}
296391

392+
/// Decodes a codable value field from the data.
393+
///
394+
/// - Parameter field: The field to decode.
395+
/// - Returns: A codable value.
297396
package mutating func codableField<T>(_ field: ProtobufDecoder.Field) throws -> T where T: Decodable {
298397
let data = try dataField(field)
299398
return try value(fromBinaryPlist: data)
300399
}
301400
}
302401

303402
extension ProtobufDecoder {
403+
/// Decodes a variant from the data.
304404
private mutating func decodeVariant() throws -> UInt {
305405
var value: UInt = 0
306406
var shift: UInt = 0
@@ -318,6 +418,7 @@ extension ProtobufDecoder {
318418
return value
319419
}
320420

421+
/// Decodes a data buffer from the data.
321422
private mutating func decodeDataBuffer() throws -> UnsafeRawBufferPointer {
322423
let count = try Int(decodeVariant())
323424
let oldPtr = ptr
@@ -329,6 +430,7 @@ extension ProtobufDecoder {
329430
return UnsafeRawBufferPointer(start: oldPtr, count: count)
330431
}
331432

433+
/// Begins decoding a message.
332434
private mutating func beginMessage() throws {
333435
stack.append(end)
334436
let count = try Int(decodeVariant())
@@ -339,18 +441,34 @@ extension ProtobufDecoder {
339441
end = newPtr
340442
}
341443

444+
/// Decodes a message from the data.
445+
///
446+
/// - Parameters:
447+
/// - body: A closure that decodes the message.
448+
/// - Returns: The value decoded from the message.
342449
private mutating func decodeMessage<T>(_ body: (inout ProtobufDecoder) throws -> T) throws -> T {
343450
try beginMessage()
344451
defer { end = stack.removeLast() }
345452
return try body(&self)
346453
}
347454

455+
/// Decodes a message from the data.
456+
///
457+
/// - Returns: A ProtobufDecodableMessage value.
348458
private mutating func decodeMessage<T>() throws -> T where T: ProtobufDecodableMessage {
349459
try beginMessage()
350460
defer { end = stack.removeLast() }
351461
return try T(from: &self)
352462
}
353463

464+
/// Decodes a value from a binary plist.
465+
///
466+
/// NOTE: This is only available on non-WASI platforms.
467+
///
468+
/// - Parameters:
469+
/// - data: The plist data.
470+
/// - type: The type to decode.
471+
/// - Returns: The decodable value resulting from the plist data.
354472
func value<T>(fromBinaryPlist data: Data, type: T.Type = T.self) throws -> T where T: Decodable {
355473
#if os(WASI)
356474
fatalError("PropertyListDecoder is not avaiable on WASI")

0 commit comments

Comments
 (0)