aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Sources/WireGuardKit/IPAddressRange.swift23
1 files changed, 23 insertions, 0 deletions
diff --git a/Sources/WireGuardKit/IPAddressRange.swift b/Sources/WireGuardKit/IPAddressRange.swift
index 28a95d8..20e0da9 100644
--- a/Sources/WireGuardKit/IPAddressRange.swift
+++ b/Sources/WireGuardKit/IPAddressRange.swift
@@ -27,6 +27,29 @@ extension IPAddressRange: Hashable {
}
}
+extension IPAddressRange: Codable {
+ public func encode(to encoder: Encoder) throws {
+ var container = encoder.singleValueContainer()
+
+ try container.encode(self.stringRepresentation)
+ }
+
+ public init(from decoder: Decoder) throws {
+ let container = try decoder.singleValueContainer()
+ let value = try container.decode(String.self)
+
+ if let ipAddressRange = IPAddressRange(from: value) {
+ self = ipAddressRange
+ } else {
+ let context = DecodingError.Context(
+ codingPath: container.codingPath,
+ debugDescription: "Invalid IPAddressRange representation"
+ )
+ throw DecodingError.dataCorrupted(context)
+ }
+ }
+}
+
extension IPAddressRange {
public var stringRepresentation: String {
return "\(address)/\(networkPrefixLength)"