Skip to content

Commit 09315a6

Browse files
authored
Merge pull request #5 from alta/ydnar/swift-format
chore: switch to Apple swift-format
2 parents 6dcdffb + d345206 commit 09315a6

14 files changed

+129
-74
lines changed

.github/workflows/swift.yaml

+7-28
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,30 @@ on:
55
branches:
66
- main
77
paths:
8-
- ".github/workflows/swift.yaml"
98
- "**.swift"
10-
- "**/Package.resolved"
9+
- "Package.resolved"
1110
pull_request:
1211
paths:
13-
- ".github/workflows/swift.yaml"
1412
- "**.swift"
15-
- "**/Package.resolved"
13+
- "Package.resolved"
1614

1715
jobs:
1816
format:
19-
name: Check SwiftFormat
17+
name: Format
2018
runs-on: ubuntu-latest
21-
timeout-minutes: 10
2219
steps:
2320
- name: Checkout repo
24-
uses: actions/checkout@v3
21+
uses: actions/checkout@v4
2522
with:
2623
submodules: recursive
2724

28-
- name: Install SwiftFormat
25+
- name: Install swift-format
2926
uses: Cyberbeni/install-swift-tool@v2
3027
with:
31-
url: https://github.com/nicklockwood/SwiftFormat
28+
url: https://github.com/apple/swift-format
3229

3330
- name: Format Swift code
34-
run: swiftformat --verbose .
31+
run: swift-format -r -i ./
3532

3633
- name: Verify formatted code is unchanged
3734
run: git diff --exit-code HEAD -w -G'(^[^# /])|(^#\w)|(^\s+[^#/])' # Ignore whitespace and comments
38-
39-
test:
40-
name: Test Swift packages
41-
runs-on: ${{ matrix.os }}
42-
timeout-minutes: 10
43-
strategy:
44-
matrix:
45-
os:
46-
- macos-latest
47-
- ubuntu-latest
48-
steps:
49-
- name: Checkout repo
50-
uses: actions/checkout@v3
51-
with:
52-
submodules: recursive
53-
54-
- name: Run tests
55-
run: swift test --enable-test-discovery

.github/workflows/test.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths:
8+
- ".github/workflows/test.yaml"
9+
- ".gitmodules"
10+
- "**.swift"
11+
- "Package.resolved"
12+
pull_request:
13+
paths:
14+
- ".github/workflows/test.yaml"
15+
- ".gitmodules"
16+
- "**.swift"
17+
- "Package.resolved"
18+
19+
jobs:
20+
test:
21+
name: Test
22+
runs-on: macos-latest
23+
timeout-minutes: 10
24+
steps:
25+
- name: Checkout repo
26+
uses: actions/checkout@v4
27+
with:
28+
submodules: recursive
29+
30+
- name: Test
31+
run: swift test
32+
33+
- name: Generate release build
34+
run: swift build -c release

.swift-format

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": 1,
3+
"lineLength": 2048,
4+
"indentation": {
5+
"tabs": 1
6+
},
7+
"tabWidth": 4,
8+
"maximumBlankLines": 1,
9+
"respectsExistingLineBreaks": true,
10+
"lineBreakBeforeControlFlowKeywords": true,
11+
"lineBreakBeforeEachArgument": false,
12+
"prioritizeKeepingFunctionOutputTogether": true
13+
}

.swiftformat

-6
This file was deleted.

.vscode/extensions.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
{
2+
// List of extensions which should be recommended for users of this workspace.
3+
// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
4+
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
25
"recommendations": [
6+
"davidanson.vscode-markdownlint",
37
"eamodio.gitlens",
48
"eriklynd.json-tools",
59
"foxundermoon.shell-format",
10+
"redhat.vscode-yaml",
611
"stkb.rewrap",
7-
"vknabel.vscode-swiftformat",
12+
"vknabel.vscode-apple-swift-format",
813
"xaver.clang-format",
9-
]
14+
],
15+
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
16+
"unwantedRecommendations": []
1017
}

.vscode/settings.json

