Skip to content

Commit 0e06fbe

Browse files
committed
Updated for Swift 4
1 parent 764041f commit 0e06fbe

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

Sources/Decoder.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import Foundation
1010

11-
struct TLVDecoder {
11+
public struct TLVDecoder {
1212

13-
static func decode(data: Data, from types: [TLVDecodable.Type]) throws -> [TLVDecodable] {
13+
public static func decode(data: Data, from types: [TLVDecodable.Type]) throws -> [TLVDecodable] {
1414

1515
var offset = 0
1616

@@ -51,11 +51,11 @@ struct TLVDecoder {
5151

5252
// MARK: - Supporting Types
5353

54-
extension TLVDecoder {
54+
public extension TLVDecoder {
5555

5656
struct DecodingContext {
5757

58-
let offset: Int
58+
public let offset: Int
5959
}
6060

6161
enum DecodingError: Swift.Error {
@@ -70,7 +70,7 @@ extension TLVDecoder {
7070

7171
// MARK: - Coder Convenience Extensions
7272

73-
extension TLVDecoder {
73+
public extension TLVDecoder {
7474

7575
static func decode <Decodable: TLVDecodable> (data: Data, from type: Decodable.Type) throws -> Decodable {
7676

@@ -162,7 +162,7 @@ extension TLVDecoder {
162162
(data: Data, from types: (T1.Type, T2.Type, T3.Type, T4.Type, T5.Type, T6.Type)) throws -> (T1?, T2?, T3?, T4?, T5?, T6?) {
163163

164164
let decodables = try decode(data: data, from: [types.0, types.1, types.2, types.3, types.4, types.5])
165-
.sorted(by: { type(of: $0).typeCode.rawValue < type(of: $1).typeCode.rawValue })
165+
.sorted(by: { Swift.type(of: $0).typeCode.rawValue < type(of: $1).typeCode.rawValue })
166166

167167
return (decodables.count > 0 ? decodables[0] as? T1 : nil,
168168
decodables.count > 1 ? decodables[1] as? T2 : nil,

Sources/TLVCodable.swift

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,77 +78,77 @@ public extension TLVDecodable where Self: RawRepresentable, Self.RawValue == Str
7878
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue == String {
7979

8080
var valueData: Foundation.Data {
81-
81+
8282
guard let data = self.rawValue.data(using: .utf8)
8383
else { fatalError("Could not encode string") }
8484

8585
return data
8686
}
8787
}
88-
88+
8989
#elseif swift(>=3.0)
90+
91+
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: UnsignedInteger {
9092

91-
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: UnsignedInteger {
92-
93-
init?(valueData: Foundation.Data) {
94-
95-
typealias IntegerType = Self.RawValue.RawValue
96-
97-
assert(MemoryLayout<IntegerType>.size == 1, "Default implementation only for UInt8 enums")
98-
99-
guard valueData.count == 1
100-
else { return nil }
101-
102-
let valueByte = valueData[0]
103-
104-
guard let rawValue = RawValue.init(rawValue: valueByte as! IntegerType)
105-
else { return nil }
106-
107-
self.init(rawValue: rawValue)
108-
}
93+
public init?(valueData: Foundation.Data) {
94+
95+
typealias IntegerType = Self.RawValue.RawValue
96+
97+
assert(MemoryLayout<IntegerType>.size == 1, "Default implementation only for UInt8 enums")
98+
99+
guard valueData.count == 1
100+
else { return nil }
101+
102+
let valueByte = valueData[0]
103+
104+
guard let rawValue = RawValue.init(rawValue: valueByte as! IntegerType)
105+
else { return nil }
106+
107+
self.init(rawValue: rawValue)
109108
}
109+
}
110+
111+
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: UnsignedInteger {
110112

111-
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: RawRepresentable, Self.RawValue.RawValue: UnsignedInteger {
112-
113-
var valueData: Foundation.Data {
114-
115-
typealias IntegerType = Self.RawValue.RawValue
116-
117-
assert(MemoryLayout<IntegerType>.size == 1, "Default implementation only for UInt8 enums")
118-
119-
let byte = numericCast(rawValue.rawValue) as UInt8
120-
121-
return Data([byte])
122-
}
113+
public var valueData: Foundation.Data {
114+
115+
typealias IntegerType = Self.RawValue.RawValue
116+
117+
assert(MemoryLayout<IntegerType>.size == 1, "Default implementation only for UInt8 enums")
118+
119+
let byte = numericCast(rawValue.rawValue) as UInt8
120+
121+
return Data([byte])
123122
}
123+
}
124124

125-
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: ExpressibleByStringLiteral {
126-
127-
init?(valueData: Foundation.Data) {
128-
129-
typealias StringType = Self.RawValue
130-
131-
assert(Self.RawValue.self == String.self, "Default implementation only for String")
132-
133-
guard let string = String(data: valueData, encoding: .utf8)
134-
else { return nil }
135-
136-
self.init(rawValue: string as! StringType)
137-
}
125+
public extension TLVDecodable where Self: RawRepresentable, Self.RawValue: ExpressibleByStringLiteral {
126+
127+
public init?(valueData: Foundation.Data) {
128+
129+
typealias StringType = Self.RawValue
130+
131+
assert(Self.RawValue.self == String.self, "Default implementation only for String")
132+
133+
guard let string = String(data: valueData, encoding: .utf8)
134+
else { return nil }
135+
136+
self.init(rawValue: string as! StringType)
138137
}
138+
}
139+
140+
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: ExpressibleByStringLiteral {
139141

140-
public extension TLVEncodable where Self: RawRepresentable, Self.RawValue: ExpressibleByStringLiteral {
141-
142-
var valueData: Foundation.Data {
143-
144-
assert(Self.RawValue.self == String.self, "Default implementation only for String")
145-
146-
guard let data = (self.rawValue as! String).data(using: .utf8)
147-
else { fatalError("Could not encode string") }
148-
149-
return data
150-
}
142+
public var valueData: Foundation.Data {
143+
144+
assert(Self.RawValue.self == String.self, "Default implementation only for String")
145+
146+
guard let data = (self.rawValue as! String).data(using: .utf8)
147+
else { fatalError("Could not encode string") }
148+
149+
return data
151150
}
151+
}
152152

153153
#endif
154154

0 commit comments

Comments
 (0)