Skip to content

Commit e67adb8

Browse files
committed
formatting
1 parent f14b45d commit e67adb8

File tree

6 files changed

+379
-308
lines changed

6 files changed

+379
-308
lines changed

Package.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,39 @@ let package = Package(
1919
name: "swift-multicodec",
2020
platforms: [
2121
.macOS(.v10_10),
22-
.iOS(.v10)
22+
.iOS(.v10),
2323
],
2424
products: [
2525
// Products define the executables and libraries produced by a package, and make them visible to other packages.
2626
.executable(
2727
name: "update-codecs",
28-
targets: ["Updater"]),
28+
targets: ["Updater"]
29+
),
2930
.library(
3031
name: "Multicodec",
31-
targets: ["Multicodec"]),
32+
targets: ["Multicodec"]
33+
),
3234
],
3335
dependencies: [
3436
// Dependencies declare other packages that this package depends on.
35-
// .package(url: /* package url */, from: "1.0.0"),
36-
//.package(name: "VarInt", path: "../VarInt") //Use this when attempting to run update-codecs (until our repo is public)
3737
.package(url: "https://github.com/swift-libp2p/swift-varint.git", from: "0.0.1")
3838
],
3939
targets: [
4040
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
4141
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
4242
.executableTarget(
4343
name: "Updater",
44-
dependencies: []),
44+
dependencies: []
45+
),
4546
.target(
4647
name: "Multicodec",
4748
dependencies: [
48-
.product(name: "VarInt", package: "swift-varint"),
49-
]),
49+
.product(name: "VarInt", package: "swift-varint")
50+
]
51+
),
5052
.testTarget(
5153
name: "MulticodecTests",
52-
dependencies: ["Multicodec"]),
54+
dependencies: ["Multicodec"]
55+
),
5356
]
5457
)

Sources/Multicodec/Codecs+Equatable.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414

1515
import Foundation
1616

