Skip to content

Commit 0c633ee

Browse files
committed
Add String extensions and remove Scripts directory
- Added String.swift with String extension utilities - Updated code formatting and implementation - Removed Scripts/setup-rfc.sh - Updated Package.swift and gitignore
1 parent 8ffc4e8 commit 0c633ee

File tree

7 files changed

+45
-144
lines changed

7 files changed

+45
-144
lines changed

.gitignore

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
1-
# Swift Package Manager
2-
.build/
3-
.swiftpm/
4-
Package.resolved
1+
*~
52

6-
# Xcode
7-
*.xcodeproj/
8-
*.xcworkspace/
9-
xcuserdata/
3+
Package.resolved
104
DerivedData/
11-
*.hmap
12-
*.ipa
13-
*.dSYM.zip
14-
*.dSYM
15-
16-
# macOS
17-
.DS_Store
18-
19-
# AI tools
20-
CLAUDE.md
21-
.claude
22-
.cursor/
23-
.aider*
5+
Thumbs.db
246

25-
# IDE
26-
.vscode/
27-
.idea/
7+
# Dot files/directories (opt-in only)
8+
/.*
9+
!/.github
10+
!/.gitignore
11+
!/.spi.yml
12+
!/.swift-format
13+
!/.swiftformat
14+
!/.swiftlint.yml
2815

29-
# Temporary files
30-
*.tmp
31-
*.swp
32-
*~
16+
# Documentation (opt-in for top-level .md files only)
17+
/*.md
18+
!README.md
19+
!LICENSE.md
20+
!CHANGELOG.md
21+
!CONTRIBUTING.md
22+
!CODE_OF_CONDUCT.md
23+
!SECURITY.md

Package.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// swift-tools-version:6.0
22

3-
import Foundation
43
import PackageDescription
54

65
extension String {
@@ -15,8 +14,10 @@ extension Target.Dependency {
1514
let package = Package(
1615
name: "swift-rfc-1123",
1716
platforms: [
18-
.macOS(.v13),
19-
.iOS(.v16)
17+
.macOS(.v15),
18+
.iOS(.v18),
19+
.tvOS(.v18),
20+
.watchOS(.v11)
2021
],
2122
products: [
2223
.library(name: .rfc1123, targets: [.rfc1123]),
@@ -41,4 +42,4 @@ let package = Package(
4142
swiftLanguageModes: [.v6]
4243
)
4344

44-
extension String { var tests: Self { self + " Tests" } }
45+
extension String { var tests: Self { self + " Tests" } }

Scripts/setup-rfc.sh

Lines changed: 0 additions & 62 deletions
This file was deleted.

Sources/RFC_1123/RFC 1123.swift

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Coen ten Thije Boonkkamp on 28/12/2024.
66
//
77

8-
import Foundation
98
import RFC_1035
109

1110
/// RFC 1123 compliant host name
@@ -65,7 +64,7 @@ extension Domain {
6564
extension Domain {
6665
/// A type-safe host label that enforces RFC 1123 rules
6766
public struct Label: Hashable, Sendable {
68-
private let value: String
67+
public let value: String
6968

7069
/// Initialize a label, validating RFC 1123 rules
7170
internal init(_ string: String, validateAs type: ValidationType) throws {
@@ -84,8 +83,6 @@ extension Domain {
8483

8584
self.value = string
8685
}
87-
88-
public var stringValue: String { value }
8986
}
9087
}
9188

@@ -114,7 +111,7 @@ extension Domain {
114111
extension Domain {
115112
/// The complete host name as a string
116113
public var name: String {
117-
labels.map(\.stringValue).joined(separator: ".")
114+
labels.map(String.init).joined(separator: ".")
118115
}
119116

120117
/// The top-level domain (rightmost label)
@@ -135,7 +132,7 @@ extension Domain {
135132

136133
/// Creates a subdomain by prepending new labels
137134
public func addingSubdomain(_ components: [String]) throws -> Domain {
138-
try Domain(labels: components + labels.map(\.stringValue))
135+
try Domain(labels: components + labels.map(String.init))
139136
}
140137

141138
public func addingSubdomain(_ components: String...) throws -> Domain {
@@ -145,41 +142,24 @@ extension Domain {
145142
/// Returns the parent domain by removing the leftmost label
146143
public func parent() throws -> Domain? {
147144
guard labels.count > 1 else { return nil }
148-
return try Domain(labels: labels.dropFirst().map(\.stringValue))
145+
return try Domain(labels: labels.dropFirst().map(String.init))
149146
}
150147

151148
/// Returns the root domain (tld + sld)
152149
public func root() throws -> Domain? {
153150
guard labels.count >= 2 else { return nil }
154-
return try Domain(labels: labels.suffix(2).map(\.stringValue))
151+
return try Domain(labels: labels.suffix(2).map(String.init))
155152
}
156153
}
157154

158155
// MARK: - Errors
159156
extension Domain {
160-
public enum ValidationError: Error, LocalizedError, Equatable {
157+
public enum ValidationError: Error, Equatable {
161158
case empty
162159
case tooLong(_ length: Int)
163160
case tooManyLabels
164161
case invalidLabel(_ label: String)
165162
case invalidTLD(_ tld: String)
166-
167-
public var errorDescription: String? {
168-
switch self {
169-
case .empty:
170-
return "Host name cannot be empty"
171-
case .tooLong(let length):
172-
return "Host name length \(length) exceeds maximum of \(Limits.maxLength)"
173-
case .tooManyLabels:
174-
return "Host name has too many labels (maximum \(Limits.maxLabels))"
175-
case .invalidLabel(let label):
176-
return
177-
"Invalid label '\(label)'. Must start and end with letter/digit, and contain only letters/digits/hyphens"
178-
case .invalidTLD(let tld):
179-
return
180-
"Invalid TLD '\(tld)'. Must start and end with letter, and contain only letters/digits/hyphens"
181-
}
182-
}
183163
}
184164
}
185165

Sources/RFC_1123/String.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// File.swift
3+
// swift-rfc-1123
4+
//
5+
// Created by Coen ten Thije Boonkkamp on 19/11/2025.
6+
//
7+
8+
extension String {
9+
public init(
10+
_ label: RFC_1123.Domain.Label
11+
) {
12+
self = label.value
13+
}
14+
}

Tests/RFC_1123 Tests/RFC 1123 Tests.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Created by Coen ten Thije Boonkkamp on 28/12/2024.
66
//
77

8-
import Foundation
98
import RFC_1123
109
import Testing
1110

@@ -108,12 +107,4 @@ struct RFC1123Tests {
108107
let host = try Domain.subdomain("com", "example", "host")
109108
#expect(host.name == "host.example.com")
110109
}
111-
112-
@Test("Successfully encodes and decodes")
113-
func testCodable() throws {
114-
let original = try Domain("example.com")
115-
let encoded = try JSONEncoder().encode(original)
116-
let decoded = try JSONDecoder().decode(Domain.self, from: encoded)
117-
#expect(original == decoded)
118-
}
119110
}

Tests/RFC_1123 Tests/ReadmeVerificationTests.swift

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Verifies that README code examples actually work
66
//
77

8-
import Foundation
98
import RFC_1035
109
import RFC_1123
1110
import Testing
@@ -81,7 +80,7 @@ struct ReadmeVerificationTests {
8180
#expect(rfc1123Domain.name == "example.com")
8281

8382
// Convert RFC 1123 domain to RFC 1035
84-
let backToRFC1035 = try rfc1123Domain.toRFC1035()
83+
let backToRFC1035 = try RFC_1035.Domain(rfc1123Domain)
8584

8685
#expect(backToRFC1035.name == "example.com")
8786
}
@@ -98,17 +97,4 @@ struct ReadmeVerificationTests {
9897
_ = try RFC_1123.Domain("example.123com")
9998
}
10099
}
101-
102-
@Test("README Line 184-190: Codable support")
103-
func codableSupport() throws {
104-
let host = try RFC_1123.Domain("example.com")
105-
106-
// Encode to JSON
107-
let encoded = try JSONEncoder().encode(host)
108-
109-
// Decode from JSON
110-
let decoded = try JSONDecoder().decode(RFC_1123.Domain.self, from: encoded)
111-
112-
#expect(host == decoded)
113-
}
114100
}

0 commit comments

Comments
 (0)