Skip to content

Commit

Permalink
Codable and Server Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Consuelita committed Jan 30, 2019
1 parent 455a279 commit 10fa152
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 95 deletions.
59 changes: 59 additions & 0 deletions Sources/Replicant/NWEndpoint.Port+Codable.swift
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
}
}
}

75 changes: 75 additions & 0 deletions Sources/Replicant/NWEnpoint.Host+Codable.swift
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
}
}
}
98 changes: 3 additions & 95 deletions Sources/Replicant/ServerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import Network

public struct ServerConfig: Codable
{
public let ipString: String?
public let host: NWEndpoint.Host?
public let port: NWEndpoint.Port

public init(withPort port: NWEndpoint.Port, andIP ipAddressString: String? = nil)
public init(withPort port: NWEndpoint.Port, andHost host: NWEndpoint.Host?)
{
self.port = port
self.ipString = ipAddressString
self.host = host
}

/// Creates and returns a JSON representation of the ServerConfig struct.
Expand Down Expand Up @@ -73,95 +73,3 @@ extension String
return NWEndpoint.Host(self)
}
}

extension NWEndpoint.Host: Encodable
{
public func encode(to encoder: Encoder) throws
{
var container = encoder.singleValueContainer()
let hostString = "\(self)"

do
{
try container.encode(hostString)
}
catch let error
{
throw error
}
}
}

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.Host: Decodable
{
public init(from decoder: Decoder) throws
{
do
{
let container = try decoder.singleValueContainer()

do
{
let hostString = try container.decode(String.self)
let host = NWEndpoint.Host(hostString)
self = host
}
catch let error
{
throw error
}
}
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
}
}
}

0 comments on commit 10fa152

Please sign in to comment.