17-
public extension Codecs {
18-
static func ==(lhs: Codecs, rhs: Codecs) -> Bool {
19-
return lhs.rawValue == rhs.rawValue
17+
extension Codecs {
18+
public static func == (lhs: Codecs, rhs: Codecs) -> Bool {
19+
lhs.rawValue == rhs.rawValue
2020
}
21-
22-
static func ==(lhs: Codecs, rhs: Int64) -> Bool {
23-
return lhs.rawValue == rhs
21+
22+
public static func == (lhs: Codecs, rhs: Int64) -> Bool {
23+
lhs.rawValue == rhs
2424
}
2525

26-
static func ==(lhs: Codecs, rhs: Int) -> Bool {
27-
return lhs.rawValue == Int64(rhs)
26+
public static func == (lhs: Codecs, rhs: Int) -> Bool {
27+
lhs.rawValue == Int64(rhs)
2828
}
2929

30-
static func ==(lhs: Codecs, rhs: String) -> Bool {
31-
return lhs.name == rhs
30+
public static func == (lhs: Codecs, rhs: String) -> Bool {
31+
lhs.name == rhs
3232
}
33-
34-
func isEqual(object: AnyObject?) -> Bool {
33+
34+
public func isEqual(object: AnyObject?) -> Bool {
3535
if let obj = object as? Codecs {
3636
return self.rawValue == obj.rawValue
3737
} else if let obj = object as? Int64 {
@@ -64,4 +64,3 @@ public extension Codecs {
6464
// return lhs.rawValue == rhs
6565
// }
6666
//}
67-

Sources/Multicodec/Codecs.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import Foundation
1919
import VarInt
2020

21+
// swift-format-ignore
2122
/// An Enum for navigating supported Multiformat Codecs and their properties, most importantly their Name and Integer Code
2223
///
2324
/// - Warning: Do not use the Codecs `rawValue` directly, instead make sure to use the `code` property to ensure backward compatibility with deprecated codecs.
@@ -568,7 +569,7 @@ public enum Codecs:UInt64, CaseIterable, Equatable {
568569
case es512 = 0xd01202
569570
case rs256 = 0xd01205
570571
case scion = 0xd02000
571-
572+
572573

573574
/// Allows instantiation of a Codec based on it's name
574575
/// ```
@@ -591,17 +592,17 @@ public enum Codecs:UInt64, CaseIterable, Equatable {
591592
self = s
592593
} else { throw MulticodecError.UnknownCodecId }
593594
}
594-
595+
595596
public init(_ code:Int) throws {
596597
guard let s = Codecs(rawValue: UInt64(code)) else { throw MulticodecError.UnknownCodecId }
597598
self = s
598599
}
599-
600+
600601
public init(_ code:Int64) throws {
601602
guard let s = Codecs(rawValue: UInt64(code)) else { throw MulticodecError.UnknownCodecId }
602603
self = s
603604
}
604-
605+
605606
public init(_ code:UInt64) throws {
606607
guard let s = Codecs(rawValue: code) else { throw MulticodecError.UnknownCodecId }
607608
self = s
@@ -615,18 +616,18 @@ public enum Codecs:UInt64, CaseIterable, Equatable {
615616

616617
/// Returns a list of all known Codec codes
617618
public static var codecCodes:[UInt64] { return Codecs.allCases.map { $0.rawValue } }
618-
619+
619620
/// Returns the code for this Codec
620621
public var code:UInt64 {
621622
return self.rawValue
622623
}
623-
624+
624625
/// Returns the name for this Codec
625626
public var name:String { return "\(self)".replacingOccurrences(of: "_", with: "-") }
626-
627+
627628
/// Returns the code for this Codec as a VarInt Byte Buffer
628629
public var asVarInt:[UInt8] { return putUVarInt(self.rawValue) }
629-
630+
630631
public var tag:String {
631632
switch self {
632633
case .identity:
@@ -1707,7 +1708,7 @@ public enum Codecs:UInt64, CaseIterable, Equatable {
17071708
return "varsig"
17081709
case .scion:
17091710
return "multiaddr"
1710-
1711+
17111712
}
17121713
}
17131714

Sources/Multicodec/Multicodec.swift

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
import Foundation
1919
import VarInt
2020

21-
enum MulticodecError : Error {
21+
enum MulticodecError: Error {
2222
case PrefixExtractionBufferTooSmall
2323
case PrefixExtractionValueOverflow
2424
case UnknownCodecString
2525
case UnknownCodecId
2626
}
2727

28-
2928
/// Extract the prefix value from a multicodec prefixed byte buffer
3029
///
3130
/// - Parameter bytes: a multicodec prefixed byte buffer
@@ -38,7 +37,7 @@ public func extractPrefix(bytes: [UInt8]) throws -> UInt64 {
3837
if bytesRead == 0 { throw MulticodecError.PrefixExtractionBufferTooSmall }
3938
throw MulticodecError.PrefixExtractionValueOverflow
4039
}
41-
40+
4241
return prefix
4342
}
4443

@@ -48,7 +47,7 @@ public func extractPrefix(bytes: [UInt8]) throws -> UInt64 {
4847
/// - Returns: the prefix value for the given multicodec as bytes
4948
/// - Throws: UnknownCodecString if the name was invalid
5049
public func getPrefix(multiCodec: String) throws -> [UInt8] {
51-
return putUVarInt(try Codecs(multiCodec).rawValue)
50+
putUVarInt(try Codecs(multiCodec).rawValue)
5251
}
5352

5453
/// Return the prefix value for a given multicodec string
@@ -57,10 +56,9 @@ public func getPrefix(multiCodec: String) throws -> [UInt8] {
5756
/// - Returns: the prefix value for the given multicodec as bytes
5857
/// - Throws: UnknownCodecString if the name was invalid
5958
public func getPrefix(multiCodec: Codecs) -> [UInt8] {
60-
return putUVarInt(multiCodec.rawValue)
59+
putUVarInt(multiCodec.rawValue)
6160
}
6261

63-
6462
/// Add multicodec prefix to the front of the given byte buffer
6563
///
6664
/// - Parameters:
@@ -69,7 +67,7 @@ public func getPrefix(multiCodec: Codecs) -> [UInt8] {
6967
/// - Returns: the prefixed byte buffer
7068
/// - Throws: UnknownCodecString if given an invalid multicodec name
7169
public func addPrefix(multiCodec: String, bytes: [UInt8]) throws -> [UInt8] {
72-
return try getPrefix(multiCodec: multiCodec) + bytes
70+
try getPrefix(multiCodec: multiCodec) + bytes
7371
}
7472

7573
/// Add multicodec prefix to the front of the given byte buffer
@@ -80,7 +78,7 @@ public func addPrefix(multiCodec: String, bytes: [UInt8]) throws -> [UInt8] {
8078
/// - Returns: the prefixed byte buffer
8179
/// - Throws: UnknownCodecString if given an invalid multicodec name
8280
public func addPrefix(code: Int64, bytes: [UInt8]) throws -> [UInt8] {
83-
return try getPrefix(multiCodec: try Codecs(code).name) + bytes
81+
try getPrefix(multiCodec: try Codecs(code).name) + bytes
8482
}
8583

8684
/// Add multicodec prefix to the front of the given byte buffer
@@ -91,7 +89,7 @@ public func addPrefix(code: Int64, bytes: [UInt8]) throws -> [UInt8] {
9189
/// - Returns: the prefixed byte buffer
9290
/// - Throws: UnknownCodecString if given an invalid multicodec name
9391
public func addPrefix(code: UInt64, bytes: [UInt8]) throws -> [UInt8] {
94-
return try getPrefix(multiCodec: try Codecs(code).name) + bytes
92+
try getPrefix(multiCodec: try Codecs(code).name) + bytes
9593
}
9694

9795
/// Add multicodec prefix to the front of the given byte buffer
@@ -101,8 +99,8 @@ public func addPrefix(code: UInt64, bytes: [UInt8]) throws -> [UInt8] {
10199
/// - bytes: the byte buffer to prefix
102100
/// - Returns: the prefixed byte buffer
103101
/// - Throws: UnknownCodecString if given an invalid multicodec name
104-
public func addPrefix(code: Int, bytes: [UInt8]) throws -> [UInt8] {
105-
return try getPrefix(multiCodec: try Codecs(code).name) + bytes
102+
public func addPrefix(code: Int, bytes: [UInt8]) throws -> [UInt8] {
103+
try getPrefix(multiCodec: try Codecs(code).name) + bytes
106104
}
107105

108106
/// Add multicodec prefix to the front of the given byte buffer
@@ -112,10 +110,9 @@ public func addPrefix(code: Int, bytes: [UInt8]) throws -> [UInt8] {
112110
/// - bytes: the byte buffer to prefix
113111
/// - Returns: the prefixed byte buffer
114112
public func addPrefix(codec: Codecs, bytes: [UInt8]) -> [UInt8] {
115-
return getPrefix(multiCodec: codec) + bytes
113+
getPrefix(multiCodec: codec) + bytes
116114
}
117115

118-
119116
/// Remove the prefix from a prefixed byte buffer
120117
///
121118
/// - Parameter bytes: the prefixed byte buffer
@@ -126,7 +123,6 @@ public func removePrefix(bytes: [UInt8]) throws -> [UInt8] {
126123
return Array(bytes[prefix.count...])
127124
}
128125

129-
130126
/// Get the codec name of the codec in the given byte buffer
131127
///
132128
/// - Parameter bytes: the multicodec prefixed byte buffer
@@ -141,27 +137,29 @@ public func getCodecEnum(bytes: [UInt8]) throws -> Codecs {
141137
return try Codecs(prefix)
142138
}
143139

144-
public extension String {
140+
extension String {
145141
/// Encodes a String into it's UTF8 Byte Array with the specified Multicodec prefix
146-
func encodeUTF8(as codec:Codecs) -> [UInt8] {
147-
return addPrefix(codec: codec, bytes: Array(self.utf8))
142+
public func encodeUTF8(as codec: Codecs) -> [UInt8] {
143+
addPrefix(codec: codec, bytes: Array(self.utf8))
148144
}
149145
}
150146

151-
public extension Array where Element == UInt8 {
152-
func multiCodec() throws -> (codec:Codecs, bytes:[UInt8]) {
147+
extension Array where Element == UInt8 {
148+
public func multiCodec() throws -> (codec: Codecs, bytes: [UInt8]) {
153149
let codec = try getCodecEnum(bytes: self)
154150
return (codec: codec, bytes: try removePrefix(bytes: self))
155151
}
156-
157-
func extractCodec() throws -> (codec:Codecs, bytes:[UInt8]) {
152+
153+
public func extractCodec() throws -> (codec: Codecs, bytes: [UInt8]) {
158154
let codec = try getCodecEnum(bytes: self)
159155
return (codec: codec, bytes: try removePrefix(bytes: self))
160156
}
161-
162-
func decodeMulticodec(using encoding:String.Encoding) throws -> (codec:Codecs, contents:String) {
157+
158+
public func decodeMulticodec(using encoding: String.Encoding) throws -> (codec: Codecs, contents: String) {
163159
let codec = try getCodecEnum(bytes: self)
164-
guard let str = String(bytes: try removePrefix(bytes: self), encoding: encoding) else { throw MulticodecError.UnknownCodecId } //Better Error
160+
guard let str = String(bytes: try removePrefix(bytes: self), encoding: encoding) else {
161+
throw MulticodecError.UnknownCodecId
162+
} //Better Error
165163
return (codec: codec, contents: str)
166164
}
167165
}

0 commit comments

Comments
 (0)