Skip to content

Commit

Permalink
swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
jollyjinx committed Jan 14, 2025
1 parent e55d09e commit 8976e43
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 43 deletions.
36 changes: 20 additions & 16 deletions Sources/UnifiLibrary/Helper/IPv4Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ public enum IPv4
{
public let prefixlength: UInt8

public var description : String { String(prefixlength) }
public var description: String { String(prefixlength) }
}

public struct Address : Sendable, Hashable, CustomStringConvertible
public struct Address: Sendable, Hashable, CustomStringConvertible
{
public let bits: UInt32

public var description: String { IPv4.addressIntToString(bits) }
}

public struct Network : Sendable, Hashable, CustomStringConvertible
public struct Network: Sendable, Hashable, CustomStringConvertible
{
public let address: Address
public let netmask: Netmask

public var gateway: Address { address }
public var network: Network { Network(address:Address(bits:address.bits & netmask.bits) , netmask:netmask) }
public var network: Network { Network(address: Address(bits: address.bits & netmask.bits), netmask: netmask) }
public var description: String { "\(address)/\(netmask)" }
}
}
Expand All @@ -35,7 +35,7 @@ public extension IPv4.Netmask
{
init(bitmask: UInt32)
{
self.prefixlength = UInt8(32 - bitmask.leadingZeroBitCount)
prefixlength = UInt8(32 - bitmask.leadingZeroBitCount)
}

init?(_ string: String)
Expand Down Expand Up @@ -70,9 +70,9 @@ public extension IPv4.Network
{
let components = cidrString.split(separator: "/")
guard components.count == 2 else { return nil }
guard let address = IPv4.Address( String(components[0]) ),
let netmask = IPv4.Netmask( String(components[1]) )

guard let address = IPv4.Address(String(components[0])),
let netmask = IPv4.Netmask(String(components[1]))
else { return nil }

self.address = address
Expand Down Expand Up @@ -107,38 +107,42 @@ public extension IPv4
}
}

extension IPv4.Network : Codable
extension IPv4.Network: Codable
{
public func encode(to encoder: any Encoder) throws
{
var container = encoder.singleValueContainer()

try container.encode( "\(address)/\(netmask)" )
try container.encode("\(address)/\(netmask)")
}

public init(from decoder: any Decoder) throws
{
let container = try decoder.singleValueContainer()
let cidr = try container.decode(String.self)
guard let network = IPv4.Network(cidr) else
guard let network = IPv4.Network(cidr)
else
{
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid network address \(cidr)")
}
self = network
}
}

