Skip to content

Commit 200eac1

Browse files
authored
Add public inits to record types (#23)
Motivation: The record types are public but don't have public inits, this makes it impossible for users to implement the `DNSResolver` protocol in a useful way. The record types are also `Equatable` but not `Hashable`. There's no reason for them not to be `Hashable`. Modifications: - Add `public` `init`s to the record types - Make the record types `Hashable` Result: Possible to implement a `DNSResolver`
1 parent 4d83a2c commit 200eac1

File tree

1 file changed

+53
-10
lines changed

1 file changed

+53
-10
lines changed

Sources/AsyncDNSResolver/AsyncDNSResolver.swift

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ enum QueryType {
184184

185185
// MARK: - Query reply types
186186

187-
public enum IPAddress: Sendable, Equatable, CustomStringConvertible {
187+
public enum IPAddress: Sendable, Hashable, CustomStringConvertible {
188188
case ipv4(IPv4)
189189
case ipv6(IPv6)
190190

@@ -216,33 +216,47 @@ public enum IPAddress: Sendable, Equatable, CustomStringConvertible {
216216
}
217217
}
218218

219-
public struct ARecord: Sendable, Equatable, CustomStringConvertible {
219+
public struct ARecord: Sendable, Hashable, CustomStringConvertible {
220220
public let address: IPAddress.IPv4
221221
public let ttl: Int32?
222222

223223
public var description: String {
224224
"\(Self.self)(address=\(self.address), ttl=\(self.ttl.map { "\($0)" } ?? ""))"
225225
}
226+
227+
public init(address: IPAddress.IPv4, ttl: Int32?) {
228+
self.address = address
229+
self.ttl = ttl
230+
}
226231
}
227232

228-
public struct AAAARecord: Sendable, Equatable, CustomStringConvertible {
233+
public struct AAAARecord: Sendable, Hashable, CustomStringConvertible {
229234
public let address: IPAddress.IPv6
230235
public let ttl: Int32?
231236

232237
public var description: String {
233238
"\(Self.self)(address=\(self.address), ttl=\(self.ttl.map { "\($0)" } ?? ""))"
234239
}
240+
241+
public init(address: IPAddress.IPv6, ttl: Int32?) {
242+
self.address = address
243+
self.ttl = ttl
244+
}
235245
}
236246

237-
public struct NSRecord: Sendable, Equatable, CustomStringConvertible {
247+
public struct NSRecord: Sendable, Hashable, CustomStringConvertible {
238248
public let nameservers: [String]
239249

240250
public var description: String {
241251
"\(Self.self)(nameservers=\(self.nameservers))"
242252
}
253+
254+
public init(nameservers: [String]) {
255+
self.nameservers = nameservers
256+
}
243257
}
244258

245-
public struct SOARecord: Sendable, Equatable, CustomStringConvertible {
259+
public struct SOARecord: Sendable, Hashable, CustomStringConvertible {
246260
public let mname: String?
247261
public let rname: String?
248262
public let serial: UInt32
@@ -256,32 +270,45 @@ public struct SOARecord: Sendable, Equatable, CustomStringConvertible {
256270
}
257271
}
258272

259-
public struct PTRRecord: Sendable, Equatable, CustomStringConvertible {
273+
public struct PTRRecord: Sendable, Hashable, CustomStringConvertible {
260274
public let names: [String]
261275

262276
public var description: String {
263277
"\(Self.self)(names=\(self.names))"
264278
}
279+
280+
public init(names: [String]) {
281+
self.names = names
282+
}
265283
}
266284

267-
public struct MXRecord: Sendable, Equatable, CustomStringConvertible {
285+
public struct MXRecord: Sendable, Hashable, CustomStringConvertible {
268286
public let host: String
269287
public let priority: UInt16
270288

271289
public var description: String {
272290
"\(Self.self)(host=\(self.host), priority=\(self.priority))"
273291
}
292+
293+
public init(host: String, priority: UInt16) {
294+
self.host = host
295+
self.priority = priority
296+
}
274297
}
275298

276-
public struct TXTRecord: Sendable, Equatable {
299+
public struct TXTRecord: Sendable, Hashable {
277300
public let txt: String
278301

279302
public var description: String {
280303
"\(Self.self)(\(self.txt))"
281304
}
305+
306+
public init(txt: String) {
307+
self.txt = txt
308+
}
282309
}
283310

284-
public struct SRVRecord: Sendable, Equatable, CustomStringConvertible {
311+
public struct SRVRecord: Sendable, Hashable, CustomStringConvertible {
285312
public let host: String
286313
public let port: UInt16
287314
public let weight: UInt16
@@ -290,9 +317,16 @@ public struct SRVRecord: Sendable, Equatable, CustomStringConvertible {
290317
public var description: String {
291318
"\(Self.self)(host=\(self.host), port=\(self.port), weight=\(self.weight), priority=\(self.priority))"
292319
}
320+
321+
public init(host: String, port: UInt16, weight: UInt16, priority: UInt16) {
322+
self.host = host
323+
self.port = port
324+
self.weight = weight
325+
self.priority = priority
326+
}
293327
}
294328

295-
public struct NAPTRRecord: Sendable, Equatable, CustomStringConvertible {
329+
public struct NAPTRRecord: Sendable, Hashable, CustomStringConvertible {
296330
public let flags: String?
297331
public let service: String?
298332
public let regExp: String?
@@ -303,4 +337,13 @@ public struct NAPTRRecord: Sendable, Equatable, CustomStringConvertible {
303337
public var description: String {
304338
"\(Self.self)(flags=\(self.flags ?? ""), service=\(self.service ?? ""), regExp=\(self.regExp ?? ""), replacement=\(self.replacement), order=\(self.order), preference=\(self.preference))"
305339
}
340+
341+
public init(flags: String?, service: String?, regExp: String?, replacement: String, order: UInt16, preference: UInt16) {
342+
self.flags = flags
343+
self.service = service
344+
self.regExp = regExp
345+
self.replacement = replacement
346+
self.order = order
347+
self.preference = preference
348+
}
306349
}

0 commit comments

Comments
 (0)