PlaceholderMessageBlock.swift 770 B

123456789101112131415161718192021222324252627282930
  1. //
  2. // PlaceholderMessageBlock.swift
  3. // OmniKit
  4. //
  5. // Created by Pete Schwamb on 10/24/17.
  6. // Copyright © 2017 Pete Schwamb. All rights reserved.
  7. //
  8. import Foundation
  9. public struct PlaceholderMessageBlock: MessageBlock {
  10. public let blockType: MessageBlockType
  11. public let length: UInt8
  12. public let data: Data
  13. public init(encodedData: Data) throws {
  14. if encodedData.count < 2 {
  15. throw MessageBlockError.notEnoughData
  16. }
  17. guard let blockType = MessageBlockType(rawValue: encodedData[0]) else {
  18. throw MessageBlockError.unknownBlockType(rawVal: encodedData[0])
  19. }
  20. self.blockType = blockType
  21. length = encodedData[1]
  22. data = encodedData.prefix(upTo: Int(length))
  23. }
  24. }