extension IPv4.Address : Codable
extension IPv4.Address: Codable
{
public func encode(to encoder: any Encoder) throws {
public func encode(to encoder: any Encoder) throws
{
var container = encoder.singleValueContainer()
try container.encode( description )
try container.encode(description)
}

public init(from decoder: any Decoder) throws {
public init(from decoder: any Decoder) throws
{
let container = try decoder.singleValueContainer()
let value = try container.decode(String.self)
guard let address = IPv4.Address(value) else
guard let address = IPv4.Address(value)
else
{
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid address \(value)")
}
Expand Down
22 changes: 16 additions & 6 deletions Sources/UnifiLibrary/Helper/MACAddress.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
//
// MACAddress.swift
//

import Foundation
import RegexBuilder

public struct MACAddress: Hashable, Sendable
{
public let address: String

public enum InvalidMACAddressError: Error {
public enum InvalidMACAddressError: Error
{
case invalidFormat
}

public init(_ address: String) throws {
public init(_ address: String) throws
{
let regex = /^([:hexdigit:]{2}[:-]){5}([:hexdigit:]{2})$/

guard let match = try regex.firstMatch(in: address)
Expand All @@ -21,21 +27,25 @@ public struct MACAddress: Hashable, Sendable
}
}

extension MACAddress: CustomStringConvertible {
public var description: String {
extension MACAddress: CustomStringConvertible
{
public var description: String
{
return address
}
}

extension MACAddress: Codable
{
public init(from decoder: Decoder) throws {
public init(from decoder: Decoder) throws
{
let container = try decoder.singleValueContainer()
let address = try container.decode(String.self)
try self.init(address)
}

public func encode(to encoder: Encoder) throws {
public func encode(to encoder: Encoder) throws
{
var container = encoder.singleValueContainer()
try container.encode(address)
}
Expand Down
1 change: 0 additions & 1 deletion Sources/UnifiLibrary/OldAPI/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,3 @@ extension Device: Hashable, Equatable
return lhs.mac == rhs.mac
}
}

18 changes: 8 additions & 10 deletions Sources/UnifiLibrary/OldAPI/ReportedNetwork.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//
// ReportedNetwork.swift
// unifi2mqtt
//
// Created by Patrick Stein on 13.01.25.
//

import Foundation

public struct ReportedNetwork: Sendable, Hashable, Equatable
Expand All @@ -12,25 +10,26 @@ public struct ReportedNetwork: Sendable, Hashable, Equatable
public let address: IPv4.Network?
}

extension ReportedNetwork
public extension ReportedNetwork
{
public var gateway: IPv4.Address? { address?.gateway }
public var network: IPv4.Network? { address?.network }
var gateway: IPv4.Address? { address?.gateway }
var network: IPv4.Network? { address?.network }
}

extension ReportedNetwork : Codable
extension ReportedNetwork: Codable
{
enum DeCodingKeys: String, CodingKey
{
case name
case address
}

enum EncodingKeys: String, CodingKey
{
case name
case address
case gateway // own addition
case network // own addition
case gateway // own addition
case network // own addition
}

public init(from decoder: Decoder) throws
Expand All @@ -50,4 +49,3 @@ extension ReportedNetwork : Codable
try container.encode(network, forKey: EncodingKeys.network)
}
}

8 changes: 4 additions & 4 deletions Sources/UnifiLibrary/UnifiHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ extension UnifiHost
for client in retrievedClients
{
if let cacheEntry = clientCache[client.macAddress],
cacheEntry.entry.isEqual(to: client), // has not changed
cacheEntry.lastUpdate > staleDate // not stale
cacheEntry.entry.isEqual(to: client), // has not changed
cacheEntry.lastUpdate > staleDate // not stale
{
newCache[client.macAddress] = cacheEntry
continue
Expand Down Expand Up @@ -172,8 +172,8 @@ extension UnifiHost
for device in retrievedDevices
{
if let cacheEntry = deviceCache[device.macAddress],
cacheEntry.entry.isEqual(to: device), // has not changed
cacheEntry.lastUpdate > staleDate // not stale
cacheEntry.entry.isEqual(to: device), // has not changed
cacheEntry.lastUpdate > staleDate // not stale
{
newCache[device.macAddress] = cacheEntry
continue
Expand Down
3 changes: 1 addition & 2 deletions Sources/UnifiLibraryTests/OldDevicesDecodingTests..swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// UnifiDeviceDecodingTests.swift
// OldDevicesDecodingTests..swift
//

//
Expand Down Expand Up @@ -27,5 +27,4 @@ struct OldUnifiDeviceDecodingTests
let device = try decoder.decode(Device.self, from: data)
#expect(device.reported_networks?.count ?? 0 == 2)
}

}
8 changes: 4 additions & 4 deletions Sources/unifi2mqtt/unifi2mqtt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct unifi2mqtt: AsyncParsableCommand

@Option(name: .long, help: "Unifi hostname") var unifiHostname: String = "unifi"
@Option(name: .long, help: "Unifi port") var unifiPort: UInt16 = 8443
@Option(name: .long, help: "UniFi API key. This key can also be provided via the UNIFI_API_KEY environment variable") var unifiAPIKey: String = { ProcessInfo.processInfo.environment["UNIFI_API_KEY"] ?? "" }()
@Option(name: .long, help: "UniFi API key. This key can also be provided via the UNIFI_API_KEY environment variable") var unifiAPIKey: String = ProcessInfo.processInfo.environment["UNIFI_API_KEY"] ?? ""
@Option(name: .long, help: "Unifi site id") var unifiSiteId: String? = nil

#if DEBUG
Expand Down Expand Up @@ -71,7 +71,7 @@ struct unifi2mqtt: AsyncParsableCommand
{
case missingEnvironmentVariable(String)
}
guard !unifiAPIKey.isEmpty else { throw ValidationError("UniFi API Key not set.\n\n\(unifi2mqtt.helpMessage())") }
guard !unifiAPIKey.isEmpty else { throw ValidationError("UniFi API Key not set.\n\n\(unifi2mqtt.helpMessage())") }

let mqttPublisher = try await MQTTPublisher(hostname: mqttHostname, port: Int(mqttPort), username: mqttUsername, password: mqttPassword, emitInterval: minimumEmitInterval, baseTopic: basetopic, jsonOutput: jsonOutput)

Expand Down Expand Up @@ -120,7 +120,7 @@ struct unifi2mqtt: AsyncParsableCommand
{
let networks = unifiHost.networks

var hasPublishedNetwork : [ ReportedNetwork: Bool ] = [:]
var hasPublishedNetwork: [ReportedNetwork: Bool] = [:]

for client in clients
{
Expand All @@ -147,7 +147,7 @@ struct unifi2mqtt: AsyncParsableCommand
}
else if let ipAddress = client.ipAddress
{
if let reportedNetwork = networks.first(where: { $0.network?.contains(ipAddress) ?? false}),
if let reportedNetwork = networks.first(where: { $0.network?.contains(ipAddress) ?? false }),
let network = reportedNetwork.network
{
if !hasPublishedNetwork[reportedNetwork, default: false]
Expand Down

0 comments on commit 8976e43

Please sign in to comment.