Refactoring

This commit is contained in:
Ali
2021-08-03 12:23:13 +02:00
parent 1a0dc56953
commit 7e6eb2b3bd
46 changed files with 702 additions and 470 deletions

View File

@@ -1,6 +1,10 @@
import Foundation
public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
enum CodingKeys: String, CodingKey {
case internalValue = "iv"
}
public struct Namespace: Comparable, Hashable, Codable, CustomStringConvertible {
public static var max: Namespace {
return Namespace(rawValue: 0x7)
@@ -156,6 +160,17 @@ public struct PeerId: Hashable, CustomStringConvertible, Comparable, Codable {
self.id = Id(rawValue: Int32(bitPattern: UInt32(clamping: idLowBits)))
}
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let value = try container.decode(Int64.self, forKey: .internalValue)
self.init(value)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(self.toInt64(), forKey: .internalValue)
}
public func toInt64() -> Int64 {
let idLowBits = UInt32(bitPattern: self.id.rawValue)