Ver Fonte

Split BytesRepresentable into CustomBytesConvertible and LosslessBytesConvertible

maxep há 5 anos atrás
pai
commit
5160b05f06

+ 34 - 29
Sources/Binary/BytesRepresentable.swift → Sources/Binary/Convertible.swift

@@ -1,4 +1,4 @@
-// BytesRepresentable.swift
+// Convertible.swift
 // This file is part of KeePass.swift
 //
 // Copyright © 2021 Maxime Epain. All rights reserved.
@@ -18,18 +18,23 @@
 
 import Foundation
 
-// MARK: - Bytes Representatble Protocol
+/// A type with a customized bytes representation.
+public protocol CustomBytesConvertible {
 
-public protocol BytesRepresentable {
+    /// A bytes representation of this instance.
+    var bytes: Bytes { get }
+}
 
-    init(_ bytes: Bytes) throws
+public protocol LosslessBytesConvertible: CustomBytesConvertible {
 
-    var bytes: Bytes { get }
+    /// Instantiates an instance of the conforming type from bytes
+    /// representation.
+    init(_ bytes: Bytes) throws
 }
 
 // MARK: - Streamable Boolean Bytes
 
-extension Bool: BytesRepresentable {
+extension Bool: LosslessBytesConvertible {
 
     public init(_ bytes: Bytes) throws {
         guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
@@ -43,7 +48,7 @@ extension Bool: BytesRepresentable {
 
 // MARK: - Streamable Integer
 
-extension BytesRepresentable where Self: BinaryInteger {
+extension LosslessBytesConvertible where Self: BinaryInteger {
 
     public init(_ bytes: Bytes) throws {
         guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
@@ -55,29 +60,29 @@ extension BytesRepresentable where Self: BinaryInteger {
     }
 }
 
-extension Int: BytesRepresentable {}
+extension Int: LosslessBytesConvertible {}
 
-extension Int8: BytesRepresentable {}
+extension Int8: LosslessBytesConvertible {}
 
-extension Int16: BytesRepresentable {}
+extension Int16: LosslessBytesConvertible {}
 
-extension Int32: BytesRepresentable {}
+extension Int32: LosslessBytesConvertible {}
 
-extension Int64: BytesRepresentable {}
+extension Int64: LosslessBytesConvertible {}
 
-extension UInt: BytesRepresentable {}
+extension UInt: LosslessBytesConvertible {}
 
-extension UInt8: BytesRepresentable {}
+extension UInt8: LosslessBytesConvertible {}
 
-extension UInt16: BytesRepresentable {}
+extension UInt16: LosslessBytesConvertible {}
 
-extension UInt32: BytesRepresentable {}
+extension UInt32: LosslessBytesConvertible {}
 
-extension UInt64: BytesRepresentable {}
+extension UInt64: LosslessBytesConvertible {}
 
 // MARK: - Streamable Floating Point
 
-extension BytesRepresentable where Self: FloatingPoint {
+extension LosslessBytesConvertible where Self: FloatingPoint {
 
     public init(_ bytes: Bytes) throws {
         guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
@@ -85,17 +90,17 @@ extension BytesRepresentable where Self: FloatingPoint {
     }
 
     public var bytes: Bytes {
-        withUnsafeBytes(of: self) { Bytes(rawValue: Array($0)) }
+        withUnsafeBytes(of: self) { Bytes($0) }
     }
 }
 
-extension Double: BytesRepresentable {}
+extension Double: LosslessBytesConvertible {}
 
-extension Float: BytesRepresentable {}
+extension Float: LosslessBytesConvertible {}
 
 // MARK: - RawRepresentable Bytes
 
-extension BytesRepresentable where Self: RawRepresentable, RawValue: BytesRepresentable {
+extension LosslessBytesConvertible where Self: RawRepresentable, RawValue: LosslessBytesConvertible {
 
     public init(_ bytes: Bytes) throws {
         let rawValue = try RawValue(bytes)
@@ -108,7 +113,7 @@ extension BytesRepresentable where Self: RawRepresentable, RawValue: BytesRepres
 
 // MARK: - Bytes
 
-extension Bytes: BytesRepresentable {
+extension Bytes: LosslessBytesConvertible {
 
     public init(_ bytes: Bytes) throws {
         self = bytes
@@ -121,7 +126,7 @@ extension Bytes: BytesRepresentable {
 
 extension Array where Element == Bytes {
 
-    subscript<T>(_ index: Int) -> T? where T: BytesRepresentable {
+    subscript<T>(_ index: Int) -> T? where T: LosslessBytesConvertible {
         try? T(self[index])
     }
 }
@@ -130,7 +135,7 @@ extension Array where Element == Bytes {
 
 extension Dictionary where Value == Bytes {
 
-    subscript<T>(_ key: Key) -> T? where T: BytesRepresentable {
+    subscript<T>(_ key: Key) -> T? where T: LosslessBytesConvertible {
         guard let bytes = self[key] else { return nil }
         return try? T(bytes)
     }
@@ -138,7 +143,7 @@ extension Dictionary where Value == Bytes {
 
 // MARK: - String Bytes
 
-extension String: BytesRepresentable {
+extension String: LosslessBytesConvertible {
 
     public var bytes: Bytes { bytes(using: .utf8) ?? [] }
 
@@ -150,7 +155,7 @@ extension String: BytesRepresentable {
 
 // MARK: - Data Bytes
 
-extension Data: BytesRepresentable {
+extension Data: LosslessBytesConvertible {
 
     public var bytes: Bytes { Bytes(data: self) }
 
@@ -161,7 +166,7 @@ extension Data: BytesRepresentable {
 
 // MARK: - UUID Bytes
 
-extension UUID: BytesRepresentable {
+extension UUID: LosslessBytesConvertible {
 
     public var bytes: Bytes {
         withUnsafeBytes(of: uuid) { Bytes($0) }
@@ -174,7 +179,7 @@ extension UUID: BytesRepresentable {
     }
 }
 
-extension Optional where Wrapped: BytesRepresentable {
+extension Optional where Wrapped: LosslessBytesConvertible {
 
     public init(_ bytes: Bytes?) {
         if let bytes = bytes, let wrapped = try? Wrapped(bytes) {

+ 1 - 1
Sources/Binary/Input.swift

@@ -50,7 +50,7 @@ public class Input {
         return out.prefix(count)
     }
 
-    public func read<T>(lenght: Int) throws -> T where T: BytesRepresentable {
+    public func read<T>(lenght: Int) throws -> T where T: LosslessBytesConvertible {
         let bytes = try read(lenght: lenght)
         return try T(bytes)
     }

+ 2 - 2
Sources/Binary/Streamable.swift

@@ -48,7 +48,7 @@ public protocol Readable {
 /// any type that conforms to both protocols.
 public typealias Streamable = Readable & Writable
 
-extension Readable where Self: BytesRepresentable {
+extension Readable where Self: LosslessBytesConvertible {
 
     public init(from input: Input) throws {
         let bytes = try input.read(lenght: MemoryLayout<Self>.size)
@@ -56,7 +56,7 @@ extension Readable where Self: BytesRepresentable {
     }
 }
 
-extension Writable where Self: BytesRepresentable {
+extension Writable where Self: LosslessBytesConvertible {
 
     public func write(to output: Output) throws {
         try output.write(bytes)

+ 7 - 7
Sources/Binary/TLV.swift

@@ -46,11 +46,11 @@ public struct TLV<Type, Lenght>: TypeLenghtValue where Lenght: BinaryInteger {
         self.value = value
     }
 
-    public func get<T>() throws -> T where T: BytesRepresentable {
+    public func get<T>() throws -> T where T: LosslessBytesConvertible {
         try T(value)
     }
 
-    public mutating func set<T>(_ value: T?) throws where T: BytesRepresentable {
+    public mutating func set<T>(_ value: T?) throws where T: LosslessBytesConvertible {
         self.value = value?.bytes ?? []
     }
 }
@@ -75,22 +75,22 @@ extension TLV: Writable where Type: Writable, Lenght: Writable {
 
 extension Sequence where Element: TypeLenghtValue, Element.Type_: Equatable, Element.Value == Bytes {
 
-    public func first<T>(valueOf type: Element.Type_) throws -> T? where T: BytesRepresentable {
+    public func first<T>(valueOf type: Element.Type_) throws -> T? where T: LosslessBytesConvertible {
         return try first(where: { $0.type == type }).map { try T($0.value) }
     }
 
     public func first<T>(where type: Element.Type_, _ predicate: (T) throws -> Bool) throws -> Element?
-        where T: BytesRepresentable {
+        where T: LosslessBytesConvertible {
         return try first(where: { try predicate(try T($0.value)) })
     }
 
     public func sorted<T>(field: Element.Type_,
                           by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Self.Element]
-        where T: BytesRepresentable {
+        where T: LosslessBytesConvertible {
         return try sorted(by: { try areInIncreasingOrder(try T($0.value), try T($1.value)) })
     }
 
-    public subscript<T>(_ type: Element.Type_) -> T? where T: BytesRepresentable {
+    public subscript<T>(_ type: Element.Type_) -> T? where T: LosslessBytesConvertible {
         try? first(valueOf: type)
     }
 }
@@ -105,7 +105,7 @@ extension RangeReplaceableCollection where Element: TypeLenghtValue, Element.Typ
 extension TLV: CustomDebugStringConvertible {
 
     public var debugDescription: String {
-        "(T:\(type) L:\(value.lenght))"
+        "(T:\(type)\nL:\(value.lenght)/nV:\(value.hexa)"
     }
 }
 

+ 3 - 3
Sources/KDB/Row.swift

@@ -58,7 +58,7 @@ extension Row where Column: Equatable {
         return Database.date(from: bytes)
     }
 
-    public subscript<T>(_ column: Column) -> T? where T: BytesRepresentable {
+    public subscript<T>(_ column: Column) -> T? where T: LosslessBytesConvertible {
         get { T?(self[column]) }
         set { self[column] = newValue?.bytes }
     }
@@ -71,7 +71,7 @@ extension Row where Column: Equatable {
 extension Sequence where Element: Row, Element.Column: Equatable {
 
     public func first<T>(column: Element.Column, where predicate: (T) throws -> Bool) throws -> Element?
-        where T: BytesRepresentable {
+        where T: LosslessBytesConvertible {
         try first(where: {
             guard let bytes = $0[column] else { return false }
             return try predicate(T(bytes))
@@ -79,7 +79,7 @@ extension Sequence where Element: Row, Element.Column: Equatable {
     }
 
     public func sorted<T>(column: Element.Column, by areInIncreasingOrder: (T, T) throws -> Bool) throws -> [Element]
-        where T: BytesRepresentable {
+        where T: LosslessBytesConvertible {
         try sorted(by: {
             guard let rhs = $0[column], let lhs = $1[column] else { return false }
             return try areInIncreasingOrder(T(rhs), T(lhs))

+ 2 - 2
Sources/KDBX/Header.swift

@@ -49,13 +49,13 @@ enum InnerHeader: UInt8, Streamable, Endable {
     public static var endValue: InnerHeader { .end }
 }
 
-enum Compression: UInt32, BytesRepresentable {
+enum Compression: UInt32, LosslessBytesConvertible {
     case none = 0
     case gzip = 1
     case count = 2
 }
 
-enum RandomStream: UInt32, BytesRepresentable {
+enum RandomStream: UInt32, LosslessBytesConvertible {
     case none = 0
     case arc4 = 1
     case salsa20 = 2

+ 10 - 7
Sources/KDBX/Variant.swift

@@ -134,7 +134,7 @@ public enum Variant {
         return value
     }
 
-    func unwrap<T>() throws -> T where T: BytesRepresentable {
+    func unwrap<T>() throws -> T where T: LosslessBytesConvertible {
         guard case let .Bytes(bytes) = self else { throw KDBXError.invalidValue }
         return try T(bytes)
     }
@@ -263,12 +263,7 @@ extension Dictionary: Streamable where Key == String, Value == Variant {
     }
 }
 
-extension Dictionary: BytesRepresentable where Key == String, Value == Variant {
-
-    public init(_ bytes: Bytes) throws {
-        let input = Input(bytes: bytes)
-        try self.init(from: input)
-    }
+extension Dictionary: CustomBytesConvertible where Key == String, Value == Variant {
 
     public var bytes: Bytes {
         let output = Output()
@@ -276,3 +271,11 @@ extension Dictionary: BytesRepresentable where Key == String, Value == Variant {
         return output.bytes ?? []
     }
 }
+
+extension Dictionary: LosslessBytesConvertible where Key == String, Value == Variant {
+
+    public init(_ bytes: Bytes) throws {
+        let input = Input(bytes: bytes)
+        try self.init(from: input)
+    }
+}