+36-5
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
{
22
// Editor
33
"editor.codeActionsOnSave": {
4-
"source.fixAll.eslint": true
4+
"source.fixAll.eslint": "explicit"
55
},
66
"editor.detectIndentation": false,
77
"editor.formatOnSave": true,
88
"editor.insertSpaces": false,
9-
"editor.tabSize": 8,
9+
"editor.tabSize": 4,
1010
"files.eol": "\n",
1111
//
1212
// Files
1313
"files.exclude": {
1414
"**/.build": true,
1515
"**/.git": true,
16+
// "Sources/Copus": true,
1617
},
18+
"files.associations": {},
1719
"files.insertFinalNewline": true,
1820
"files.watcherExclude": {
1921
"**/.git/objects/**": true,
2022
"**/.git/subtree-cache/**": true,
2123
},
2224
"files.trimTrailingWhitespace": true,
2325
//
26+
// HTML
27+
"html.format.contentUnformatted": "pre,code,textarea,script",
28+
"html.format.unformatted": "pre,code,textarea,script,wbr",
29+
//
30+
// JSON
31+
"[json]": {
32+
"editor.defaultFormatter": "vscode.json-language-features",
33+
},
34+
"[jsonc]": {
35+
"editor.defaultFormatter": "vscode.json-language-features",
36+
},
37+
//
38+
// Make
39+
"[makefile]": {
40+
"editor.tabSize": 4
41+
},
42+
//
2443
// Markdown
2544
"[markdown]": {
2645
"editor.rulers": [
2746
80
2847
],
2948
"editor.codeActionsOnSave": {
30-
"source.fixAll.markdownlint": true
49+
"source.fixAll.markdownlint": "explicit"
3150
}
3251
},
3352
"markdown.preview.breaks": true,
@@ -41,7 +60,7 @@
4160
// Property Lists
4261
"[plist]": {
4362
"editor.formatOnSave": true,
44-
"editor.tabSize": 8,
63+
"editor.tabSize": 4,
4564
},
4665
//
4766
// Shell
@@ -51,8 +70,9 @@
5170
//
5271
// Swift
5372
"[swift]": {
73+
"editor.detectIndentation": true,
5474
"editor.insertSpaces": false,
55-
"editor.tabSize": 8,
75+
"editor.tabSize": 4,
5676
},
5777
//
5878
// YAML
@@ -67,4 +87,15 @@
6787
"strings": true
6888
}
6989
},
90+
"[github-actions-workflow]": {
91+
"editor.autoIndent": "keep",
92+
"editor.detectIndentation": true,
93+
"editor.insertSpaces": true,
94+
"editor.tabSize": 2,
95+
"editor.quickSuggestions": {
96+
"other": true,
97+
"comments": false,
98+
"strings": true
99+
}
100+
}
70101
}

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
.library(
1212
name: "RTP",
1313
targets: ["RTP"]
14-
),
14+
)
1515
],
1616
dependencies: [],
1717
targets: [

Sources/RTP/Connection.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@
5050
if let error = error {
5151
print("Error receiving UDP packet: \(error)")
5252
return
53-
} else if let data = data {
53+
}
54+
else if let data = data {
5455
// print("⬇️ Received UDP packet of size: \(data.count)")
5556

5657
do {
5758
let packet = try Packet(from: data)
5859
// print("🅿️ Parsed RTP packet: \(packet)")
5960
self?.receiverBlock(packet)
60-
} catch {
61+
}
62+
catch {
6163
print("Error handling RTP: \(error)")
6264
return
6365
}

Sources/RTP/Data+Extensions.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import Foundation
22

3-
internal extension Data {
3+
extension Data {
44
// big returns a big-endian integer of type T extracted from the bytes at the specified offset.
55
func big<T: FixedWidthInteger>(at offset: Int) -> T {
66
var value: T = 0
77
withUnsafeMutablePointer(to: &value) {
8-
self.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1), from: offset ..< offset + MemoryLayout<T>.size)
8+
self.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1), from: offset..<offset + MemoryLayout<T>.size)
99
}
1010
return T(bigEndian: value)
1111
}
@@ -14,7 +14,7 @@ internal extension Data {
1414
func little<T: FixedWidthInteger>(at offset: Int) -> T {
1515
var value: T = 0
1616
withUnsafeMutablePointer(to: &value) {
17-
self.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1), from: offset ..< offset + MemoryLayout<T>.size)
17+
self.copyBytes(to: UnsafeMutableBufferPointer(start: $0, count: 1), from: offset..<offset + MemoryLayout<T>.size)
1818
}
1919
return T(littleEndian: value)
2020
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import Foundation
22

