-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Consuelita
committed
Jan 30, 2019
1 parent
455a279
commit 10fa152
Showing
3 changed files
with
137 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// NWEndpoint.Port+Codable.swift | ||
// Replicant | ||
// | ||
// Created by Mafalda on 1/29/19. | ||
// | ||
|
||
import Foundation | ||
import Network | ||
|
||
extension NWEndpoint.Port: Encodable | ||
{ | ||
public func encode(to encoder: Encoder) throws | ||
{ | ||
let portInt = self.rawValue | ||
var container = encoder.singleValueContainer() | ||
|
||
do | ||
{ | ||
try container.encode(portInt) | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
extension NWEndpoint.Port: Decodable | ||
{ | ||
public init(from decoder: Decoder) throws | ||
{ | ||
do | ||
{ | ||
let container = try decoder.singleValueContainer() | ||
|
||
do | ||
{ | ||
let portInt = try container.decode(UInt16.self) | ||
guard let port = NWEndpoint.Port(rawValue: portInt) | ||
else | ||
{ | ||
throw ReplicantError.invalidPort | ||
} | ||
|
||
self = port | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// NWEnpoint.Host+Codable.swift | ||
// Replicant | ||
// | ||
// Created by Mafalda on 1/29/19. | ||
// | ||
|
||
import Foundation | ||
import Network | ||
|
||
extension NWEndpoint.Host: Encodable | ||
{ | ||
public func encode(to encoder: Encoder) throws | ||
{ | ||
var container = encoder.singleValueContainer() | ||
|
||
switch self | ||
{ | ||
case .ipv4(let ipv4Address): | ||
do | ||
{ | ||
let addressString = "\(ipv4Address)" | ||
try container.encode(addressString) | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
case .ipv6(let ipv6Address): | ||
do | ||
{ | ||
let addressString = "\(ipv6Address)" | ||
try container.encode(addressString) | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
case .name(let nameString, _): | ||
do | ||
{ | ||
try container.encode(nameString) | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension NWEndpoint.Host: Decodable | ||
{ | ||
public init(from decoder: Decoder) throws | ||
{ | ||
do | ||
{ | ||
let container = try decoder.singleValueContainer() | ||
|
||
do | ||
{ | ||
let addressString = try container.decode(String.self) | ||
self.init(addressString) | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
catch let error | ||
{ | ||
throw error | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters