BytesRepresentable.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // BytesRepresentable.swift
  2. // This file is part of MiKee.
  3. //
  4. // Copyright © 2019 Maxime Epain. All rights reserved.
  5. //
  6. // MiKee is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // MiKee is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with MiKee. If not, see <https://www.gnu.org/licenses/>.
  18. import Foundation
  19. // MARK: - Bytes Representatble Protocol
  20. public protocol BytesRepresentable {
  21. init(_ bytes: Bytes) throws
  22. var bytes: Bytes { get }
  23. }
  24. // MARK: - Streamable Boolean Bytes
  25. extension Bool: BytesRepresentable {
  26. public init(_ bytes: Bytes) throws {
  27. guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
  28. self = bytes.withUnsafeBytes { $0.load(as: Self.self) }
  29. }
  30. public var bytes: Bytes {
  31. withUnsafeBytes(of: self) { Bytes($0) }
  32. }
  33. }
  34. // MARK: - Streamable Integer
  35. extension BytesRepresentable where Self: BinaryInteger {
  36. public init(_ bytes: Bytes) throws {
  37. guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
  38. self = bytes.withUnsafeBytes { $0.load(as: Self.self) }
  39. }
  40. public var bytes: Bytes {
  41. withUnsafeBytes(of: self) { Bytes($0) }
  42. }
  43. }
  44. extension Int: BytesRepresentable { }
  45. extension Int8: BytesRepresentable { }
  46. extension Int16: BytesRepresentable { }
  47. extension Int32: BytesRepresentable { }
  48. extension Int64: BytesRepresentable { }
  49. extension UInt: BytesRepresentable { }
  50. extension UInt8: BytesRepresentable { }
  51. extension UInt16: BytesRepresentable { }
  52. extension UInt32: BytesRepresentable { }
  53. extension UInt64: BytesRepresentable { }
  54. // MARK: - Streamable Floating Point
  55. extension BytesRepresentable where Self: FloatingPoint {
  56. public init(_ bytes: Bytes) throws {
  57. guard bytes.lenght == MemoryLayout<Self>.size else { throw BinaryError.invalidLenght }
  58. self = bytes.withUnsafeBytes { $0.load(as: Self.self) }
  59. }
  60. public var bytes: Bytes {
  61. withUnsafeBytes(of: self) { Bytes(rawValue: Array($0)) }
  62. }
  63. }
  64. extension Double: BytesRepresentable { }
  65. extension Float: BytesRepresentable { }
  66. // MARK: - RawRepresentable Bytes
  67. extension BytesRepresentable where Self: RawRepresentable, RawValue: BytesRepresentable {
  68. public init(_ bytes: Bytes) throws {
  69. let rawValue = try RawValue(bytes)
  70. guard let value = Self(rawValue: rawValue) else { throw BinaryError.invalidValue }
  71. self = value
  72. }
  73. public var bytes: Bytes { rawValue.bytes }
  74. }
  75. // MARK: - Bytes
  76. extension Bytes: BytesRepresentable {
  77. public init(_ bytes: Bytes) throws {
  78. self = bytes
  79. }
  80. public var bytes: Bytes { self }
  81. }
  82. // MARK: - Bytes Array
  83. extension Array where Element == Bytes {
  84. subscript<T>(_ index: Int) -> T? where T: BytesRepresentable {
  85. try? T(self[index])
  86. }
  87. }
  88. // MARK: - Bytes Dictionary
  89. extension Dictionary where Value == Bytes {
  90. subscript<T>(_ key: Key) -> T? where T: BytesRepresentable {
  91. guard let bytes = self[key] else { return nil }
  92. return try? T(bytes)
  93. }
  94. }
  95. // MARK: - String Bytes
  96. extension String: BytesRepresentable {
  97. public var bytes: Bytes { bytes(using: .utf8) ?? [] }
  98. public init(_ bytes: Bytes) throws {
  99. guard let string = String(bytes: bytes, encoding: .utf8) else { throw BinaryError.invalidValue }
  100. self = string
  101. }
  102. }
  103. // MARK: - Data Bytes
  104. extension Data: BytesRepresentable {
  105. public var bytes: Bytes { Bytes(data: self) }
  106. public init(_ bytes: Bytes) throws {
  107. self = Data(bytes.rawValue)
  108. }
  109. }
  110. // MARK: - UUID Bytes
  111. extension UUID: BytesRepresentable {
  112. public var bytes: Bytes {
  113. withUnsafeBytes(of: uuid) { Bytes($0) }
  114. }
  115. public init(_ bytes: Bytes) throws {
  116. guard bytes.lenght == MemoryLayout<uuid_t>.size else { throw BinaryError.invalidLenght }
  117. let uuid = bytes.withUnsafeBytes { $0.load(as: uuid_t.self) }
  118. self = UUID(uuid: uuid)
  119. }
  120. }
  121. extension Optional where Wrapped: BytesRepresentable {
  122. public init(_ bytes: Bytes?) {
  123. if let bytes = bytes, let wrapped = try? Wrapped(bytes) {
  124. self = .some(wrapped)
  125. } else {
  126. self = .none
  127. }
  128. }
  129. }