3-
public extension FixedWidthInteger {
3+
extension FixedWidthInteger {
44
// random is a convenience function to generate a random value of the concrete type in [min,max]
5-
static func random() -> Self {
5+
public static func random() -> Self {
66
Self.random(in: .min ... .max)
77
}
88
}

Sources/RTP/Packet.swift

+9-14
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public struct Packet {
3030
public let padding: UInt8
3131

3232
var payloadWithoutPadding: Data {
33-
payload[0 ..< payload.count - Int(padding)]
33+
payload[0..<payload.count - Int(padding)]
3434
}
3535

3636
var encodedSize: Int {
@@ -81,9 +81,7 @@ public struct Packet {
8181
}
8282
let hasPadding = (data[0] & Self.paddingMask) != 0
8383
let hasExtension = (data[0] & Self.extensionMask) != 0
84-
let sizeWithPaddingAndExtension = Self.headerSize +
85-
(hasPadding ? 1 : 0) +
86-
(hasExtension ? Extension.headerSize : 0)
84+
let sizeWithPaddingAndExtension = Self.headerSize + (hasPadding ? 1 : 0) + (hasExtension ? Extension.headerSize : 0)
8785

8886
// Parse second octet
8987
marker = (data[Self.markerOffset] & Self.markerMask) != 0
@@ -106,10 +104,11 @@ public struct Packet {
106104

107105
// Parse optional CSRCs in octets 13+
108106
if csrcCount > 0 {
109-
csrcs = (0 ..< csrcCount).map {
107+
csrcs = (0..<csrcCount).map {
110108
data.big(at: Self.csrcOffset + $0)
111109
}
112-
} else {
110+
}
111+
else {
113112
csrcs = nil
114113
}
115114

@@ -130,14 +129,10 @@ public struct Packet {
130129
var data = Data(capacity: encodedSize)
131130

132131
// Encode first octect (version, padding, extension)
133-
data.append(contentsOf: [(Self.version << 6 & Self.versionMask) |
134-
(padding > 0 ? Self.paddingMask : 0) |
135-
(`extension` != nil ? Self.extensionMask : 0) |
136-
(UInt8(csrcs?.count ?? 0) & Self.csrcCountMask)])
132+
data.append(contentsOf: [(Self.version << 6 & Self.versionMask) | (padding > 0 ? Self.paddingMask : 0) | (`extension` != nil ? Self.extensionMask : 0) | (UInt8(csrcs?.count ?? 0) & Self.csrcCountMask)])
137133

138134
// Encode second octet
139-
data.append(contentsOf: [(marker ? Self.markerMask : 0) |
140-
(payloadType.rawValue & Self.payloadTypeMask)])
135+
data.append(contentsOf: [(marker ? Self.markerMask : 0) | (payloadType.rawValue & Self.payloadTypeMask)])
141136

142137
// Encode sequence number
143138
data.append(contentsOf: [UInt8(sequenceNumber >> 8 & 0xFF), UInt8(sequenceNumber & 0xFF)])
@@ -150,7 +145,7 @@ public struct Packet {
150145

151146
// Encode CSRCs
152147
if let csrcs = csrcs {
153-
for i in 0 ..< csrcs.count {
148+
for i in 0..<csrcs.count {
154149
data.append(contentsOf: [UInt8(csrcs[i] >> 24 & 0xFF), UInt8(csrcs[i] >> 16 & 0xFF), UInt8(csrcs[i] >> 8 & 0xFF), UInt8(csrcs[i] & 0xFF)])
155150
}
156151
}
@@ -203,7 +198,7 @@ public struct Extension {
203198
throw EncodingError.extensionDataTooSmall(size)
204199
}
205200

206-
payload = data[Self.headerSize ..< size]
201+
payload = data[Self.headerSize..<size]
207202
}
208203
}
209204

Tests/RTPTests/ConnectionTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
XCTAssertEqual(conn.conn.state, .cancelled)
1818
conn.conn.restart()
1919
sleep(1)
20-
XCTAssertEqual(conn.conn.state, .cancelled) // NWConnection instances cannot be restarted once cancelled
20+
XCTAssertEqual(conn.conn.state, .cancelled) // NWConnection instances cannot be restarted once cancelled
2121
}
2222
}
2323

Tests/RTPTests/PacketTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import XCTest
55
class PacketTests: XCTestCase {
66
func testInitWithData() throws {
77
let bytes: [UInt8] = [
8-
2 << 6, // Version 2, no marker or extension
9-
111, // Opus payload type
10-
0, 123, // Sequence number 123
11-
0, 0, 0, 1, // Timestamp 1
12-
0, 255, 255, 255, // SSRC 0x00FFFFFF
8+
2 << 6, // Version 2, no marker or extension
9+
111, // Opus payload type
10+
0, 123, // Sequence number 123
11+
0, 0, 0, 1, // Timestamp 1
12+
0, 255, 255, 255, // SSRC 0x00FFFFFF
1313
]
1414

1515
let packet = try RTP.Packet(from: Data(bytes))

0 commit comments

Comments
 (0)