wireguard-apple/WireGuard/Shared/Model/DNSServer.swift

54 lines
1.3 KiB
Swift
Raw Normal View History

2018-10-24 01:37:28 +00:00
// SPDX-License-Identifier: MIT
// Copyright © 2018 WireGuard LLC. All Rights Reserved.
2018-10-23 10:53:27 +00:00
import Foundation
import Network
struct DNSServer {
let address: IPAddress
2018-12-21 04:52:45 +00:00
init(address: IPAddress) {
self.address = address
2018-10-23 10:53:27 +00:00
}
}
extension DNSServer: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
2018-12-21 04:52:45 +00:00
try container.encode(stringRepresentation)
2018-10-23 10:53:27 +00:00
}
2018-12-21 04:52:45 +00:00
2018-10-23 10:53:27 +00:00
public init(from decoder: Decoder) throws {
2018-12-21 04:52:45 +00:00
let values = try decoder.singleValueContainer()
let addressString = try values.decode(String.self)
if let address = IPv4Address(addressString) {
self.address = address
} else if let address = IPv6Address(addressString) {
self.address = address
} else {
2018-10-23 10:53:27 +00:00
throw DecodingError.invalidData
}
}
2018-12-21 04:52:45 +00:00
2018-10-23 10:53:27 +00:00
enum DecodingError: Error {
case invalidData
}
}
2018-12-21 04:52:45 +00:00
extension DNSServer {
var stringRepresentation: String {
return "\(address)"
}
init?(from addressString: String) {
if let addr = IPv4Address(addressString) {
address = addr
} else if let addr = IPv6Address(addressString) {
address = addr
} else {
return nil
}
}
}